Thread: Need help

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    12

    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-+

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    cin is C++. Do you want C or C++?
    For your first question, read on loops.
    For your second question, the modulus operator (&#37 will do the job.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    12
    Thank Elysia. But can you be more specific with the %?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    % 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++?

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    12
    Thank Elysia. I am looking for C++

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    You should probably post in the C++ section next time then. Keep that in mind.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    12
    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-+
    Last edited by C-+; 12-02-2007 at 04:03 PM.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Does this line look OK to you?

    Code:
        if ( num == 1. {
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    12
    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;
        
    }

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Look in the FAQ.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    12
    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-+

  12. #12
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    Don't put a semicolon after the condition. What is the period in the first if condition for?

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    12
    Deleted the semicolons and compiled but when I put a number it doesn't say anything. The period is for decimal numbers

  14. #14
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    Use float or double for decimal numbers, like 7.77 or 1.0003.

  15. #15
    Registered User
    Join Date
    Dec 2007
    Posts
    12
    It works, The problem was the second if. It is 0 not 1.

    Thank for your help guys, and DarkAlex, are you really DAX?

Popular pages Recent additions subscribe to a feed