This is part of a larger program and this is the only part that I can't get down. Right now, it runs fine once through, but telling it yes to repeat the loop at the end does not allow the user to input a new name for student once the loop repeats. How can I allow for name to be inputted again when the loop repeats? Any help is appreciated.

#include <iostream>



using namespace std;

int main ()
{
    string name;
    char choice;
    do
    {
    
    cout << "What is the student's name?" << endl;

    getline (cin, name);  
    
    cout << "Want to enter the data again? (y/n)" << endl;
    cin >> choice;
    
    while ((choice != 'y') && (choice != 'Y') && (choice != 'n') && (choice != 'N'))
    {
          cout << "Please enter a valid choice" << endl;
          cin >> choice;
    }
    
    }
    while ((choice == 'y') || (choice == 'Y'));
    
    
    
    getchar();


    return 0;
}

Recommended Answers

All 2 Replies

After your cin >> choice; add code to read the rest of the input buffer to clear it out, up the and including the \n.

When you enter values, ALL characters you type are in the buffer, including the <ENTER>. When you read a single character, the \n is left in the buffer for the next read.

thanks that fixed it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.