Positive/Negative integers

Hi guys :) I am pretty much a beginner in C++ and came across the following problem.

Write a C++ program that asks its user to enter any positive or integer number. Your program should display a message indicating if the number is positive or negative, and if it is a five-digit integer or not. If the number entered is Zero, then a message indicating that should be displayed.

My code so far (not fully conpleted) is as follows:


#include <iostream>

using namespace std;

int main()
{
int x;

cout << "PLease enter an integer value \n" << x << endl;

if
(x > 9999)

cout << "The value you have entered is positive and a five digit number \n" << endl;

else

cout << "The value you have entered is negative and a five digit number \n" << endl;

return 0;
}


It keeps giving me the following errors

2010\Projects\Question1\Debug\Question1.exe', Symbols loaded.
'Question1.exe': Loaded 'C:\Windows\System32\ntdll.dll', Cannot find or open the PDB file
'Question1.exe': Loaded 'C:\Windows\System32\kernel32.dll', Cannot find or open the PDB file
'Question1.exe': Loaded 'C:\Windows\System32\KernelBase.dll', Cannot find or open the PDB file
'Question1.exe': Loaded 'C:\Windows\System32\msvcp100d.dll', Symbols loaded.
'Question1.exe': Loaded 'C:\Windows\System32\msvcr100d.dll', Symbols loaded.
Run-Time Check Failure #3 - The variable 'x' is being used without being initialized.
Run-Time Check Failure #3 - The variable 'x' is being used without being initialized.
The program '[7184] Question1.exe: Native' has exited with code 0 (0x0).




Any help would be greatly appreciated. This is for bonus points that I would like to get.

Thanks!
Last edited on
The code should compile fine. The "Run-Time Check Failure" is just saying you never give x a value.
When I try to compile it, it gives me the errors given above and does not give me a chance to even enter a value. I also just finished the rest of the code, would you mind taking a look at it please?


#include <iostream>

using namespace std;

int main()
{
int x;

cout << "PLease enter an integer value \n" << x << endl;

if
(x > 9999)

cout << "The value you have entered is positive and a five digit number \n" << endl;

else

cout << "The value you have entered is positive and a non-five digit number \n" << endl;

if
(x < 0)

cout << "The value you have entered is negative and is a non-five digit number \n" << endl;

if
(x > -9999)

cout << "The value you have entered is negative and is a five digit number \n" <<endl;
if
(x = 0)
cout << "The value you have entered is zero \n" << endl;

return 0;
}


Thank You.
It is not possible to enter a value because you have not added the code for that yet.

Also, x = 0 should be x == 0

To be honest I don't think the errors you get is because of your code. Are you sure they are errors and not just warnings?
When I debug it, it brings up a run time failure as well as a debugging error. I have tried to modify the code to the following :


#include <iostream>

using namespace std;

int main()
{
float integer;

cout << "PLease enter an integer value \n" << integer << endl;


if
(integer > 9999)

cout << "The value you have entered is positive and a five digit number \n" << integer << endl;


else
{
cout << "The value you have entered is positive and a non-five digit number \n" << integer << endl;

}

if
(integer < 0)

cout << "The value you have entered is negative and is a non-five digit number \n" << integer << endl;


if
(integer > -9999)

cout << "The value you have entered is negative and is a five digit number \n" << integer << endl;


if
(integer == 0)
cout << "The value you have entered is zero \n" << integer << endl;


return 0;
}


It is still giving me the error of a variable not being initialized.
Try adding integer=0; after float integer
gh24, the good news is that it ran without error. The bad news is that it gave me 0 for everything and didn't allow for the input of values. I feel like we are very close to breaking this.

I was just helping you get rid of the error. I can look into the code. One sec.
I added cin >> integer; after every if except for the first one. The code works, I just have to tweak it some. For instance, when I enter -19, it gives the output of "The integer you have entered is positive and a non five digit."

Thank You gh24 :)
That is because you have written the else if ladder incorrectly. After the if(x>9999> loop, put a condition else if(x>0)...
Last edited on
I think this works:

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>

using namespace std;

int main()
{
float integer;
integer=0;
cout << "PLease enter an integer value: ";
cin>>integer;


if
(integer > 9999)

cout << "The value you have entered is positive and a five digit number \n" << integer << endl;

else
	if(integer>0)

{
cout << "The value you have entered is positive and a non-five digit number \n" << integer << endl;

}

if 
(integer < 0 && integer >-10000)

cout << "The value you have entered is negative and is a non-five digit number \n" << integer << endl;


if 
(integer < -9999)

cout << "The value you have entered is negative and is a five digit number \n" << integer << endl;


if
(integer == 0)
cout << "The value you have entered is zero \n" << integer << endl;


return 0;
}


You also may want to add a check in there to be sure that the user has entered an integer. As you currently have the code it accepts any number at all.
Last edited on
Thank You! I was fixing around on the original code and it came out very similar to yours, with the exception of a few mistakes here and there. Mines is:


#include <iostream>

using namespace std;

int main()
{
float integer;
integer = 0;
cout << "PLease enter an integer value \n" << endl;
cin >> integer;
if
(integer != 0 && integer > 9999)

cout << "The value you have entered is positive and a five digit number \n" << endl;

else

cout << "The value you have entered is positive and a non-five digit number \n" << endl;

if
(integer < 0)

cout << "The value you have entered is negative and is a non-five digit number \n" << endl;
else ;

if
(integer < 0 && integer > -9999)

cout << "The value you have entered is negative and is a five digit number \n" << endl;

else ;

if
(integer == 0)
cout << "The value you have entered is zero \n" << endl;

else;


return 0;
}
ALthough I do have a question from the code you provided:

cout << "The value you have entered is positive and a five digit number \n" << integer << endl;

else
if(integer>0)

{
cout << "The value you have entered is positive and a non-five digit number \n" << integer << endl;

}

If I was to put integer = 10000, then wouldn't it give me both "The value you have entered is positive and a five digit number" AND "The value you have entered is positive and a non-five digit number" because in both cases, it is integer > 0 and integer > 9999.
No because of the else statement. After the first if is satisfied it does not proceed to the else statement.
Thank You :) I have been working on this for around 2 hours now and it is...2:10 AM and I still have two more codes to go!
Topic archived. No new replies allowed.