reading multiple inputs using array

Josh Henry (19)
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using std::cin;
using std::cout;
using std::endl;


int main()
{
  
  const int 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 
}
shacktar (1143)
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
}
Josh Henry (19)
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?!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
using std::cin;
using std::cout;
using std::ios;
using std::string;
using std::endl;
#include <string>
#include <cstdlib>



int main()
{

  const int 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();
    } 
	else if (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 
}
shacktar (1143)
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
else if (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 
Josh Henry (19)
so I did this but now the program once a non numeric is entered will not complete the rest of the program?
shacktar (1143)
Can we see the new code?
Josh Henry (19)
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??
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
using std::cin;
using std::cout;
using std::ios;

using std::endl;

#include <cstdlib>

// function prototypes
int readArray(int, int[]);

  int main()
{
  const int 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);
}
  
  
cire (2347)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <cctype>

int main()
{
    const int sentinel = -1 ;
    const unsigned scoreCapacity = 50 ;
    int score[scoreCapacity] ;
    unsigned nScores = 0 ;

    std::cout << "Input up to 50 numbers (0-100) enter -1 after last value input.\n" ;

    unsigned discarded = 0 ;
    bool notDone = true ;
    while ( notDone && nScores < scoreCapacity )
    {
        int input ;
        if ( std::cin >> input && (input >= 0 && input <= 100) )
            score[nScores++] = input ;
        else
        {
            if ( !std::cin )
            {
                std::cin.clear() ;
                while ( std::cin && !std::isdigit(std::cin.peek()) )
                    std::cin.ignore() ;
            }
            else if ( input == sentinel )
                notDone = false ;

            if ( notDone )
                ++discarded ;
        }
    }

    std::cout << "Recorded " << nScores << " values.\nDiscarded input " 
        << discarded << " times.\n" ;
}
Registered users can post here. Sign in or register to post.