so i have this program that needs to read multiple numbers entered in one line seperated by a space and after last number -1 to end the count then it counts how many scores were entered. only problem im having is that i need to make it so that no numeric input does not mess up the count this is what it is so far
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
constint MAX_SCORE = 50; // the maximum #of scores that a user can enter
int score[MAX_SCORE]; // create storage for up to 50 scores
int nScores = 0; // count the number of scores entered
// read the scores from the keyboard, space and/or newline delimited
for (int i = 0; i < MAX_SCORE; i++)
{
cout << "Input up to 50 numbers (0-100) enter -1 after last value input: " << i << endl;
cin >> score[i];
if (score[i] < 0)
break; // enter no more scores after the 1st negative is found
else
nScores++; // count the score if it is non-negative
}
cout << " The number of scores entered was: " << nScores << endl; // say how many scores entered
}
You can use the cin::fail() to determine if the previous stream operation has failed. Here, it could be used to see if the last number entered was non-numeric.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
cin >> score[i];
if(cin.fail())
{
//what was just read in is non-numeric
//make it so the last input doesn't count
//remember to clear the error flags before reading again
cin.clear();
}
else
{
//proceed as normal
}
so i tried that and it worked but im suppose to read the int as strings and covert to numeric? this is what I tried but after seeing a non numeric the program does not read any input after?!
#include <iostream>
using std::cin;
using std::cout;
using std::ios;
using std::string;
using std::endl;
#include <string>
#include <cstdlib>
int main()
{
constint MAX_SCORE = 50; // the maximum #of scores that a user can enter
int score[MAX_SCORE]; // create storage for up to 50 scores
int nScores = 0; // count the number of scores entered
// read the scores from the keyboard, space and/or newline delimited
cout << "Input up to 50 numbers (0-100) enter -1 after last value input: " << endl;
for (int i = 0; i < MAX_SCORE; i++)
{
cin >> score[i];
if(cin.fail())
{
cin.clear();
}
elseif (score[i] > 100)
{
cin.clear();
}
else
{
cin >> score[i];
//proceed as normal
if (score[i] < 0)
break; // enter no more scores after the 1st negative is found
else
nScores++; // count the score if it is non-negative
}}
cout << " The number of scores entered was: " << nScores << endl; // say how many scores entered
}
You should add a call to cin.sync() as well after clearing the error flags. There are some unread characters left in the stream after the failed read and one of those characters probably ended up being read as a negative number.
1 2 3 4 5 6
if(cin.fail())
{
cin.clear();
cin.sync(); // <-- Add this call here
}
Also, the call to cin.clear() is not necessary if the score is greater than 100 (this doesn't count as a failed read).
1 2 3 4 5
elseif (score[i] > 100)
{
cin.clear(); // <-- This is not necessary
}
Also, you're reading scores twice in the success case.
1 2 3 4
else
{
cin >> score[i]; // <-- You already read the score above
//proceed as normal
so really i need to read input into an array then calculate averages and such so its something like this but cant figure out how to take input from a string? convert to numeric and then put it into the array prototype readArray??
#include <iostream>
using std::cin;
using std::cout;
using std::ios;
using std::endl;
#include <cstdlib>
// function prototypes
int readArray(int, int[]);
int main()
{
constint MAX_SCORE = 50; // the maximum #of scores that a user can enter
char score[MAX_SCORE];
int scores;
cout << readArray(MAX_SCORE, score) << endl;
cout << "enter #" << endl;
for (int i = 0; i < MAX_SCORE; i++)
{
cin.getline(score, MAX_SCORE);
scores = atoi(score);
if (score[i] < 0)
break;
else
scores++;
}
readArray(MAX_SCORE, score);
}
ok now if i need to make a prototype for reading the values recorded say // function prototypes
int readArray(int, int[]); how would i plug that into the already given code?