Can't let character through input stream

I'm using an if (!cin) expression to test whether user input a character or a number, but I want to let the '#' character through, as I want using that character to cause the loop to break.

I tried implementing it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int fillScore(int ar[], const int n)
{
	int i;

	for (i = 0; i < n; i++)
	{
		std::cout << "Score " << i + 1 << ": ";
		std::cin >> ar[i];
		if ((ar[i] != '#') && (!std::cin))
		{
			std::cout << "Only numbers are accepted.\n";
			std::cin.clear();
			std::cin.ignore(1000, '\n');
			i--;
			continue;
		}
		else if ((ar[i] < 0) || (ar[i] == '#'))
			break;
	}

	return i;
}


Is there no way to do this this simply, or have I done it the wrong way?
Receive the input as a char. Check it's '#' or in the range of characters from '0' to '9'.

If it is, accept it and process it further. If not, dump it.

If you want to accept numbers of more than one digit, receive the input as a string and process similarly.
Last edited on
That sounds like a good idea, but I need the input to be integers as it is processed further. In another function it is used for finding an average etc.
If you tell cin to expect an integer ( std::cin >> ar[i]; ), and you feed it '#' , then cin will fail. You cannot tell it to expect an int value and then feed it something that is not an int.

Fetch input to a string.
Look at the string.
If the string is a number, use std::stoi ( http://en.cppreference.com/w/cpp/string/basic_string/stol ) to turn it into an int, and then use that int.
If the string is '#' , do whatever else you're going to do with it.
If the string is something else, do whatever you do in that case.
Last edited on
Thank you, that was basically my question, if I could somehow "trick" the input stream, but I guess I'll go with that
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>
#include <sstream>
#include <string>


int fillScore( int ar[], const int n )
{
   std::string answer;

   std::cout << "Enter a maximum of " << n << " scores, concluded by a negative or #\n";

   int i = 0;
   while ( i < n )
   {
      std::cout << "Score " << i + 1 << ": ";
      std::getline( std::cin, answer );

      // First check for #
      int pos = answer.find_first_not_of( ' ' );
      if ( pos != std::string::npos && answer[pos] == '#' ) break;

      // Now try to input an integer
      std::stringstream ss( answer );
      if ( !( ss >> ar[i] ) ) std::cout << "Only numbers are accepted.\n";
      else if ( ar[i] < 0 )   break;        // what did you intend here?
      else                    i++;
   }

   return i;                                // returns the actual number input (last good index + 1)
}


//======================================


int main()
{
   const int MAXNUM = 100;
   int a[MAXNUM];

   int num = fillScore( a, MAXNUM );

   std::cout << "\nNumber entered: " << num << '\n';
   std::cout << "Scores: ";
   for ( int i = 0; i < num; i++ ) std::cout << a[i] << " ";
}



Enter a maximum of 100 scores, concluded by a negative or #
Score 1: 10
Score 2: 25
Score 3: 15
Score 4: #

Number entered: 3
Scores: 10 25 15
Topic archived. No new replies allowed.