473,692 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

iostream and getline

Hello,

I just want to read a whole line from a console input. What I don't
understand is that I always have to press ENTER twice in order to see
the line I've entered.

Here's my code :

#include <string>
#include <iostream>

int main(void)
{
string input = "";
while ( input != "exit")
{
cout <<"\n$"; // show prompt for operator input
getline(cin, input, '\n');
cout << input;
}

cout << "\nBye";
return 0;
}
After starting the program the console output is as follows

$hello world <ENTER>
<ENTER>
hello world
$hello world again <ENTER>

$ <ENTER>
hello world again
$
What I want is entering the line, pressing ENTER and having it again
as an output to my console. Can anyone explain why this doesn't work ?
I've played around with printf and scanf which had the same effect.

I use Windows NT4 Sp6a and Visual C++ 6

thanks
Erich
Jul 22 '05 #1
19 3837
"er************ @gmx.net" wrote:

Hello,

I just want to read a whole line from a console input. What I don't
understand is that I always have to press ENTER twice in order to see
the line I've entered.


Any chance you are using VC++ 6.0

This is a documented big in their implementation.
Go to http://www.dinkumware.com/vc_fixes.html
and apply the patches.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #2

"Karl Heinz Buchegger" <kb******@gasca d.at> wrote in message
news:40******** *******@gascad. at...
"er************ @gmx.net" wrote:

Hello,

I just want to read a whole line from a console input. What I don't
understand is that I always have to press ENTER twice in order to see
the line I've entered.


Any chance you are using VC++ 6.0


Yes, he is using VC++ 6.0 (He mentions in the last line of his post) :-)
Verified..the code works just fine on VC++ 7.0.

Best wishes,
Sharad
Jul 22 '05 #3
er************@ gmx.net wrote:
Hello,

I just want to read a whole line from a console input. What I don't
understand is that I always have to press ENTER twice in order to see
the line I've entered.

Here's my code :

#include <string>
#include <iostream>

int main(void)
{
string input = "";
while ( input != "exit")
{
cout <<"\n$"; // show prompt for operator input
getline(cin, input, '\n');
cout << input;
}

cout << "\nBye";
return 0;
}
After starting the program the console output is as follows

$hello world <ENTER>
<ENTER>
hello world
$hello world again <ENTER>

$ <ENTER>
hello world again
$
What I want is entering the line, pressing ENTER and having it again
as an output to my console. Can anyone explain why this doesn't work ?
I've played around with printf and scanf which had the same effect.

I use Windows NT4 Sp6a and Visual C++ 6

thanks
Erich

Sounds like:

1) Your compiler blows.
2) Your program is getting confused by carriage returns.

Please try this, and say whether it works as you expect:

#include <string>
#include <iomanip>
#include <iostream>

int main(void)
{
std::string input;

std::cout << "$ " << std::flush; // Prompt.

while( getline( std::cin, input ) and input != "exit" )
{
std::cout << input;
std::cout << "\n$ " << std::flush;
}

std::cout << "\nBye\n";
}
Jul 22 '05 #4
I'm not familiar with string data type in C++ (unless its own implemented :)), but this should work fine:

# include <iostream.h>
# include <string.h>

void main()
{
char input[256] = "";
while(strcmp(in put,"exit")){
cout <<"\n$"; // show prompt for operator input
cin>>input;
cout<<input<<en dl;
}
cout<<"\nBye";
}

er************@ gmx.net wrote:
Hello,

I just want to read a whole line from a console input. What I don't
understand is that I always have to press ENTER twice in order to see
the line I've entered.

Here's my code :

#include <string>
#include <iostream>

int main(void)
{
string input = "";
while ( input != "exit")
{
cout <<"\n$"; // show prompt for operator input
getline(cin, input, '\n');
cout << input;
}

cout << "\nBye";
return 0;
}
After starting the program the console output is as follows

$hello world <ENTER>
<ENTER>
hello world
$hello world again <ENTER>

$ <ENTER>
hello world again
$
What I want is entering the line, pressing ENTER and having it again
as an output to my console. Can anyone explain why this doesn't work ?
I've played around with printf and scanf which had the same effect.

I use Windows NT4 Sp6a and Visual C++ 6

thanks
Erich


Jul 22 '05 #5
It iz I, Karl Heinz Buchegger, I will zay zis only onze:
This is a documented big in their implementation.


Sorry, couldn't resist. :)

M4

Jul 22 '05 #6
"/dev/nu**@localhost. ld" wrote:

I'm not familiar with string data type in C++ (unless its own implemented :)), but this should work fine:


It's just a documented bug in his compiler system.
Once the patch is applied it will work fine.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #7

"/dev/nu**@localhost. ld" <"/dev/nu**@localhost. ld"> wrote in message
news:bu******** **@atlantis.new s.tpi.pl...
I'm not familiar with string data type in C++ (unless its own implemented :)), but this should work fine:
# include <iostream.h>
# include <string.h>

void main()
{
char input[256] = "";
while(strcmp(in put,"exit")){
cout <<"\n$"; // show prompt for operator input
cin>>input;
cout<<input<<en dl;
}
cout<<"\nBye";
}


Looks like C++ code from a bygone era. :-)

Sumit.
Jul 22 '05 #8
Sumit Rajan wrote:
Looks like C++ code from a bygone era. :-)

:'(

Ok lets try something better:
-------------------------CMyString.h-----------------------------
# if !defined __CMYSTRING_H
# define __CMYSTRING_H

#include <windows.h>

