msk100
asked on
Simple C++ code dosnt work for a noob
Recently decided to dive into C++ again.
Can anyone tell me why this code doesnt work? It is supposed to validate that the user has entered a number between 2 and 15 and if not, have them re-enter it. I'm not familiar with using ! expressions. As simple as this code may seem I assure you, this isnt someones homework. ;) I just decided to get back into it since i installed Gcc on my ubuntu. I compiled this using a comand line in ubuntu edgy.
me@mylinux:g++ myapp.cpp -o test
Simple code:
#include<iostream>
using namespace std;
int main()
{
int num;
num = 0;
do
{
cout << "Please enter a number between 2 and 15: \n";
cin >> "num;
}
while (!(num >=2)&&(num<=16);
cout << "Good job!! You entered " << num << "\n";
return 0;
}
If i typed this correctly it should produce a success no matter what number you enter.
also, if anyone knows a good C++ noob book that caters to gcc or G++, i would appreciate any reccomendations.
Thanks
Can anyone tell me why this code doesnt work? It is supposed to validate that the user has entered a number between 2 and 15 and if not, have them re-enter it. I'm not familiar with using ! expressions. As simple as this code may seem I assure you, this isnt someones homework. ;) I just decided to get back into it since i installed Gcc on my ubuntu. I compiled this using a comand line in ubuntu edgy.
me@mylinux:g++ myapp.cpp -o test
Simple code:
#include<iostream>
using namespace std;
int main()
{
int num;
num = 0;
do
{
cout << "Please enter a number between 2 and 15: \n";
cin >> "num;
}
while (!(num >=2)&&(num<=16);
cout << "Good job!! You entered " << num << "\n";
return 0;
}
If i typed this correctly it should produce a success no matter what number you enter.
also, if anyone knows a good C++ noob book that caters to gcc or G++, i would appreciate any reccomendations.
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you, works great now.
cin >> "num;
should be :
cin >> num;
there was an " too much ...
2)
while (!(num >=2)&&(num<=16);
the ! should be for the whole check, and not just (num >= 2). So, make that :
while (!((num >=2)&&(num<=16)));
Note that there was also a ) missing at the end ...
I can recommend this basic C++ tutorial which should get you started :
http://www.cplusplus.com/doc/tutorial/
Notice that on that same site you can also find a good reference of the standard libraries ...