473,320 Members | 1,846 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,320 software developers and data experts.

getline problem

I'm new to c++, so bear with me...

Whenever I execute the code below, I am able to enter my name, but then the program skips the "getline" for the movie title and goes straight to the final cout statement.

What am I doing wrong?

Thanks,

Terence

------------------------------

# include <iostream>
# include <string>

using namespace std;

string name;
string movie;

int main()
{

cout << "Enter your name:" << endl;
cin >> name;

cout << "Enter your favorite movie:" << endl;
getline(std::cin, movie);

cout << name << "'s favorite movie is " << movie << endl;

return 0;
}
Oct 31 '06 #1
1 2418
manontheedge
175 100+
Expand|Select|Wrap|Line Numbers
  1. # include <iostream>
  2. # include <string>
  3.  
  4. using namespace std;
  5.  
  6. string name;
  7. char movie[70];  // changed the string to a character array
  8.  
  9.  
  10. int main()
  11. {
  12.  
  13.    cout << "Enter your name:" << endl;
  14.    cin >> name;
  15.  
  16.    cout << "Enter your favorite movie:" << endl;
  17.    std::cin.ignore();
  18.    std::cin.getline( movie, 70 );  
  19.  
  20.    cout << name << "'s favorite movie is " << movie << endl;
  21.  
  22.    return 0;
  23. }
  24.  
if you're using getline, the FIRST thing you have to do is use 'ignore()', otherwise, it will read in whitespace and won't even let the user enter anything. Then, i changed your 'string movie' to a character array (just picked a random size to allow for long movie titles). Then, inside the arguments for 'getline', you have to have the variable it's being put in, and the size. Hopefully this helps.
Oct 31 '06 #2

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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.