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.
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;
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 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."
#include <iostream>
usingnamespace 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;
elseif(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.
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;
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.