473,403 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

The Getline() Problem

This is the second time I am experiencing a problem with getline.
I've used it before successfully, but I dont know what I'm doing wrong here.
Here is the code where the problem is:

Expand|Select|Wrap|Line Numbers
  1.    if (a == 6)
  2.     {
  3.   string f;
  4.   int g;
  5.   cout << "Enter the message to display at the end of the countdown:" << endl;
  6.   getline (cin, f);
  7.   cout << endl;
  8.   cout << "< Enter the starting number >- ";
  9.   cin >> g;
  10.  
  11.   while (g>0) {
  12.     cout << g << ", ";
  13.     --g;
  14.   }
  15. cout << f << endl;
  16. cout << endl;
  17. cout << endl;
  18.    }
  19.  
The code never waits for user input at line 6, and skips straight to the code after it. String f is totally ignored throughout the entire code, and i have included strings. What to do? How do I fix it?
Aug 16 '07 #1
9 4090
oler1s
671 Expert 512MB
I know (or have a very educated guess) on what the problem is, but I need to mention a few tangential points. The first is to write properly indented code. It's a very small snippet, but your code is already showing inconsistencies in indentation. It's fine for a trivially small code snippet, but please make sure in the future that your code follows a proper indentation style. It helps you because you can spot mistakes faster, and it helps us to read your code.

Also, avoid the user of one letter variable names. It's quick and convenient, but unless you have specific short term situations like an iterated indexer, in the end one letter variable names only obfuscate. Is it really that hard to type a longer name?

When trying to check if a code is faulty, try to write a fully compilable code example that uses the faulty code. Had you done so, you would have determined that by itself, the code works. So something from before is creating problems.

What causes getline to skip? Well, for one thing, getline isn't skipping. Getline returns when you hit that enter key. Remember, hitting the enter key is equivalent to entering a newline character into the stream. So if getline "skips", it's really because there's a leftover newline.

The culprit is cin >> whatever. The >> operation will leave a \n in the stream buffer (cin). So when you use getline, it first encounters a \n. Equivalent to typing the enter key right away.

See http://www.augustcouncil.com/~tgibso....html#problems . Either use getline for all input, or ignore or get rid of the newline in the buffer.
Aug 16 '07 #2
so wat exactly do I have to get rid of?
Aug 16 '07 #3
Ganon11
3,652 Expert 2GB
You shouldn't have to get rid of anything. Just understand that, before your getline statement, there is an extraneous '\n' still in the input stream. Since getline reads information until the next '\n' is found, it will read this first character and stop reading. You have to get rid of that '\n' somehow - either by reading it and discarding it with a cin.get() statement, or with a cin.ignore() statement.
Aug 16 '07 #4
Hey, it works. I inserted
Expand|Select|Wrap|Line Numbers
  1. cin.ignore();
before the getline command, and now it works!

Thanx a lot ganon

:P
Aug 16 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
Hey, it works. I inserted

Code: ( text )
cin.ignore();
What if there is more than one character you need to ignore? You may need to flush the input buffer before your getline() call.
Aug 16 '07 #6
how do i do that?

? ?
?
----
Aug 16 '07 #7
weaknessforcats
9,208 Expert Mod 8TB
Use fflush(stdin).

stdin is the input buffer stream.

If fflush() returns 0, the buffer was successfully flushed.
Aug 16 '07 #8
oler1s
671 Expert 512MB
weaknessforcats, please see http://faq.cprogramming.com/cgi-bin/...&id=1043284351

fflush is defined only for output streams. stdin happens to be an input stream. Quite often, the compiler has a non-portable extension for fflush(stdin), but it relies on non-standard behavior. There's a perfectly valid way to strip whitespace without breaking any standards.

Try cin.ignore(std::numeric_limits<int>::max()). You can also use the sync member function (http://www.cplusplus.com/reference/i...ream/sync.html ).
Aug 16 '07 #9
weaknessforcats
9,208 Expert Mod 8TB
I forgot about sync. I was trying to avoid __flushall() since it is Microsoft specific.
Aug 18 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Vikram | last post by:
Hi, I don't remember if it happened previously, but nowadays I'm having problem with using cin.getline function and cin>> function simultaneously. I have Visual Studio 6. If I use cin.getline...
4
by: Joe | last post by:
Hello - I wrote a program that uses ifstream to open an ASCII file and getline() to read in the lines. The problem is when I try to open the same file again later in the code. I used close()...
1
by: Jim Phelps | last post by:
Hello all, I am in a bit of a pickle using the getline function with an ifstream. It does not seem to work as advertised. Here is my scenario. In a nutshell, my code needs to pick up a fixed...
14
by: KL | last post by:
I am so lost. I am in a college course for C++, and first off let me state I am not asking for anyone to do my assignment, just clarification on what I seem to not be able to comprehend. I have a...
2
by: jalkadir | last post by:
I am trying to get character string from the user, to do that I use getline(char_type*, streamsize), but I get a segmentation fault??!! Can anyone give me a hand, what am I doing wrong? --snip...
5
by: allspamgoeshere3 | last post by:
Hi! I'm having trouble reading text lines from a file describing the levels for a simple game I'm creating. Inside the function that reads the content of the file there is a loop that basically...
11
by: Markus | last post by:
Hi, I want to get an integer from the user. If the user inserts a character instead of an int the program goes crazy. So I tried something like this: This program works pretty well. But if...
7
by: Chris | last post by:
Running into a problem on Windows. This code std::string randomStuff; std::getline(std::cin, randomStuff); works on unix, but on windows, it requires the user to hit the enter key *twice*...
6
by: ankit.kumar.agarwal | last post by:
I am facing a problem with getline I am reading a text file with a getline function the lines can have '|' as separator. everything works OK but in case if i have 2 delimitors in file '234||...
11
by: rory | last post by:
I am reading a binary file and I want to search it for a string. The only problem is that failbit gets set after only a few calls to getline() so it never reaches the end of the file where the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.