473,569 Members | 2,590 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getline problem

2 New Member
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::ci n, movie);

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

return 0;
}
Oct 31 '06 #1
1 2448
manontheedge
175 New Member
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
3546
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 function at first and then I use the cin>> function then the next time cin.getline function does not work.
4
3681
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() to close the file but the next open() fails. If I comment out the getline() then I can open it a second time without a problem. Here are parts of...
1
2354
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 record length flat file that is generated by an old IBM mainframe. These data fields need to be read in by my program EXACTLY as they are represented...
14
3865
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 ..txt file that I want to read into a multi-dimensional string array. Each line of the file needs to be read into the array. OK..sounds easy...
2
3446
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 char* cstr; std::cout << "House/Appartment # "; std::cin.getline(cstr, CHAR_MAX); //<<==seg fault
5
3630
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 looks like this ifstream file("levels\\level1.txt"); string line; getline(file, line);
11
3258
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 I put this code in my main program the b-array does not get a value. When I change my code the array does not get a value.
7
2439
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* before it continues past the getline. I tried changing the getline to std::getline(std::cin, randomStuff, '\n');
6
3806
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|| dfdg' so here my program does not go beyond the 2nd '|'. i am using ifstream to read the file. somehow check for eof() gets passed although end of...
11
13472
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 string is contained. From reading through posts to this list it seems that failbit gets set if there is a format error whilst reading. Is it bad form...
0
7701
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7615
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...
0
7924
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. ...
0
8130
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7979
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
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...
1
5514
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...
1
2115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
940
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...

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.