473,402 Members | 2,072 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,402 software developers and data experts.

Problem with c++ getline

I'm using the getline function to read the Degree Program of the user but for some reason the program skips the degree program and goes on to the next line which asks for the courses that the useris doing this semester.
This is the code:

#include <iostream>
#include <string>

using namespace std;

int main()
{

char name[256];
char id[256];
string degree;
char courses[256];

cout <<"Please insert your name " << endl;
cin.get(name,256);
cout << "Please insert your ID# " << endl;
cin>>id;
cout<<endl;
cout << "Please insert your Degree Program: " << endl;
cin.getline(degree,(256));
cout<<endl;
cout << "Please insert your Courses: " <<endl;
cin.getline(courses,256);

return 0;
}
Oct 21 '08 #1
4 1538
joem86
2
I've always had troubles with cin.getline. Try the other getline (defined by <string>)

Expand|Select|Wrap|Line Numbers
  1. getline(cin, degree);
I hope this helps!

Joe
Oct 21 '08 #2
boxfish
469 Expert 256MB
That's probably because when you cin to id, the newline character is left on the stream, and degree gets the empty line that id was on. Maybe you could do an extra getline or use ignore with the '\n' character.
Hope this helps.

Please use code tags around your code. Put [CODE] before it and [/CODE] after it so it shows up in a code box and the indentation isn't wrecked. Thanks.
Oct 22 '08 #3
Thank you so much but i found the source of the problem . I had to include cin.get(); for some strange new reason. here's the new code.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{

char name[256];
char id[256];
string degree;
string courses;

cout <<"Please insert your name " << endl;
cin.get(name,256);
cout << "Please insert your ID# " << endl;
cin>>id;
cout<<endl;
cin.get();
cout << "Please insert your Degree Program " << endl;
getline(cin,degree);
cout<<endl;
cout << "Please insert your Courses: " <<endl;
getline(cin,courses);


return 0;
}
Oct 22 '08 #4
Thanks. Yep. That was the problem.

That's probably because when you cin to id, the newline character is left on the stream, and degree gets the empty line that id was on. Maybe you could do an extra getline or use ignore with the '\n' character.
Hope this helps.

Please use code tags around your code. Put [CODE] before it and [/CODE] after it so it shows up in a code box and the indentation isn't wrecked. Thanks.
Oct 22 '08 #5

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

Similar topics

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()...
3
by: SB | last post by:
Hello. I have an input file which is laid out in the following manner... Name Day 1 am time 1 am time 2 appointment pm time 1 pm time 2 appointment Day 2
4
by: Eric Boutin | last post by:
Hi all ! I'm currently writing a function that evaluate if there is a diffence beetween 2 streams (istringstream and ifstream). The problem is, it doesn't work. It *always* finds a difference...
15
by: Protoman | last post by:
My code keeps putting the input statements on the same line, instead of letting me input them normally; here's the code: int main() { cout << "Encrypt or decrypt? "; bool select; cin >>...
0
by: abottchow | last post by:
Hi all, I'm new to the group and am seeking your advice on my Linux programming problem. Two programs are involved. One is myProgram.cc, which reads user's input from keyboard and prints...
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...
16
by: Jordan Tiona | last post by:
Here is my code. main.cpp: #include "cddb.h" CDData* head; CDData* curData; int main(){
9
by: Thomas Helmke | last post by:
Hello NG. I'm trying to read in a textfile. I will give you an extract of my code: bool LogfileHandle::ReadFile( string filename ) { std::ifstream LogFile( filename.c_str() ); if(...
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...
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||...
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
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...
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
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
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.