Hi

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.

Please reply, anyone.

yoshi
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>
using namespace std;

int main() {

int integer = 0;

cout << "Enter an integer";
cin >> integer;

if (integer == 2)
cout << "even number";
else 
cout << "Odd number";

return 0;
}
Last edited on
Yoshi,

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.

~maingeek
Hi cminusminus, maingeek,

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!

Sincerely,
Yoshi
Yoshi,

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.

~maingeek

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace 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;
}
maingeek,

Thanks your advisement.
I was wondering if I put zero as integer, it would be zero and even as same time.
How should I separate zero and even?

Thanks
Yoshi
Yoshi-san,

1
2
3
4
5
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 
Topic archived. No new replies allowed.