dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Program won't output correctly

  1. #1
    Join Date
    May 2018
    Posts
    2

    Program won't output correctly

    Hi all, I have written code and my task is to print if a flower grows in the sun or shade. I am supposed to use a while loop and implement code to open my .cpp file which contains a list of flowers. I've gotten most of it figured out I'm to the point where it prints the first flower, Astilbe, but then times out. Anyone have any suggestions? Thank you!
    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
        // Declare variables here
        ifstream fin;
        string flowerName;
        string sun_or_shade;
        ifstream data_in;
        ofstream data_out;
    
    
        // Open input file
        fin.open("flowers.dat");
        fin >> flowerName;
        fin >> sun_or_shade;
        while (!fin.eof())
    
    
            // Write while loop that reads records from file.
            while(!(data_in.eof()))
            {
                data_in >> flowerName;
                data_in >> sun_or_shade;
                cout << flowerName << " grows in the " << sun_or_shade << endl;
            }
        return 0;
    } // End of main function
    Here is my .cpp file:
    Code:
    Astilbe Shade
    Marigold Sun
    Begonia Sun
    Primrose Shade
    Cosmos Sun
    Dahlia Sun
    Geranium Sun
    Foxglove Shade
    Trillium Shade
    Pansy Sun
    Petunia Sun
    Daisy Sun
    Aster Sun

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,229

    Re: Program won't output correctly

    You seem to stay on the first line - that's a problem!
    Try to search for ifstream example...
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,416

    Re: Program won't output correctly

    Why do you open the input file to fin, read 2 items, and then use data_in to read more items??? data_in stream hasn't got a file opened!

    Also, stream extraction returns the state of the stream which can be tested - rather than use .eof(). So you only need one loop

    Code:
    while (fin >> flowerName >> sun_or_shade)
    ...
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.4)

  4. #4
    Join Date
    May 2018
    Posts
    2

    Re: Program won't output correctly

    Thanks, I have changed
    Code:
    while(!(data_in.eof()))
            {
                data_in >> flowerName;
                data_in >> sun_or_shade;
                cout << flowerName << " grows in the " << sun_or_shade << endl;
            }
        return 0;
    to
    Code:
    if (fin.is_open())	// Open input file
    	{
    		while (fin >> flowerName >> sun_or_shade)	// Write while loop that reads records from file.
    		{
    			cout << flowerName << " grows in the " << sun_or_shade << endl;
    		}
    	}
       fin.close(); 	
       return 0;
    Now all of the flowers print if they are in the sun or shade except for the first one, Astilbe.

  5. #5
    Join Date
    Feb 2017
    Posts
    344

    Re: Program won't output correctly

    Quote Originally Posted by helpwithcplusplus View Post
    except for the first one
    In your original post you do this before you enter the while loop,

    Code:
        fin.open("flowers.dat");
        fin >> flowerName;
        fin >> sun_or_shade;
    If these assignments to flowerName and sun_or_shade are still present in the code and you don't print them then they will be missing in the output. So if the assignments are there, remove them and the first line will be printed.
    Last edited by wolle; May 20th, 2018 at 10:43 AM.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,416

    Re: Program won't output correctly

    Small point, but if the specified file can't be opened then it is usual to display a message. Consider

    Code:
    if (fin.is_open())    // Open input file
        while (fin >> flowerName >> sun_or_shade)    // Write while loop that reads records from file.
            cout << flowerName << " grows in the " << sun_or_shade << endl;
    else
        cout << "Cannot open input file" << endl;
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.4)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)