Prog Prob

This is a discussion on Prog Prob within the C++ Programming forums, part of the General Programming Boards category; hey. Im not sure how to adapt cin.get(); to stop my prog exiting here it is. If sum1 could point ...

  1. #1
    I'll take you down! polonyman's Avatar
    Join Date
    Sep 2004
    Posts
    50

    Post Prog Prob

    hey. Im not sure how to adapt cin.get(); to stop my prog exiting here it is. If sum1 could point me in the correct direction that would be cool
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
        string firstName;
        string lastName;
        int ID;
        int age;
        float salary;
        string occupation;
    
        cout << "Enter your first name ";
        cin >> firstName;
    
        cout << "Enter your last name ";
        cin >> lastName;
    
        cout << "Enter your ID number ";
        cin >> ID;
        
        cout << "Enter your age ";
        cin >> age;
        
        cout << "Enter your salary (Per Year) ";
        cin >> salary;
        
        cout << "Enter your occupation ";
        cin >> occupation;
    
        cout << "Hello " << firstName << " " << lastName << " " << age << " " << salary << " "<< occupation; 
        cout << " or should I say " << ID << end1;
    
        return 0;
    }
    thankz

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    #include <iostream>
    #include <limits>
    #include <string>
    using namespace std;
    int main()
    {
        string firstName;
        string lastName;
        int ID;
        int age;
        float salary;
        string occupation;
    
        cout << "Enter your first name ";
        cin >> firstName;
    
        cout << "Enter your last name ";
        cin >> lastName;
    
        cout << "Enter your ID number ";
        cin >> ID;
        
        cout << "Enter your age ";
        cin >> age;
        
        cout << "Enter your salary (Per Year) ";
        cin >> salary;
        
        cout << "Enter your occupation ";
        cin >> occupation;
    
        cout << "Hello " << firstName << " " << lastName << " " << age << " " << salary << " "<< occupation; 
        cout << " or should I say " << ID << endl;
    
        cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
        cin.get();
    
        return 0;
    }
    My best code is written with the delete key.

  3. #3
    I'll take you down! polonyman's Avatar
    Join Date
    Sep 2004
    Posts
    50

    ?

    ive seen cin.get(); bein used without that header or other thing and it was compliant. plz explain!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    That's because you #include <iostream> for cin.get(), but Prelude has something else to it as well, in bold.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Quote Originally Posted by Prelude
    Code:
    #include <iostream>
    #include <limits>
    #include <string>
    using namespace std;
    int main()
    {
        string firstName;
        string lastName;
        int ID;
        int age;
        float salary;
        string occupation;
    
        cout << "Enter your first name ";
        cin >> firstName;
    
        cout << "Enter your last name ";
        cin >> lastName;
    
        cout << "Enter your ID number ";
        cin >> ID;
        
        cout << "Enter your age ";
        cin >> age;
        
        cout << "Enter your salary (Per Year) ";
        cin >> salary;
        
        cout << "Enter your occupation ";
        cin >> occupation;
    
        cout << "Hello " << firstName << " " << lastName << " " << age << " " << salary << " "<< occupation; 
        cout << " or should I say " << ID << endl;
    
        cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
        cin.get();
    
        return 0;
    }
    Using cin.ignore(); works and you don't have to include <Limits>. So it would look like this:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
        string firstName;
        string lastName;
        int ID;
        int age;
        float salary;
        string occupation;
    
        cout << "Enter your first name ";
        cin >> firstName;
    
        cout << "Enter your last name ";
        cin >> lastName;
    
        cout << "Enter your ID number ";
        cin >> ID;
        
        cout << "Enter your age ";
        cin >> age;
        
        cout << "Enter your salary (Per Year) ";
        cin >> salary;
        
        cout << "Enter your occupation ";
        cin >> occupation;
    
        cout << "Hello " << firstName << " " << lastName << " " << age << " " << salary << " "<< occupation; 
        cout << " or should I say " << ID << endl;
    
        cin.ignore();
        cin.get();
    
        return 0;
    }

  6. #6
    I'll take you down! polonyman's Avatar
    Join Date
    Sep 2004
    Posts
    50

    cool

    u guys are hell smart

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote Originally Posted by stickman
    Using cin.ignore(); works and you don't have to include <Limits>. So it would look like this:
    There is a reason Prelude included <limits> and added the extra code to the ignore() call... it's better. The <limits> header is required only for that extra code (to get numerical_limits).

    cin.ignore(); only ignores one character in the stream.
    cin.ignore( numeric_limits<streamsize>::max(), '\n' ); ignores all characters in the stream up to and including the first newline.

    If you type "Programmer duh!" for your occupation, the first one will not work (it will ignore only the d in "duh!"), but the second one will work fine because it will ignore all of "duh!" and the newline after it.

    P.S. Prelude: what is streamsize? and why did you use it?

  8. #8
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Quote Originally Posted by jlou
    There is a reason Prelude included <limits> and added the extra code to the ignore() call... it's better. The <limits> header is required only for that extra code (to get numerical_limits).

    cin.ignore(); only ignores one character in the stream.
    cin.ignore( numeric_limits<streamsize>::max(), '\n' ); ignores all characters in the stream up to and including the first newline.

    If you type "Programmer duh!" for your occupation, the first one will not work (it will ignore only the d in "duh!"), but the second one will work fine because it will ignore all of "duh!" and the newline after it.

    P.S. Prelude: what is streamsize? and why did you use it?
    Yeah, but if you want the user to hit enter and then close, use cin.ignore();
    cin.get();

    The user can type whatever and when the user hits enter the program quits or whatever follows that line.

  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote Originally Posted by stickman
    Yeah, but if you want the user to hit enter and then close, use cin.ignore();
    cin.get();

    The user can type whatever and when the user hits enter the program quits or whatever follows that line.
    No, both are the same, except cin.ignore() doesn't work sometimes when the other one does.

    Just the simple ignore() of a single char only works if only a newline is in the stream. If there are other characters it won't do what you want.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >P.S. Prelude: what is streamsize? and why did you use it?
    streamsize is a signed integral type representing the size of I/O buffers. It's similar to the POSIX ssize_t in usage. I used it for portability and documentation reasons. The portability reason is that I simply don't know (or care) what the size of the I/O buffer is. The documentation reason is that streamsize better illustrates the intention of the statement than say, int or long.

    >Yeah, but if you want the user to hit enter and then close, use cin.ignore();
    >cin.get();
    This only works if there is only one character left in the stream for ignore to eat. Otherwise, get will read the next character and the user will not have a chance to hit enter. It's the classic scanf/gets combo in a different disguise.
    My best code is written with the delete key.

  11. #11
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I just wanted to say that I added cin.ignore ( numeric_limits<streamsize>::max(), '\n' ); in my own programs that use cin.get()... and it works wicked awesome.

    mucho bueno.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Deal or No Deal listbox prob
    By kryptkat in forum Windows Programming
    Replies: 5
    Last Post: 03-30-2009, 07:53 PM
  2. Bor to DevC++ prog convert prob
    By kryptkat in forum Windows Programming
    Replies: 16
    Last Post: 09-18-2007, 06:11 AM
  3. getch() prob
    By kryptkat in forum C Programming
    Replies: 29
    Last Post: 07-05-2007, 10:27 AM
  4. prob with my cimple prog
    By KidMan in forum C Programming
    Replies: 3
    Last Post: 05-06-2006, 03:50 AM
  5. if-else prob..very basic so sorry to trouble u guys
    By bugeye in forum C Programming
    Replies: 2
    Last Post: 01-26-2002, 08:55 AM

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