Help Please!

This is a discussion on Help Please! within the C++ Programming forums, part of the General Programming Boards category; Im learning c++ and im having problems with functions! The line marked with // THIS LINE IS PROBLEM! has the ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    5

    Help Please!

    Im learning c++ and im having problems with functions!

    The line marked with // THIS LINE IS PROBLEM! has the following errors (please help me fix) :

    Code:
    1>d:\users\ant1jr\documents\visual studio 2005\projects\test 1\test 1\test 1.cpp(18) : error C2144: syntax error : 'int' should be preceded by ')'
    1>d:\users\ant1jr\documents\visual studio 2005\projects\test 1\test 1\test 1.cpp(18) : error C2660: 'favnumber' : function does not take 0 arguments
    1>d:\users\ant1jr\documents\visual studio 2005\projects\test 1\test 1\test 1.cpp(18) : error C2059: syntax error : ')'
    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    int favnumber(int number);
    
    
    int main()
    {
        int input;
    
        cout<<"Welcome to Ant1jr's Test Software!\n";
        cout<<"1. Favorite Number \n";
            cout<<"Input: ";
        cin>> input;
        switch ( input ) {
            case 1:    
                favnumber( int number ); // THIS LINE IS PROBLEM!
                break;
        }
    }
    
    int favnumber(int number)
    {
        //The computer asks your fav number
        cout<<"Hello, what's your favorite number? \n";
        //You input your fav number
        cin>> number;
        cin.ignore();
        //The computer responds with "Your favorite number is <number>!"
        cout<<"Your favorite number is "<< number <<"! \n";
        cin.get();
        // If your favorite number is 2 you are identified as grandma.
        if ( number == 2 ) {
            cout<<"Hello Grandma! \n";
            // Press enter and you are told "Bye Grandma!"
            cin.get();
            cout<<"Bye Grandma!";
        }
        else if ( number == 1337 ) {
            cout<<"Hello Dad! \n";
            // Press enter and you are told "Bye Dad!"
            cin.get();
            cout<<"Bye Dad!";
        }
        else if ( number == 42 ) {
            cout<<"Hello Milo! \n";
            // Press enter and you are told "Bye Milo!"
            cin.get();
            cout<<"Bye Milo!";
        }
        // If you input 666 it will contantly print "UNHOLY DEMON!" until you close the application.
        else if ( number == 666 ) {
            for ( int x = 0; x >= 0; x++ ) {
                cout<<"UNHOLY DEMON! ";
            }
        }
        // If you dont input 2, 1337, 42, or 666 you are told your number is low if <50 or high if >50"
        else {
            if ( number <= 50 ) {
                cout<<"You have a low favorite number! \n";
                // Press enter and you are told "Bye!"
                cin.get();
                cout<<"Bye!";
            }
            else {
                cout<<"You have a high favorite number! \n";
                cin.get();
                // Press enter and you are told "Bye!"
                cout<<"Bye!";
            }
            
        }
        //Press enter to close.
        cin.get();
    }

  2. #2
    Amazingly beautiful user.
    Join Date
    Jul 2005
    Location
    If you knew I'd have to kill you
    Posts
    254
    Take a closer look at the functions tutorial. When you call a function, you don't specify the datatypes (because the variables you are using already do).
    The functions tutorial

    Here's an example.
    Code:
    void hello(int x) { //Do something }
    int main()
    {
      int bob = 10;
      hello(bob); //CORRECT
      hello(int bob); /*INCORRECT -- The compiler already knows that "bob" is an int, and when 
                                 you do that, it thinks you are trying to create a new variable called bob                            
                                 (which is invalid inside of a function call like that).*/
    
    }
    Hope that all helps.
    Programming Your Mom. http://www.dandongs.com/

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    5
    I tried removing int from the problem line but it still doesnt work! I don't understand...

  4. #4
    Amazingly beautiful user.
    Join Date
    Jul 2005
    Location
    If you knew I'd have to kill you
    Posts
    254
    Also, you need to tell it what variable you are passing.
    Notice how you have
    Code:
    int input;
    cin >> input;
    fav_number (number); //Incorrect, although the parameter for fav_number is "number," that is irrelevent.
    //  You need to pass it a local variable.  You probably want "fav_number(input);"
    Last edited by CrazyNorman; 07-27-2007 at 03:12 PM. Reason: Line breaks
    Programming Your Mom. http://www.dandongs.com/

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    5
    When I change it to input it says

    'favnumber' : must return a value

  6. #6
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    If you don't want the function to return a value then maybe declare it as void.

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    5
    woot thx that worked

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21