Register to reply

Need C++ getline() Help

by Hellsing834
Tags: getline
Share this thread:
Hellsing834
#1
Oct16-08, 06:18 PM
P: 19
I am just in a basic C++ class in high school.

I am assigned to a project [ Its really simple, but i am having problems with getline]

I am suppose to make a program in which, the all data is entered into one line, and i need to use the getline function to take that data, and assign it to different variables.

so for example: [I need to enter sales, all 3 sales in one line]

Lets say these are the sales :
1) sale1 = 23
2) sale2 = 56
3) sale3 = 25

I need to enter those 3 numbers into one line, but assign those numbers to different variables.

So In the program, its like

cout << "Enter the sales: " [ and i enter 23 56 25 all in one line]
then i need to use the getline function to assign those sales to variables sale1 sale2 and sale3

Can someone please help me out?

I am just really a beginner in c++, and i don't understand complicated terminology.

Thank you!
Phys.Org News Partner Science news on Phys.org
Physical constant is constant even in strong gravitational fields
Montreal VR headset team turns to crowdfunding for Totem
Researchers study vital 'on/off switches' that control when bacteria turn deadly
mgb_phys
#2
Oct16-08, 06:31 PM
Sci Advisor
HW Helper
P: 8,953
You could read the line into three variables using cin
cin >> sale1 >> sale2 >> sale3

Or if you want to use getline() to read it into a temporary string you would use stringstream to split it again. Stringstream lets you treat a string in the same way as cin.
Hellsing834
#3
Oct16-08, 06:33 PM
P: 19
but my instructor has specifically asked us to use getline() function and that only. I belive its something like getline(sales1,sales2,sales3), and i have tried that, but it just wont work.

mgb_phys
#4
Oct16-08, 06:40 PM
Sci Advisor
HW Helper
P: 8,953
Need C++ getline() Help

getline takes the input and puts it into a single string
eg.
cout << "enter sales figures" << std::endl
string s;
getline( cin, s );

The string s now contains "23 56 25" - you have to split the string to get the three values
Hellsing834
#5
Oct16-08, 06:43 PM
P: 19
well, we aren't using strings, we are just using double. Atleast i am, because the problem is that i have to enter 3 sale amounts and 3 month names. Then i need the program to display the total, the average. Then it will display what sale1 = , what sale2= and what sale3 = . So at the start, i have to input all in one line, and at the end, i need it to display in seperate line, and same goes for the month. But i am just using double, because this is our own project, and our instructor cannot help us.
mgb_phys
#6
Oct16-08, 07:04 PM
Sci Advisor
HW Helper
P: 8,953
Anything you enter at the keyboard is a string, getline() can only handle strings.
Once you have read the line in as a string - you need to convert it into a double.
you can do this in c++ using stringstream (Either entering all 3 numbrs at once, or one at a time)

Search for getline() and stringstream examples
Hellsing834
#7
Oct16-08, 07:07 PM
P: 19
well, for that i am only allowed to use static_cast, so i think ill have to do that =/. But thanks for letting me know on the strings thing. But i need to be able to enter all the numbers in one line, and then at the end display them in another line. And i can only use getline. So i cant use stringstream, then i dont know how I can assign the numbers from just one string to three.
Dr Transport
#8
Oct16-08, 09:16 PM
Sci Advisor
PF Gold
P: 1,481
Is getline(cin, s1, s2, s3); legal???

I am taking a C++ course this semester and it seems like a logical extension......
mgb_phys
#9
Oct16-08, 09:37 PM
Sci Advisor
HW Helper
P: 8,953
No, you could write one but the std::lib function takes either a string, or a char* and a ize.
KTC
#10
Oct16-08, 10:13 PM
P: 90
Quote Quote by Hellsing834 View Post
well, for that i am only allowed to use static_cast, so i think ill have to do that =/. But thanks for letting me know on the strings thing. But i need to be able to enter all the numbers in one line, and then at the end display them in another line. And i can only use getline. So i cant use stringstream, then i dont know how I can assign the numbers from just one string to three.
It is not possible to do what you're wanting by calling and only calling getline() and not end up using string. getline() read into a string (the stream version read into a C-style string, while the std::string version read into a C++-style string). You either simply read in the 3 numbers using cin as shown before, or you use getline() and then (for the simplest solution) use stringstream to spilt out the 3 seperate value.
jtbell
#11
Oct17-08, 07:26 AM
Mentor
jtbell's Avatar
P: 11,881
By default, getline() reads until the end of the line (the "newline" character, '\n'). You can use some other character as the "line terminator" instead. For example,

getline (cin, myString, ' ');

reads from cin into the string myString until it encounters a blank space.
KTC
#12
Oct17-08, 05:16 PM
P: 90
That is true, but then one would still need to convert the string to a number.
Hellsing834
#13
Oct19-08, 07:36 PM
P: 19
Now i have another problem.

I did get the string to break apart. But when i was woking in XP, it worked perfectly fine. Now in vista, it doesnt work. I don't know if its an OS problem but this is how i have it set up.

cout << "Enter the sales, seperated by a comma: ";
sales;salestwo;salesthree;
getline(cin, sales,','),(cin,salestwo,','),(cin,salesthree);

It worked fine in XP, like it would display all the 3 numbers seperatly when i did this. Now in vista, it just displays the first sale and the rest are blank. Am i doing something wrong?
jtbell
#14
Oct19-08, 11:01 PM
Mentor
jtbell's Avatar
P: 11,881
Quote Quote by Hellsing834 View Post
getline(cin, sales,','),(cin,salestwo,','),(cin,salesthree);
That's not a valid statement, or at least if it's technically valid according to the compiler, it doesn't do what you want. Use three separate getline() statements, separated by semicolons.
KTC
#15
Oct19-08, 11:50 PM
P: 90
The previous line doesn't do much either, and only compile if the 3 identifier had already been declared.
getline(cin, sales,','),(cin,salestwo,','),(cin,salesthree);
Valid syntax C++, get evaluated as
getline(cin, sales,',');
cin;
salestwo;
',';
cin;
salesthree;
Hellsing834
#16
Oct20-08, 06:19 PM
P: 19
Quote Quote by jtbell View Post
That's not a valid statement, or at least if it's technically valid according to the compiler, it doesn't do what you want. Use three separate getline() statements, separated by semicolons.
Yeah i figured that out right after i posted that, i tried putting getline before the cin, and it worked like magic.


Now the only problem is the converstion. My instructor was talking about something like Covert::ToDouble. and i was trying that but the compiler was giving me bunch of errors.

Do you know the correct way on how to convert a string to a double by using the Convert::ToDouble?
jtbell
#17
Oct20-08, 07:14 PM
Mentor
jtbell's Avatar
P: 11,881
Convert::ToDouble is not part of standard C++ as far as I know (unless they added it while I wasn't looking! ). It might be part of an add-on library supplied by Microsoft (or whoever wrote your compiler, I don't think you said which compiler you're using). Or maybe it was written specifically for your textbook; some textbook authors like to define their own libraries and provide them on a CD or something, to make certain operations easier. Either way, I don't know anything about it. Hopefully someone else here will recognize it.
mgb_phys
#18
Oct20-08, 07:17 PM
Sci Advisor
HW Helper
P: 8,953
There's a convert.ToDouble() in c#, are confusing it with this?


Register to reply

Related Discussions
Difference between getline() and get()? Programming & Computer Science 9
Getline help Programming & Computer Science 0