Console Printing Problem

closed account (oyRk92yv)
Hello
I am using Microsoft Visual Studio 2010 and i have recently typed these codes, afterwards i have launched build and it was successful but when i launch run the console only prints Enter a number less than 10 or greater than 100:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #include <iostream>
int main()
{
	int x;
	std::cout << "Enter a number less than 10 or greater than 100: ";
	std::cin >> x;
	std::cout << "\n";

	if (x >= 10)
		if (x > 100)
			std::cout << "More than 100. Thanks!\n";
		else
			std::cout << "Less than 10. Thanks!\n";

	return 0;
}
Last edited on
Line 12 - the else will pick up cases where x>=10 but <=100. That doesn't match the text you have in line 13.


Did you mean the else to be part of the first if statement?

1
2
3
4
5
6
7
	if (x >= 10)
	{
		if (x > 100)
			std::cout << "More than 100. Thanks!\n";
	}
	else
		std::cout << "Less than 10. Thanks!\n";
Last edited on
Did you type a number and hit Enter?

Note that the way you have written the if statements it will only print something if you enter a number that is larger or equal to 10.
closed account (oyRk92yv)
Thank you guys for replying i appreciate it, wildblue the truth is i am still a beginner learning the C++ Programming Language from Sams Teach Yourself C plus plus in One Hour a Day 6th Edition so i am not sure what is the problem since i have followed the instructions accordingly.
closed account (oyRk92yv)
and i want to know if the problem is related to the compiler or just the code.
These are the results the code gives. It doesn't accurately represent what was entered. (I just added in a line to output a little more text and the value actually entered.) If you want the else to pair up with the first if, then you can add the set of curly braces I used in the earlier post.

Enter a number less than 10 or greater than 100: 
I entered 5. The if/else thinks this: 

Enter a number less than 10 or greater than 100: 
I entered 10. The if/else thinks this: Less than 10. Thanks!

Enter a number less than 10 or greater than 100: 
I entered 50. The if/else thinks this: Less than 10. Thanks!

Enter a number less than 10 or greater than 100: 
I entered 100. The if/else thinks this: Less than 10. Thanks!

Enter a number less than 10 or greater than 100: 
I entered 150. The if/else thinks this: More than 100. Thanks!


closed account (oyRk92yv)
I appreciate your help wildblue but i did add the braces and yet the output is the same.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
int main()
{
	int x;
	std::cout << "Enter a number less than 10 or greater than 100: ";
	std::cin >> x;
	std::cout << "\n";

	if (x >= 10)
	{
		if (x > 100)
			std::cout << "More than 100. Thanks!\n";
	}
	else
		std::cout << "Less than 10. Thanks!\n";
	
	return 0;
}

Enter a number less than 10 or greater than 100:
closed account (oyRk92yv)
Is the problem related to the compiler.
What are you inputting when you get that prompt?
closed account (oyRk92yv)
wildblue could you please rewrite the codes completely so i could copy and paste to my compiler, Thank you in advance.
You've got the code in the post above that can be copied and pasted.

I'm curious though - when you get the output
Enter a number less than 10 or greater than 100:
- what value are you putting in? The cin is waiting for the user to input a number to be used.
closed account (oyRk92yv)
Thank you very much i have figured it out.
Topic archived. No new replies allowed.