Hey,
My codes asks for a number, if the user inputs something else it gives an error message and asked for a number again.
If the user inputs a number, it should cout whatever number the user chose.

But it doesn't work as intended...
I get the error message the ammount of letters i type in, for example
a will give me the error message once
aaa will give me the error message 3 times

Also, if I type in 77 it says that I chose 7........

Here's the code:

#include <iostream>
#include <climits>
#include <cstdio>
#include <cstdlib>

int main()
{
    using namespace std;
    /* Variables */ 
    char user_input_number;
    /* End of Variables */
    
cout
<<"############################################################################################"
<<"############ Welcome to the house of horrors! ############"
<<"##########################################################################################"
<<endl
<<endl
<<"Please pick a number! ";

cin>>user_input_number;
while(isdigit(user_input_number)==false)
{
  
 cout <<endl
      <<"An integer you bozo -_^"
      <<endl
      <<"Try again: "; cin>>user_input_number;   
    
}

cout
<<"You chose: "<<user_input_number;

cout <<endl<<endl;
system("pause");

return 1;
}

Recommended Answers

All 2 Replies

I see a few things here.

1. main() must return zero (0) if the program ends normally/successfully. The operating system considers any other return value to be indicative of an application error.

2.You are using a char as an input variable. A char is only capable of holding a single character, it can not read multi-digit numbers from the input stream in the same way as an int can for example. Is this your intent? If you want to read multi-digit numbers, you need to change the declaration of your input variable and your error checking must be expanded.

3.

while(isdigit(user_input_number)==false) {
 //...
}

This is an inefficient way of doing this. Assuming you actually want your input variable to be a char, this is a better way of writing it:

while (!isdigit(user_input_number)) {
  //...
}

I just want to complete what Fbody did, and add a few more things. If you want to take an integer from the console, you must use int (instead of char). Standart isdigit() function takes an int as an argument, but uses it as a char actually. Logically that function is something like this:

int isdigit(int c)
{
return (c >= '0' && c <= '9');
}

The example below takes an integer from the console, and prints it after that.
What will happen if you try to insert some non digit instead?
First: cin will fail (std::cin will set its flag that input operation is failed)
Second: We will clear that flag (using cin.clear())
Third: Because of the unsuccessfull input operation, the data in the input buffer is still there. We don`t need that data and we have to remove it from the buffer (to clear the input buffer).
cin >> dummy;
Now we are ready to try to take another integer from console.

#include <iostream>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <string>

int main()
{
    using namespace std;
    int user_input_number;
	string dummy;
    
cout
<<"############################################################################################"
<<"############ Welcome to the house of horrors! ############"
<<"##########################################################################################"
<<endl
<<endl;

bool success_input = false;
do{
	cout << "Please pick a number! ";
	cin >> user_input_number;
	if(cin.fail())		// Is input operation is failed?
	{
		// Yes
		cin.clear();	// Clear istream flag
		cin >> dummy;	// Remove data from input buffer
		cout <<endl
		  <<"An integer you bozo -_^"
		  <<endl;
    
	}
	else
	{
		// No
		success_input = true;
	}
}while(!success_input);

// Print the number
cout <<"You chose: " << user_input_number;

cout << endl << endl;
system("pause");

return EXIT_SUCCESS;
}

If you have questions about the code, please don`t hesitate to write again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.