C++ Beginner Question

I am having to write a program for class, and there is one part that I am having a hard time with. I know there is probably a very simple answer to this, but I have been searching for this.

Here is my problem:

I need to prompt the user for a value. I have successfully completed this, however if the user just presses enter (without entering any value). I need the value to default to zero. The problem, I do not know how to determine if the user enter no value. I have tried null, 0, " ", etc. Can someone please give me some guidance.

1
2
3
4
5
6
7
	cout << "Enter your account balance: $";
	cin >> balance;
	if (balance == null)
	{
		cout << "No balance was entered! Balance is now set to zero!" << endl;
		balance = 0;
	}
Last edited on
closed account (Dy7SLyTq)
try if balance == "\n".
" " is a space and would only work if they hit space (and you would have to compare against a char* or a string; you probably meant ' ') the definition of NULL (not null) is #define NULL 0 (or something along those lines) so it wouldnt be that much different from a literal zero. and of course zero. im guessing balance is an int, so it would be perfectly valid for them to enter zero
Thank you for reply. I did try this however, it just enters another line down. I have to enter some character and then enter again for it to proceed to the next command. But, I am able to verify that it does work to an extent. I had to format it like then

1
2
3
4
5
	if (balance = int('\n'))
	{
		cout << "No balance was entered! Balance is now set to zero!" << endl;
		balance = 0;
	}



Please enter your account number: 1001
Enter your account balance: $





x
No balance was entered! Balance is now set to zero!
Test
The principle amount in account # 1001 is: $ 0
Press any key to continue . . .

Last edited on
closed account (G30oGNh0)
if(balance = int('\n'))

Your are assigning balance to \n. I think you mean.

if(balance == int('\n'))
The user must enter something for this statement to execute: cin >> balance;

If you want the user to be able to simply press [enter], it might be better tp get the user input as a string and then convert the string to an numeric type (double or int).
I may give that a shot. What you said makes complete sense to me. Thank you!


Chervil (2519)
The user must enter something for this statement to execute: cin >> balance;

If you want the user to be able to simply press [enter], it might be better tp get the user input as a string and then convert the string to an numeric type (double or int)
Last edited on
If you go that route, I recommend the use of a stringstream (though there are other methods).
closed account (G30oGNh0)
I don't think the enter key is \n anyway, have you tried \r for the return carriage?
I don't think the enter key is \n anyway, have you tried \r for the return carriage?

In this context, that is not really relevant, since the enter key itself would not be stored as part of the user input into a numeric variable. Even if the input is of type std::string, the enter key is either discarded or left in the input buffer, rather than forming part of the string content.
closed account (Dy7SLyTq)
@chervil: i thought it might, and was actually going to suggest string stream, but since he claimed to be a beginner i was trying to think of another solution
Well, I agree it's not a good idea to overload a beginner with too many new ideas all at once. But in practice a stringstream can be extremely simple to use, because it will have a lot of behaviour in common with the already fairly familiar cin and cout streams.
Topic archived. No new replies allowed.