I tried to write a code that asked me to input numbers one by one and when a certain char was inserted ( in this case ‘x’ ) it would stop the loop. But when I insert that char it starts spamming me with “Insert Number” . I think that the fault is that I’m trying to insert a char in an int array, but I can’t think a way around it.
long int numbers[100]={0};
char h='y';
int index=0;
do
{
cout << "Insert Number : ";
cin >> numbers[index];
h=(char)numbers[index];
index++;
}
while(h!='x');
You should write a loop as:
It will read all the integers, untill you enter some invalid input, be it
'x'or any other character. Now if you want to skip all invalid inputs and continue reading integers (which might be after invalid inputs), and want to consider only'x'to exit from the loop, then wrap the above loop with another loop as:One piece of advice: prefer using
std::vector<long int>overlong int numbers[100]. What if user entered more than100integers, then your program will be corrupted.