473,382 Members | 1,424 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,382 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 2425
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.