Results 1 to 4 of 4

Thread: Using cin and while to loop until done

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2002
    Posts
    6

    Using cin and while to loop until done

    Hello: the following code is meant to capture grade data input in a console application. After that data for one student has been captured, the application askes whether data for another student should be captured. The program uses a while loop, where the boolean "newStudent" should determine whether to re-loop or proceed to the last steps of the program. I do not think that the input section at the end of the while loop is working?


    int main() {

    bool newStudent=true;
    int resp;


    while(newStudent) {
    get grade data and put into vectors--this all seems to work

    cout << "Will there be another student's grades? " << endl;
    cout<<"If yes, type 1. If no, type 0."<<endl;
    cin >> resp;
    if (resp == 1) newStudent = true; else newStudent=false;
    }

    DISPLAY THE RESULTS

    getch();
    return 0;

    } // end of main

  2. #2

    Thread Starter
    New Member
    Join Date
    Jan 2002
    Posts
    6
    After working more on this, I think that the piece of code originally shown is probably ok. What seems to be happening is that when the program runs through the while loop to obtain student grade data the second time (i.e. newStudent is true), the "cin>>" lines are not waiting for me to provide input the way that they did the first time through the loop. (I do see the cout lines that are prompting for the data input.
    Can anyone see why the cin would not work on the second pass through the while loop?

    The whole loop is pasted here
    while (newStudent) {

    cout<<"Hey, what's your first name?"<<endl<<endl;
    cin >> name;
    nameVec.push_back(name);
    cout << "Hello "<<name<<"!"<<endl<<endl;
    cout<<"Please input your various scores as prompted below:"<<endl;
    cout<<"Please enter your final exam grade."<<endl;
    cin>>final;
    cout<<"Please enter your midterm exam grade."<<endl;
    cin>>midterm;
    cout<<"Please enter your homework grades separated by "
    << "spaces. When you are done, go to a new line and input "
    <<"the end of file character" <<endl;

    while (cin>>hmwk) {
    hmwkVec.push_back(hmwk);
    }
    // determine the length of the homework vector
    /*
    vecsz hmwkSize=hmwkVec.size();

    // calculate the average homework score
    int total = 0;
    for (int i=0; i!=hmwkSize; i++)
    total += hmwkVec[i];

    if (hmwkSize == 0) return 5; //breaking out here.
    avgHmwk = total/hmwkSize;
    */
    avgHmwk = 75;
    // calculate the overall grade

    grade = 0.4*final + 0.2*midterm + 0.4*avgHmwk;
    gradeVec.push_back(grade);
    hmwkVec.clear();
    // ask whether we will be collecting data for another student

    cout << "Will there be another student's grades? (y/n) " << endl;
    resp=getch();
    cout<<resp<<endl;
    getch();
    if (resp == 'n') newStudent = false; else newStudent=true;
    cout<<"newStudent value is "<< newStudent<<endl;

    } //end of while

    // print out the names and the grades
    sizeNameVec = nameVec.size();
    cout<<"Here are the students and their grades"<<endl<<endl;
    for (int i=0; i!=sizeNameVec; i++)
    cout<<nameVec[i]<<"\t"<<gradeVec[i]<<end;




    getch();
    return 0;
    } //end of main

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It probably wouldn't work if you enter anything else than a number - that sets the stream to a "bad" state in which it won't continue until you reset it.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2002
    Posts
    6
    That's right. The problem lies with the CIN being put into an error state. Including a clear statement after the reading of data solved the problem.

    Thank you.

Posting Permissions

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



Click Here to Expand Forum to Full Width