C/C++ Getline() Help for C++ Class: Assigning Multiple Variables from One Line of Data

  • Thread starter Thread starter Hellsing834
  • Start date Start date
  • Tags Tags
    C++
AI Thread Summary
The discussion focuses on using the getline function in C++ to read multiple sales figures entered in a single line and assign them to separate variables. The user is required to input three sales figures and is struggling with the conversion from a string to double, as their instructor has mandated the use of getline only. It is clarified that getline reads input into a single string, and the user must then split this string to extract the individual sales values. The conversation also touches on the challenges faced when switching operating systems, as previous code worked on XP but not on Vista, highlighting the need for proper syntax in C++. Ultimately, the user seeks assistance with converting strings to doubles, as the suggested Convert::ToDouble is not standard in C++.
Hellsing834
Messages
18
Reaction score
0
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!
 
Technology news on Phys.org
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 let's you treat a string in the same way as cin.
 
but my instructor has specifically asked us to use getline() function and that only. I believe its something like getline(sales1,sales2,sales3), and i have tried that, but it just won't work.
 
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
 
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 separate 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.
 
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
 
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 can't use stringstream, then i don't know how I can assign the numbers from just one string to three.
 
Is getline(cin, s1, s2, s3); legal?

I am taking a C++ course this semester and it seems like a logical extension...
 
No, you could write one but the std::lib function takes either a string, or a char* and a ize.
 
  • #10
Hellsing834 said:
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 can't use stringstream, then i don't 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 separate value.
 
  • #11
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.
 
  • #12
That is true, but then one would still need to convert the string to a number.
 
  • #13
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 doesn't work. I don't know if its an OS problem but this is how i have it set up.

cout << "Enter the sales, separated 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?
 
Last edited:
  • #14
Hellsing834 said:
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.
 
  • #15
The previous line doesn't do much either, and only compile if the 3 identifier had already been declared.
Code:
getline(cin, sales,','),(cin,salestwo,','),(cin,salesthree);
Valid syntax C++, get evaluated as
Code:
getline(cin, sales,',');
cin;
salestwo;
',';
cin;
salesthree;
 
  • #16
jtbell said:
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?
 
  • #17
Convert::ToDouble is not part of standard C++ as far as I know (unless they added it while I wasn't looking! :eek:). 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.
 
  • #18
There's a convert.ToDouble() in c#, are confusing it with this?
 
Back
Top