Thread: First C++ program unsuccessful:(

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    28

    First C++ program unsuccessful:(

    Hello everyone,

    Am writing my first C++ program and I've hit the first roadblock as well lol

    Heres the code:

    Code:
    #include <iostream>
    
    class student
    
    {
    
          public:
                 char studid[5] ;
                 char stuname[20] ;
                 int age ;
    
          void accept ()
    
          {
               cout << "Enter student ID" << endl ;
               cin >> studid >> endl ;
    
               cout << "Enter your name" << endl ;
               cin >> stuname >> endl ;
    
               cout << "Enter your age" << endl ;
               cin >> age >> endl ;
    
               }
    
          void display ()
    
          {
               cout << "The entered details are as follows:" << endl ;
               cout << "studid =" << studid << endl ;
    
               }
               };
    
    
          main ()
    
          {
              student stuobj ;
              stuobj.accept() ;
              stuobj.display() ;
    
              }
    When I compile and run it I get the following errors

    In member function 'void student::accept()':

    error : 'cout' undeclared (first use this function)

    error: 'endl' undeclared (first use this function)

    error : 'cin' undeclared (first use this function)

    In member function 'void student::display()':


    same errors like above.

    Please help

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Add "using namespace std;" after the "#include <iostream>"

    Hey, don't worry. My first 10 attempts where pure failures!
    Devoted my life to programming...

  3. #3
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    attempting classes with your first C++ program...impressive. try get and set functions instead of asking for user input within your class though

  4. #4
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    just to let you know..there is a possibility of a buffer overflow, when using char array..
    you should try the string object...if the book hasn't covered, it is simple.
    #include <string> //include the string file

    string studid will be your variable..yep just like that.

    And yes..impressive.
    You ended that sentence with a preposition...Bastard!

  5. #5
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Impressive first attempt. You still need "int main()"
    "All that we see or seem
    Is but a dream within a dream." - Poe

  6. #6
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by nimitzhunter View Post
    Impressive first attempt. You still need "int main()"
    He already has a main function. It just doesn't return anything.

    OP, that's pretty lengthy for someone's first program. Try including the string header and using string variables instead of char arrays.

  7. #7
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by Babkockdood View Post
    He already has a main function. It just doesn't return anything.
    what he had was not a function. Without returning anything, it has the same affect as implicitly returning 0. But that is still not a proper function. The only legal main() are:
    Code:
    int main()
    or
    int main( int argc, char* argv[] )
    Last edited by nimitzhunter; 02-10-2011 at 03:04 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  8. #8
    Registered User
    Join Date
    Jul 2010
    Posts
    28
    Impressive?? I dont know my C++ course starts with classes and objects
    and that exact program works fine on gpp compiler but its giving errors
    on Dev C++ and Code::Blocks compiler

    My tutor told me there needs to be a path given in the compilers that am
    using or maybe my compiler installation is wrong!!! do you think she's
    right??

    Thanks for all the help guys, this forum is too good

  9. #9
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Well what errors do you now get? assuming that you have made the suggeted changes to your code?

    if you get errors like this:
    ...undeclared (first use this function)
    With library functions then it means you are not including the required header, or if you get similar errors at the linking phase then you have not set your linking to the required libraries correctly.

    In code blocks you need to set the path for includes, and the path for library linkage via the project options menus, you also need to check if you have listed the correct libraries to link to in the build options, this is usually only for third party libraries as the OS stuff is usually added with the project creation.
    Of course you need to check that the compilers actual location is properly set in the global 'compiler / debugger' options.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  10. #10
    Registered User
    Join Date
    Jul 2010
    Posts
    28
    After adding "using namespace std ;" (excluding " ") below #include <iostream>
    am getting new errors as follows:

    error: no match for 'operator >>' in (&std::cin)->std::basic_istream<_CharT,_Traits>:: operator>> [ with _CharT = char, _Traits = std::char_Traits

    Please help
    Last edited by exus69; 02-12-2011 at 07:37 AM.

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You don't use endl on input streams, like you are here:

    Code:
    cin >> studid >> endl ;
    endl is for outputting a new line character and flushing the output buffer.

  12. #12
    Registered User
    Join Date
    Jul 2010
    Posts
    28

    Problem Solved

    Ok so finally my problem is solved and these are the two changes that
    I made to the existing code :

    1) Added using "namespace std ;" (excluding " ") below #include <iostream>

    2) Removed endl after cin


    Thanks everyone for their inputs. Highly appreciated

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM