-
Need help
Hi!. Well I'm just starting C and I had a few questions:
1- I created a "program" that multiplies a number with 10(I know that is easy but I just started 3 days ago) and I have a "problem", the person writes the number and the program does the calculation good and then I press ENTER( I have cin.get() ) and the program quits. How can I tell the program to keep asking the person what number he wants to multiply instead of quiting the program?
2- I want to create a program that tells the person if the number is pair or not. A pair number is a number that when is divided by 2 it does has decimal numbers. How can I tell the program if a number has decimal numbers or not?
Thank you,
C-+
-
cin is C++. Do you want C or C++?
For your first question, read on loops.
For your second question, the modulus operator (%) will do the job.
-
Thank Elysia. But can you be more specific with the %?
-
% returns rest, so 2 % 2 == 0 3 % 2 == 1. 1000 % 2 == 0.
You still didn't answer if you're looking for C or C++?
-
Thank Elysia. I am looking for C++
-
You should probably post in the C++ section next time then. Keep that in mind.
-
I am having problems. Heres my code
Code:
//UPair
#include <iostream>
using namespace std;
int main()
{
int num;
cout<<"Welcome to UPair. This program tell you if a number is pair or unpair. Put the number here: \n";
cin>>num;
if ( num == 1. {
cout<<"The number is unpair\n";
}
else if ( num == 0 {
cout<<"The number is pair\n";
return 0
}
Here are my problems:
In function `int main()':
expected `)' before '{' token
UPair.cpp expected primary-expression at end of input
UPair.cpp expected `}' at end of input
UPair.cpp expected `;' at end of input
Dev-Cpp\Makefile.win [Build Error] [UPair.o] Error 1
I know I am doing it wrong
Thank you,
C-+
-
Does this line look OK to you?
In particular, do you think that you should start a brace within a parenthesis? If we combine that with the error message expected `)' before '{' token , perhaps that will lead you to the right conclusion?
--
Mats
-
I compiled it but when I put a number it closes. Heres the code:
Code:
//UnPair
#include <iostream>
using namespace std;
int main()
{
int num;
cout<<"Welcome to UPair. This program tell you if a number is pair or unpair. Put the number here: \n";
cin>>num;
if ( num == 1.);{
cout<<"The number is unpair\n";
}
if(num == 0);{
cout<<"The number is pair\n";
}
return 0;
}
-
-
matsp, instead of sending me to FAQ section could you help me please?
Heres the code:
Code:
//UnPair
#include <iostream>
using namespace std;
int main()
{
int num;
cout<<"Welcome to UPair. This program tell you if a number is pair or unpair. Put the number here: \n";
cin>>num;
cin.get();
if ( num%2 == 1.);{
cout<<"The number is unpair\n";
}
else if(num%2==1);{
cout<<"The number is pair\n";
}
cin.get();
return 0;
}
I have this errors:
Dev-Cpp\Proyectos\UnPair\UnPair.cpp expected primary-expression before "else"
Dev-Cpp\Proyectos\UnPair\UnPair.cpp expected `;' before "else"
Than you,
C-+
-
Don't put a semicolon after the condition. What is the period in the first if condition for?
-
Deleted the semicolons and compiled but when I put a number it doesn't say anything. The period is for decimal numbers
-
Use float or double for decimal numbers, like 7.77 or 1.0003.
-
It works, The problem was the second if. It is 0 not 1.
Thank for your help guys, and DarkAlex, are you really DAX?
-
No. I have no idea who DAX is.
-
-
Quote:
Originally Posted by
C-+
Code:
if ( num%2 == 1.);{
cout<<"The number is unpair\n";
}
The "." is only valid for objects, since it's an operator. You're comparing numbers, so it's invalid. Remove it.
IFs do not use a semicolon ( ; ) after it. If you put it there, it the same as removing the entire if because you're telling the compiler to do the IF but nothing to do IF the IF is true. Thus it will optimize it away.
Quote:
Code:
else if(num%2==1);{
cout<<"The number is pair\n";
}
Should be else if (num % 2 == 0) since you've already checked for 1.
-
-
num is an integer.
And 1. is silly and confusing, make it 1.0 or 1.0f or better yet, just 1, since num is an integer and % returns an integer.