while loop problem

This is a discussion on while loop problem within the C++ Programming forums, part of the General Programming Boards category; Ok, this is another question I hate to even post but here goes. The program never waits for an input ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    15

    while loop problem

    Ok, this is another question I hate to even post but here goes.

    The program never waits for an input for cin >> newstudent .
    I have determined that the "while(cin >> x)" is the problem by commenting this part out, but I don't understand why.

    Thanks in advance.

    Code:
    #include<iostream>
    #include<string>
    #include<cstdlib>
    #include<fstream>
    #include<vector>
    
    using namespace std;
    
    struct Student_info
    {
        string name;
        double midterm, final;
        vector<double> homework;
    };
    
        
    
    int main()
    {
        Student_info record;
        string newstudent = "y";
        
        
        
      while (newstudent == "y")
      {  
        cout << "Name: ";
        cin >> record.name;
        
        cout << "\tMidterm: ";
        cin >> record.midterm;
        
        cout << "\tFinal: ";
        cin >> record.final;
      
        cout << endl << "Please enter homework grades followed by end-of-file.";
        cout << endl;
        vector<double> homework;
        int x;
     
        while(cin >> x)
           record.homework.push_back(x);
        
     
        ofstream outfile("Student_records.txt");
        outfile << record.name << " ";
        outfile << record.midterm << " ";
        outfile << record.final << " ";
        for(int i = 0; i != record.homework.size(); ++i)
            outfile << record.homework[i] << " ";
            
        outfile.close();
       
        cout << "Student has been entered into record." << endl; 
        cout << "Enter another student? (y/n) " << endl;
        cin >> newstudent;
      }  
    system("pause");
    return 0;
    }

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Well, what happens when someone enters an int?

    They have to type some numbers, then press enter, at which point the data is put in the buffer and the numbers extracted. However, the data following the last number, like the newline, are not removed from the input buffer, so the next string read grabs "\n" immediately.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    15
    Thanks Cat.

    I understand your explanation. Great help.

    Is there a way to flush the input buffer or is there a better solution? (i'm very new at this if you haven't figured that out yet )

    I'm looking through other resources in the meantime but figured I'd ask while I've got you here.

  4. #4
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Away.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    15
    Thanks for the links, but I'm still not getting it to work. I'm honestly not even sure what to ask now. I've tried everyway I can think of using the info in the example you provided with no luck.

    Sorry I'm a little slow picking up on this.

    Thanks for any more help you can provide.
    Last edited by MedicKth; 09-30-2003 at 01:05 AM.

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    15
    Ok, I'm going to post the code I have so far so somebody can tell me if I'm way off base here. I'm still not able to get it to ask if there are more students. As always any and all help is GREATLY appreciated.


    Code:
    #include<iostream>
    #include<string>
    #include<cstdlib>
    #include<fstream>
    #include<vector>
    
    using namespace std;
    
    struct Student_info
    {
        string name;
        double midterm, final;
        vector<double> homework;
    };
    
        
    
    int main()
    {
        Student_info record;
        string newstudent = "y";
        char ch;
       
        
      while (newstudent == "y")
      {
        cout << "Name: ";
        cin >> record.name;
        while ((ch = getchar()) != '\n' && ch != EOF);  //flush input
    
        cout << "Midterm: ";
        cin >> record.midterm;
        while ((ch = getchar()) != '\n' && ch != EOF);  //flush input
    
        cout << "Final: ";
        cin >> record.final;
        while ((ch = getchar()) != '\n' && ch != EOF);  //flush input
    
        cout << endl << "Please enter homework grades followed by end-of-file.";
        cout << endl;
        vector<double> homework;
        int x;
        while (cin >> x)
        {
           record.homework.push_back(x);
        }
        while ((ch = getchar()) != '\n' && ch != EOF);    //flush input
        
        ofstream outfile("Student_records.txt");
        outfile << record.name << " ";
        outfile << record.midterm << " ";
        outfile << record.final << " ";
        for(int i = 0; i != record.homework.size(); ++i)
            outfile << record.homework[i] << " ";
            
        outfile.close();
     
        cout << "Student has been entered into record." << endl; 
        cout << "Enter another student? (y/n) " << endl;
        cin >> newstudent;
        
           
        
      }  
    system("pause");
    return 0;
    }

  7. #7
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Simple question: what happens in your code? Tell us what is happening and not only what you wanted it to do.

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    15
    As I have it now, It gets a name, midterm grade, final grade and then homework grades until an EOF is reached. When the user enters and EOF (ctrl z, is this correct?) it then waits until Enter is pressed again. When enter is pressed it then asks for another name, etc.

    I want it to ask whether or not there are more students and if yes restart the loop.


    I understand what Cat said about the input buffer and have read the links provided by Confuted but obviously I'm still not getting it right.

  9. #9
    Registered User
    Join Date
    Oct 2003
    Posts
    28

    simple

    try this:

    cin.clear();
    cin >> newstudent;

  10. #10
    Registered User
    Join Date
    Sep 2003
    Posts
    15

    Thumbs up

    Wow that was simple. It works great. Thanks Pratip. Now I'll go read up on that to see exactly what it does.

    And thanks for all of the other replies too. I learned something new from each one which has lead me on several different tracks trying to figure out other things. Must.....stay....focused.

    Again thanks.

  11. #11
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    usually I would just use cin.ignore(1)... all that does is ignore the next 1 character...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  12. #12
    Registered User
    Join Date
    Sep 2003
    Posts
    15
    For some reason the cin.ignore(1) is not working. It still enters the infinite loop. The cin.clear() works perfect, but I still wanted to try your advice. Any idea why this doesn't work?


    Also, I was thinking about this and perhaps I don't understand what Cat said as well as I thought. If the problem is caused by a /n in the input buffer because <enter> was pressed in the previous entry, then how am I able to input the name and then input the midterm grade? Why doesn't this second cin statement just take the newline also?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  3. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM
  4. Loop problem
    By Tesnik in forum C++ Programming
    Replies: 29
    Last Post: 08-23-2007, 10:24 AM
  5. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21