Simple C++ help for a beginer?

This is a discussion on Simple C++ help for a beginer? within the C++ Programming forums, part of the General Programming Boards category; well i recently took up C++, and have had a hell of a time with it. I was hoping i ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    13

    Simple C++ help for a beginer?

    well i recently took up C++, and have had a hell of a time with it. I was hoping i could get some help here if possible.

    first things first. i have had a pain in the ass time trying to keep a window open that uses user input. i know for a program that dosnt get input from a user you can just use cin.get();. however if the program gets user input than the program will just pass right throught the cin.get();. so what else may i use for this purpose?

  2. #2
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    If you just used cin, say like this,
    Code:
    cin>>input;
    and you want to use cin.get(), you need to put two in a row.
    Code:
    cin>>input;
    //.........
    
    cin.get();
    cin.get();
    Or, you can use another function (cin.ignore()), and do this:
    Code:
    cin>>input;
    //.........
    
    cin.ignore();
    cin.get();

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    cin and cout are popular options. getline( ) also works if you want to accept an entire line of user input (including blank spaces)


    Code:
    include<iostream>
    include<string>
    using namespace std;
    
    int main()
    {
         string name;
    
         cout << "Please enter your name: ";
    
         cin >> name;
    
         cout << "\n\nHello " << name << ", how are you doing today?";
    
         return 0;
    
    }
    • "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

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You can clear out the garbage from cin like this:
    Code:
    #include <iostream>
    #include <limits>
    
    using namespace std;
    
    int main()
    {
      // The rest of your program here
    
      cin.ignore(numeric_limits<streamsize>::max(), '\n');
      cin.get();
    }
    How it works doesn't really matter at this point, so just be sure to include <limits> and copy what I wrote and your problem will go away until you discover better methods of taking input.
    My best code is written with the delete key.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Prelude, what is your opinion on using cin.sync()?
    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

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Prelude, what is your opinion on using cin.sync()?
    Don't. The language in the standard is ambiguous enough to make cin.sync() non-portable for clearing the standard input stream.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    13
    1) ok, so if i put cin.ignore(); before the last cin.get, it will keep the window open? EX:

    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    int main()    {
        string name;
        int age;
    
        cout << "what is your name?\n";
        cin >> name;
    
        cout << "How old are you?\n";
        cin >> age;
    
        cout << "So " << name << ", your " << age << " years old eh?";
    
        cin.ignore();
        return 0;
    }
    2) getline(); does that require teh stdio.h header?

    ill give these methods a shot and see how it goes

  8. #8
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Is there a valid use for sync()?
    Why is it even in the standard?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  9. #9
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Here is a good tutorial on using getline( )


    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    int main()
    {
         string sentence;
    
         cout << "Enter a sentence: ";
    
         getline(cin, sentence);
    
         cout << "\n\nYour sentence has " << sentence.length() << " characters.";
    
         return 0;
    }
    • "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

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >so if i put cin.ignore(); before the last cin.get, it will keep the window open?
    Only if there's one character left in the stream because cin.ignore() only discards one character. Use my suggestion, it clears out the stream no matter how many characters are in it so that the next call to cin.get() will cause a blocking read as you would expect.

    >getline(); does that require teh stdio.h header?
    It depends which overload you want. If you want the getline that's a member function of istream, you include <iostream> for cin, or include <istream> and make your own object. If you want the getline for the std::string class, you include <string>.

    >Is there a valid use for sync()?
    Why it's a member of basic_istream is a question I've asked myself many a time. Probably to give Dr. Stroustrup a sneaky way out of his (also ambiguous, but even more misleading) statements in section 21.6.2 of TC++PL.

    >Why is it even in the standard?
    Supposedly to enhance flexibility when extending basic_ostream and streambuf's. ostream::flush() calls rdbuf->pubsync(), which calls rdbuf()->sync(), which is defined enough to do what we would expect of fflush(stdout).
    My best code is written with the delete key.

  11. #11
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Stroustrop clearly states in his book (21.6.2 3rd ed.) "flushing an istream is done using sync()". The standard though doesnt seem to take this view. I certainly wouldn't use it. Im not even sure of its purpose!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Stroustrop clearly states in his book (21.6.2 3rd ed.) "flushing an istream is done using sync()".
    Yet he neglected to specify what he meant by "flushing an istream". Sure, the most likely interpretation is that all characters in the buffer are discarded, but that's what ignore does, in a more general way. At best, with that interpretation, sync is redundant for istream, at worst, it's non-portable for any use.

    >Im not even sure of its purpose!
    The purpose of sync() is to synchronize the streambuf with the external device. For ostream that means writing any unwritten characters to the device and resetting the internal buffer pointers such that pptr() == pbase() (not necessarily, but most likely for a realistic implementation). One could argue that the same thing is done for istream where eback() and gptr() are synchronized such that eback() == gptr(), but that raises interesting and non-portable issues where the external device doesn't have the requisite behavior (I doubt that's what the standards committee wanted). It's more likely that synchronizing the streambuf with the external device for an istream means that (for example,) if working with a filtering streambuf, a file stream, a stream using shared memory, or some other external device that could be updated outside of the control of the streambuf, such as in a multithreaded or multiprocess environment, the streambuf is updated to reflect those changes.

    The key element to understanding that interpretation is that calls to gcount() are uneffected by calls to sync(). Therefore, it's logically impossible for sync() to extract characters since any unformatted input operation that extracts characters must update gcount() or risk a loss of integrity of the streambuf.
    My best code is written with the delete key.

  13. #13
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    exactly. I know the semantics of it but as you said its logically impossible for sync to extract characters because gcount is not updated. So what does it do? I read it also as a kind of fflush(stdout) yet am lost as to why its an istream member. there is no sync in ostream only flush.
    I rarely use iostreams anyway so it really doesn't affect me but this is an interesting topic.

    Prelude do you have a copy of langers iostreams book? What does she say about sync?
    Last edited by Stoned_Coder; 06-01-2005 at 11:42 AM.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Prelude do you have a copy of langers iostreams book?
    I do, but not with me right now. I'll try to remember to look it up when I get home tonight.

    >yet am lost as to why its an istream member
    In summary, if it does anything (because the external image may not have changed), it makes the internal buffer match the view of the external device.

    >there is no sync in ostream only flush.
    There's no need for sync in ostream because ostream::flush() calls rdbuf()->pubsync(), which is more or less identical to what istream::sync() does.

    >but this is an interesting topic
    Indeed, it is.
    My best code is written with the delete key.

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >What does she say about sync?
    They say the behavior of istream::sync() is not well defined and depends on the type of stream in question. Not immensely helpful; I like my interpretation better.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 10:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 07:52 AM

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