I am doing homework for basic c++.
It seems to be not easy.
Now I am tring to complie my code about test interger.
Would you mind if you could correct my bad codes?
#include <iostream>
using namestd;
int main() {
int integer;
integer = 0
cout << "Enter an integer";
cin >> "0"
intinteger;
integer = 2n (|n| > 0);
cout << "Enter an integer";
if (integer = 2n)
integer = even number
else (integer = 2n)
integer = odd number
integer = even nubmer;
return 0;
}
I am used to access school's server by putty.exe and file in nano on the software.
Hmm... I don't really know a code that tests integer numbers... if it's odd or even... I guess I have seen some codes, but I don't really remember...
However, I fixed the errors you had in the codes.
the user types a number, and if it's not two, the program will show "odd number", otherwise, it will show "even number"... you might want to edit it...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
usingnamespace std;
int main() {
int integer = 0;
cout << "Enter an integer";
cin >> integer;
if (integer == 2)
cout << "even number";
else
cout << "Odd number";
return 0;
}
You made the mistake that I have made more times than I care to admit. Semicolons! Check your code again and add semicolons to the end of each line that needs it, and it will probably compile for you. Variable names cannot contain spaces either (ie, odd number and even number). You may also want to print your results to verify it works.
Thanks guys.
I'll remain my friend told me we shouldn't use two int as below int main() in this case. Is that true?
And I forgot semicolons for the end of each line. Thanks maingeek.
Also I'll use integer%2 with my mate's advisement about my coding.
I am totally beginner for my c++ class, so that I might ask again!
I believe that this is what you were trying to accomplish. It works for all even and odd numbers. I added comments to several of the lines to indicate their purpose.
#include <iostream>
usingnamespace std;
int main()
{
int integer;
integer = 0;
cout << "Enter an integer" << endl;
cin >> integer;
if (integer%2 == 1) //Checks to see if mod of integer variable is 1
cout << "Odd number"; //if so, it's odd
else
cout << "Even number"; //if not, it's even.
cout << endl << endl;
system("pause"); //added to cause the system to remain at the cmd line
//useable only for windows code
return 0;
}
if ( integer == 0 ) { // Did the user enter 0?
cout << "Zero"; // It was 0
return 0; // Exit now, we don't need to check odd/even anymore
}
if (integer%2 == 1) //Checks to see if mod of integer variable is 1