reading multiple inputs using array

Feb 4, 2013 at 2:27am
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 
}
Feb 4, 2013 at 4:22am
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
}
Feb 8, 2013 at 2:17am
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 
}
Feb 8, 2013 at 3:22am
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 
Feb 11, 2013 at 12:09am
so I did this but now the program once a non numeric is entered will not complete the rest of the program?
Feb 11, 2013 at 4:32am
Can we see the new code?
Feb 11, 2013 at 5:10am
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);
}
  
  
Feb 11, 2013 at 6:47am
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" ;
}
Feb 14, 2013 at 2:32am
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?
Feb 14, 2013 at 2:40am
such as this need to integrate these three prototypes into my final program!
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
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

//prototypes
int readArray(int, int[]);
int avg(int, const int[]); 
int histogram(int, const int[], int[]);.
int main()
{
    const int sentinel = -1 ;
    const unsigned MAX_SCORE = 50 ;
    int score[MAX_SCORE] ;
    unsigned nScores = 0 ;

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

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

            if ( notDone )
                ++discarded;
        }
    }

    cout << "Recorded " << nScores << " values.\nDiscarded input " 
        << discarded << " times.\n" ;
}


using this for the histogram
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if (stat(nScores, score, avgScore, minScore, maxScore) == 0)
{
cout << "Average=" << avgScore << endl
<< "Max=" << maxScore << endl
<< "Min=" << minScore << endl;
int grade[5] = {0}; // = {} is for newer compilers only 
histogram(nScores, score, grade);
cout << "As: " << grade[0] << endl;
cout << "Bs: " << grade[1] << endl;
cout << "Cs: " << grade[2] << endl;
cout << "Ds: " << grade[3] << endl;
cout << "Fs: " << grade[4] << endl; 
}
else
cout << "no data" << endl;
}
Feb 14, 2013 at 3:18am
and possibly the code without using const unsigned ints if thats possible teacher very specific need to use const in MAX_SCORE = 50;
Feb 14, 2013 at 4:59am
needs to read something like this call to the array 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
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include <cctype>

//prototypes

int readArray(int, int[]);

int main()
{
    const int MAX_SCORE = 50;
    int score[MAX_SCORE];
	int nScores = readArray(MAX_SCORE, score);

    cout << "Input up to 50 test scores (0-100) enter -1 after last value input.\n";
    cout << readArray(MAX_SCORE, score) << endl;
   	
	unsigned discarded = 0;
    bool notDone = true;
    while (notDone && nScores < MAX_SCORE)
    {
        int input;
        if (cin >> input && (input >= 0 && input <= 100))
            score[nScores++] = input;
        else
        {
            if (!cin)
            {
                cin.clear() ;
                while (cin && !isdigit(cin.peek()))
                    cin.ignore();
            }
            else if (input < 0)
                notDone = false;

          
        }
    }

   
	return nScores;
	
}
Topic archived. No new replies allowed.