BigB63 BigB63 - 3 years ago 189
C++ Question

C++ cin requiring 2 or more inputs before it accepts a value

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;
}




Answer Source

The error is here:

while(!(cin>>score) || score<0){ //------ Here!
    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: ";
}

You are using the input stream again when you say cin>>score, and therefore the program prompts for more input! Think of it like a function call (hint: operator overloading!). I presume you wrote this (that is, !(cin>>score) because you want to test the stream for errors. In a small program like this, I wouldn't sweat it (school exercise?)

As to the second part of you question, about hindering enter from having an effect (the annoying newline), I'm quite sure you can't do this easily. I'd politely ask whether you really want to spend time on this though (and if it is a school exercise, everyone else is going to have the same issue)

Here is the cleaned and corrected code:

#include <iostream>
#include <iomanip>
#include <limits>
#include <string>

using std::cout;
using std::cin;
using std::string;
using std::endl;

//void enterdata(string*, int*);

int main() {
  //string names[];
  //int testscores[];
  string name;
  int score;
  char answer = 'y';

  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);

  while(answer == 'y') {
    cout << "Please enter a name: ";
    std::cin >> name;
    while (name.empty()) {
        cout << "No entry detected. Please enter a name: ";
        std::cin >> name;
    }
    cout << name << endl;
    cout << "Please enter a test score: ";
    cin >> score;
    while (score<0) {
        cin >> score;
        cin.clear();
        cin.ignore(std::numeric_limits<std::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();
 } 
return 0;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download