Is there a way to check if input is a int/char and treat them differently?

Here's my program, I currently use'999' to terminate the input
but if I want to use for example 'enter q to end' is there a way to do that?
thanks

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
 #include <iostream>
#include <cstdio>
using namespace std;

int main()
{

	int A[100],B[100];
	int i=0;
	int j=0;
	int x;
	while(true)
	{
		cout << "please enter data,enter 999 to end "; 
		cin >> x;
	
		if (x == 999)
\\is there a way to make it terminate with, for example 'q'?
			break;
		else
		{
			A[i]=x;
			B[i]=2*x;
			i++;
		}
		
	}
	
		i--;
		cout<<"your input	your output\n";

	for(j=0;j<=i;j++)
		{
		printf("%d\t\t\t%d\n",A[j],B[j]);
		}
return (0);
}
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main()
{
    char end;
    std::cin >> end;

    if ( end == 'q')
        std::cout << "the end\n";
    else
        std::cout << "q wasn't entered\n";

   return 0;
}

Last edited on
But I still want to enter the input into my array A if it is an int, I know I can add another line like
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
char y;
int main()
{
while (true)
{
cin>>y;
if (y=='q')
break;
else
return 0;
}
}

But that require the user to press enter to continue enter the next number, I don't want that
Last edited on
closed account (48T7M4Gy)
http://stackoverflow.com/questions/5029840/convert-char-to-int-in-c-and-c

The answer to that question is to adapt information from the above link. this may put severe limitations on the size and nature of the integers in the array.

You may have to go a step further and use string inputs and process those.
Last edited on
thanks
Topic archived. No new replies allowed.