C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-31-2005, 09:51 AM   #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?
Insenic is offline   Reply With Quote
Old 05-31-2005, 09:54 AM   #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();
homeyg is offline   Reply With Quote
Old 05-31-2005, 09:54 AM   #3
VA National Guard
 
The Brain's Avatar
 
Join Date: May 2004
Location: Manassas, VA USA
Posts: 902
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
The Brain is offline   Reply With Quote
Old 05-31-2005, 09:59 AM   #4
Code Goddess
 
Prelude's Avatar
 
Join Date: Sep 2001
Posts: 9,661
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.
Prelude is offline   Reply With Quote
Old 05-31-2005, 10:17 AM   #5
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,365
Prelude, what is your opinion on using cin.sync()?
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 05-31-2005, 10:34 AM   #6
Code Goddess
 
Prelude's Avatar
 
Join Date: Sep 2001
Posts: 9,661
>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.
Prelude is offline   Reply With Quote
Old 05-31-2005, 02:08 PM   #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
Insenic is offline   Reply With Quote
Old 05-31-2005, 02:39 PM   #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
Stoned_Coder is offline   Reply With Quote
Old 05-31-2005, 02:41 PM   #9
VA National Guard
 
The Brain's Avatar
 
Join Date: May 2004
Location: Manassas, VA USA
Posts: 902
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
The Brain is offline   Reply With Quote
Old 05-31-2005, 04:25 PM   #10
Code Goddess
 
Prelude's Avatar
 
Join Date: Sep 2001
Posts: 9,661
>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.
Prelude is offline   Reply With Quote
Old 05-31-2005, 08:44 PM   #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
Stoned_Coder is offline   Reply With Quote
Old 06-01-2005, 07:55 AM   #12
Code Goddess
 
Prelude's Avatar
 
Join Date: Sep 2001
Posts: 9,661
>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.
Prelude is offline   Reply With Quote
Old 06-01-2005, 11:38 AM   #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?
__________________
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

Last edited by Stoned_Coder; 06-01-2005 at 11:42 AM.
Stoned_Coder is offline   Reply With Quote
Old 06-01-2005, 12:50 PM   #14
Code Goddess
 
Prelude's Avatar
 
Join Date: Sep 2001
Posts: 9,661
>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.
Prelude is offline   Reply With Quote
Old 06-01-2005, 04:46 PM   #15
Code Goddess
 
Prelude's Avatar
 
Join Date: Sep 2001
Posts: 9,661
>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.
Prelude is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
creating very simple text editor using c if13121 C Programming 6 11-25-2004 09:49 AM
Simple message encryption Vicious C++ Programming 10 11-07-2004 11:48 PM
Binary Search Trees Part III Prelude A Brief History of Cprogramming.com 16 10-02-2004 03:00 PM
Simple simple program Ryback C++ Programming 10 09-09-2004 05:48 AM
Need help with simple DAQ program canada-paul C++ Programming 12 03-15-2002 08:52 AM


All times are GMT -6. The time now is 07:22 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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