Problem from 50 to 100(both included)

Hi... I made this simple programm but now I can understand why I am having problems when entering number from 50 to 100... it always says in the end "00 a 49"... help pls...

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
  #include "stdafx.h"
#include <iostream>

using namespace std;
int x;

int minus100()
{
	if(x >= 50)
		cout << "50 a 100\n";
	else
	 cout << "00 a 49\n";

	return 0;
}

int main()
{
	cout << "Enter: " ;
	int x;
	cin >> x ;
	if( x <= 100)
		minus100();
	else
	 cout << "> 100\n" ;

	system("pause");
	return 0;
}
What you could do instead of (else) is do (else if).
else if ( x < 50) 
Last edited on
okay, you declared two x, one that is global seen by all, and other is seen by main only, when you cin>>x; in main the value entered will be in x which is at main scope. Now, when you call minus100(), that function doesnt see whatever declared in the main() scope; thus, it will look for another x (which will find it at the global) and will use it. Since your global x has no value (NULL), if(x>=50) will never be true since you are comparing nothing to 50 (nothing could be anything). To solve this: just remove x declared in main so that the input is entered into the same x that the function will look into.
Many thanks, I was really blind... and ty for not just coirrecting but also for the explanation, really helped a lot!
Topic archived. No new replies allowed.