Class CMyString
{
int size;
LPCTSTR string;
public:
CString()
{
size=0;
string=NULL;
}
~CMyString()
{
if(string)
delete[] string;
}
CMyString& operator=(const CMyString&);
CMyString& operator=(const LPSTR);
BOOL operator==(cons t CMyString&);
BOOL operator==(cons t LPSTR);
BOOL operator!=(cons t CMyString&);
BOOL operator!=(cons t LPSTR);
friend CMyString operator+(const CMyString&, const CMyString&);
friend CMyString operator+(const CMyString&, const LPSTR);
friend CMyString operator+(const LPSTR, const CMyString&);
friend CMyString operator+(const LPSTR, const LPSTR);
friend ostream& operator<<(ostr eam&, const CMyString&);
friend istream& operator>>(ifst ream&, CMyString&);
};

# endif //__CMYSTRING_H
-------------------------------------------------------------------------------

-----------------------------------------main.h--------------------------------
# include <iostream.h>
# include "CMyString. h"
void main()
{
CMyString string="";
while(string!=" exit"){
cout <<"\n$"; // show prompt for operator input
cin>>string;
cout<<string<<e ndl;
}
cout<<"\nBye";
}
--------------------------------------------------------------------------------

Better now? ;)
Now one need to write CMyString.cpp :)

Jul 22 '05 #9
/dev/nu**@localhost. ld wrote:
Sumit Rajan wrote:
Looks like C++ code from a bygone era. :-)


:'(

Ok lets try something better:
-------------------------CMyString.h-----------------------------
# if !defined __CMYSTRING_H
# define __CMYSTRING_H

#include <windows.h>

Class CMyString
{
int size;
LPCTSTR string;
public:
CString()
{ size=0;
string=NULL;
}
~CMyString()
{
if(string)
delete[] string;
}
CMyString& operator=(const CMyString&);
CMyString& operator=(const LPSTR);
BOOL operator==(cons t CMyString&);
BOOL operator==(cons t LPSTR);
BOOL operator!=(cons t CMyString&);
BOOL operator!=(cons t LPSTR);
friend CMyString operator+(const CMyString&, const CMyString&);
friend CMyString operator+(const CMyString&, const LPSTR);
friend CMyString operator+(const LPSTR, const CMyString&);
friend CMyString operator+(const LPSTR, const LPSTR);
friend ostream& operator<<(ostr eam&, const CMyString&);
friend istream& operator>>(ifst ream&, CMyString&);
};

# endif //__CMYSTRING_H
-------------------------------------------------------------------------------
-----------------------------------------main.h--------------------------------

# include <iostream.h>
# include "CMyString. h"
void main()
{
CMyString string="";
while(string!=" exit"){
cout <<"\n$"; // show prompt for operator input
cin>>string;
cout<<string<<e ndl;
}
cout<<"\nBye";
}
--------------------------------------------------------------------------------
Better now? ;)
Now one need to write CMyString.cpp :)


Um... How is this better than using std::string?

Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
6018
by: David Rubin | last post by:
I get an error when I compile the following code: #include <algorithm> #include <cctype> #include <iostream> #include <string> using namespace std; string&
3
2665
by: Jennica Humphrey | last post by:
Let's say I have something like this: ifstream inFile( buffer, ios::in ); while ( inFile >> name >> value ) { // read lines cout << "Name: " << name << " Value: " << value; } The file reads one word at a time, which appears to be delineated by whitespace or a newline. How do I change the delineator, for example,
10
1957
by: Christopher | last post by:
I am trying to get a line from the user which could be anything, I am expecting either a string followed by a whitespace or just a string by itself. I have tryed using string::getline, stringstreams, and all sorts of cin variations and can't get it right. Currently my program stops, waiting for more input in the case the user just enters a command with no argument, in this block of code. How can I fix it up? void Menu() { string ...
20
5521
by: Mark | last post by:
I am using gnu g++ version 3.3.2, trying a simple test to read in and then write out a large (100,000 line) text file ########################################## CSTDIO VERSION TO READ/WRITE TEXT FILE: #include <cstdlib> #include <cstdio> using namespace std;
3
4613
by: Chad E. Dollins | last post by:
I've got some trick input that I need help reading. I am using iostream because it is still a holiday weekend and I'm not trying to stress, but I have some input that looks like this. 2 1453 2123 3342
5
2340
by: wesley | last post by:
Hi All. i am new to C++. I know that in order to use cout, i need to include <iostream>. I have done this, but i still get errors when i try to output to the screen. Is there a file that needs to reside in the same folder as the project? I know that in java, this is the case. Please help. Thank Wesley
9
2765
by: Active8 | last post by:
Hi: Hope this isn't an IDE or OS problem. I'm using VC++ v6 and using cin in a console program. I use cin.getline() and sometimes just cin Oh... Winders XP Pro Corp Ed. (ooohh... scary, no?) SP 1 on XP and sp4, IIRC on VS enterprise. Really pushing the envelope here. This program works fine if I run it from VC++, but even after doing a release build, if I run the exe, it acts like I didn't enter a thing
2
1033
by: =?Utf-8?B?U3RldmUgQmVobWFu?= | last post by:
I thought that the << operator would act to get a whole line from a ifstream but it doesn't. It Is there an overload of << for some stream type that reads a whole text line from a file? I have tried the << operator within ifstream but it doesn't seem to work with it. PS: I am fairly sure that getline will do the job with ifstreams but it
5
2306
by: hanna88 | last post by:
hello, im having problem to copy the data from iostream& into string.and im not sure am i doing the right way by using getline.can anyone give hints how to solve this. here's to make things clear: //method that reads through the stream and breaks it into words and sentence
0
8538
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9083
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8797
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6459
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4323
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4557
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2237
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1957
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.