Thread: Need Help!

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    South Africa
    Posts
    11

    Need Help!

    I am busy writing my first program. When I tried to test it gave 2 errors which I cant figure out what is wrong. Please help I am using Dev C++.

    PS. I am still busy with the tutorials so dont know much about C++

    Code:
    #include <iostream.h>
    #include <string.h>
    
    int question1( int age);
    int question2 ( char user_input,int age);
    
    int main()
    {
        char string[256];          //define variables
        char user_input[10];
        int age;
    
        cout<<"This is a question and answer game!\n";   //Question 1:
        cout<<"Question 1: How old are you? ";           //How old are you
        cin>>age;                                        //input
        question1(age);                                  //goto function q1
    
        cout<<"Question 2: Do you get pocket money?\n";
        cin.getline(user_input, 10, '\n');
        question2(user_input, age);                        //unknown error
    
        system ("pause");
    }
    
    int question1 (int age)
    {
        if (age > 13)                          //if age more then 13
        {
          cout<<"You are older then me!\n";   //output you are older then me on the screen
        }
        else
        {
         cout<<"You are younger then me!\n";  //otherwise say you are younger then me
        }
        return 0;                              //return 0
    
    int question2 ( char user_input, int age)
    {
       if (!strcmpi("no", user_input))                     //unknown error
       {
        if (age < 15)
        {
          cout<<"LOL you dont get pocket money\n";
        }
    
       }
       return 0;
    }
    }
    Last edited by really_bad_prog; 04-05-2006 at 06:23 AM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Question2 take a char pointer instread of a single char.
    Code:
    int question2 ( char *user_input,int age);

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    Code:
    #include <iostream.h>
    #include <string.h>
    
    int question1( int age);
    int question2 ( char *user_input,int age);
    
    int main()
    {
        char string[256];          //define variables
        char user_input[10];
        int age;
    
        cout<<"This is a question and answer game!\n";   //Question 1:
        cout<<"Question 1: How old are you? ";           //How old are you
        cin>>age;                                        //input
        question1(age);                                  //goto function q1
       cin.ignore(1);
        cout<<"Question 2: Do you get pocket money?\n";
        cin.getline(user_input, 10, '\n');
        question2(user_input, age);                        //unknown error
    
        
    }
    
    int question1 (int age)
    {
        if (age > 13)                          //if age more then 13
        {
          cout<<"You are older then me!\n";   //output you are older then me on the screen
        }
        else
        {
         cout<<"You are younger then me!\n";  //otherwise say you are younger then me
        }
        return 0;                              //return 0
    }
    int question2 ( char *user_input, int age)
    {
       if (!strcmpi("no", user_input))                     //no error now
       {
        if (age < 15)
        {
          cout<<"LOL you dont get pocket money\n";
        }
    
       }
       return 0;
    }
    this works now..

    changes made:
    >int question2 ( char *user_input,int age);
    >cin.ignore(1);

    and dont define functions inside functions.

  4. #4
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Avoid the use of system() functions
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  5. #5
    Registered User
    Join Date
    Mar 2006
    Location
    South Africa
    Posts
    11
    Thx. Why musnt i use system() functions?

  6. #6
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  7. #7
    Registered User
    Join Date
    Mar 2006
    Location
    South Africa
    Posts
    11
    :sigh: another error it says "Parse error at end of input line 68" there is only 67 lines?

    Code:
    int question2 ( char *user_input, int age)
    {
      if (!strcmpi("no", user_input))
       {
        if (age > 15)
        {
          cout<<"LOL you dont get pocket money and you are"<<age<<"\n";
        }
        else
        {
          cout<<"If I were you I'd beg your parents for money\n";
        }
       }
        else
       {
        if (age > 12)
       {
        cout<<"You are lucky you have money in your pocket so DONT waste it!\n";
       }
       else
       {
        cout<<"When I was your age I didnt get pocket money\n";
       }
    
       return 0;
    }

  8. #8
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    you forgot a '}' for

    Code:
    if (!strcmpi("no", user_input))
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> #include <iostream.h>

    That is an outdated and non-standard header file. You should be using the new, standard headers. In this case, that would be <iostream>. The new headers put names into something called a namespace, so you'll have to handle just that small change. I prefer to add std:: in front of cout, cin, endl, and anything else I use from the standard library, but the simplest solution is to just put the line "using namespace std;" without the quotes under the #include lines.

    Also, since you are just learning C++, you should be learning C++ instead of C style C++. That means learning the C++ string class instead of character arrays. Consider finding newer tutorials that teach modern C++.

  10. #10
    Registered User
    Join Date
    Mar 2006
    Location
    South Africa
    Posts
    11
    Do you know where I can find these "Modern" tutorials.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Honestly, no, but I've never really looked. Basically, look for tutorials that have <iostream> instead of <iostream.h>, and that teach the C++ string class. I personally would use a book like Accelerated C++. You should at least find a tutorial that teaches <iostream>. Tutorials that teach the string class are unfortunately not all that common, since the C++ standard hasn't been around for that long. The tutorials on this site might do it. You can also search the forum to see other people asking for good tutorials.

    The string class is actually pretty easy. It acts like a built-in type like int or double. You can find information about it at a reference site like cppreference.com.

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Tutorials, right under your nose. Oh, man!

  13. #13
    Registered User
    Join Date
    Mar 2006
    Location
    South Africa
    Posts
    11
    I am learning C++ from those tutorials. My compiler cant use <iostream> only <iostream.h> i have dev C++

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Did you get the 4.0 version or the latest 4.9x version?

    Code:
    #include <iostream>
    using namespace std;
    int main ( ) {
      cout << "hello world" << endl;
    }
    gcc (the compiler dev-c++ uses) has known about this for a good while now, I'd be surprised if it refused to accept this example.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Registered User
    Join Date
    Mar 2006
    Location
    South Africa
    Posts
    11
    I think I am using V4.0. I dont like using beta versions. I'll try it anyway

Popular pages Recent additions subscribe to a feed