Thread: Runtime error cannot figure out

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

    Runtime error cannot figure out

    Hi everyone,

    Following is the C++ code that am stuck with at runtime:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std ;
    
    
    class gradebook
    
    {
          
          public:
                 
                 void displaymessage (string nameofcourse)
                 
                 {
                      
                      cout << "Welcome to the Gradebook for\n" << nameofcourse ;
                      
                 }
                 
    } ;
    
    
    int main ()
    
    {
        
        string nameofcourse ;
        
        cout << "Please enter the course name\n\n" ;
        
        getline(cin, nameofcourse) ;
        
        cin.get () ;     
        
        gradebook mygradebook ;
        
        mygradebook.displaymessage (nameofcourse) ;
        
        return 0 ;
        
    }
    When I compile(Dev C++ compiler) and run, "Please enter the course name" (without quotes) gets printed however the displaymessage function does NOT get printed. I've deliberately kept the parameter list name to nameofcourse at both places to avoid confusion.

    Please help
    Last edited by exus69; 04-20-2011 at 02:45 AM.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    remove the cin.get();

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    28
    Removing cin.get () does not help. In fact the screen vanishes if I type something as input and press enter.

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    ...ohh...remove it from that place and put it before "return 0;"

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Maybe the output would be easier to read if you wrote:
    Code:
    cout << "Welcome to the Gradebook for " << nameofcourse << "\n";
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by exus69 View Post
    Removing cin.get () does not help. In fact the screen vanishes if I type something as input and press enter.
    run your program from command line window
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

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

    Problem solved

    Quote Originally Posted by manasij7479 View Post
    ...ohh...remove it from that place and put it before "return 0;"
    That worked. Thanks alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error in runtime
    By sick in forum C Programming
    Replies: 7
    Last Post: 08-15-2009, 02:51 AM
  2. Runtime error
    By MarlonDean in forum C++ Programming
    Replies: 2
    Last Post: 07-02-2008, 02:28 AM
  3. compile time error or runtime error?
    By George2 in forum C# Programming
    Replies: 3
    Last Post: 05-07-2008, 07:08 AM
  4. I'm getting a runtime error
    By Ariod in forum C Programming
    Replies: 6
    Last Post: 02-12-2005, 04:23 PM
  5. Runtime error! Please help
    By Garfield in forum C Programming
    Replies: 7
    Last Post: 09-22-2001, 05:40 AM