I've tried a lot of things and keep changing this code but I can't get it to stop require two inputs before it accepts a value for 'score'. Also, I've been having trouble finding a way to stop the cin from 'score' and 'answer' from allowing the user to hit enter without entering a value. The only way I've seen to do that is to accept them both as strings, but I was hoping to avoid that.
#include<iostream>
#include<iomanip>
#include<limits>**strong text**
using namespace std;
//void enterdata(string*, int*);
int main(){
// string names[];
// int testscores[];
string name;
int score;
char answer;
cout<<"This program asks the user to enter a list of names and test scores,"<<endl;
cout<<"then sorts the list into ascending order according to the scores"<<endl;
cout<<"and calculates the average test score."<<endl;
// enterdata(names, testscores);
do{
cout<<"Please enter a name: ";
getline(cin, name);
while(name.empty()){
cout<<"No entry detected. Please enter a name: ";
getline(cin, name);
}
cout<<name<<endl;
cout<<"Please enter a test score: ";
cin>>score;
while(!(cin>>score) || score<0){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout<<"Input not valid. Only positive integers will be accpeted."<<endl;
cout<<"Please enter a test score: ";
}
cout<<score<<endl;
cin.sync();
cout<<"Would you like to enter another student name and test score? [y/n] ";
cin>>answer;
while(answer!='y' && answer!='n'){
cout<<"Input not valid. Only y or n will be accepted."<<endl;
cout<<"Would you like to enter another student name and test score? [y/n] ";
cin>>answer;
}
cout<<answer<<endl;
cin.sync();
} while(answer == 'y');
return 0;
}