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

  • C/C++
  • Thread starter Hellsing834
  • Start date
  • Tags
    C++
In summary, the student is having problems with the getline function. They need help understanding the function and how to use it.
  • #1
Hellsing834
19
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
  • #2
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.
 
  • #3
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.
 
  • #4
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
 
  • #5
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.
 
  • #6
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
 
  • #7
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.
 
  • #8
Is getline(cin, s1, s2, s3); legal?

I am taking a C++ course this semester and it seems like a logical extension...
 
  • #9
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?
 

1. What is the purpose of the getline() function in C++?

The getline() function in C++ is used to read a line of data from an input stream, such as a file or the standard input (keyboard). It is commonly used to read user input or to read data from a file.

2. How do I use the getline() function in my C++ code?

To use the getline() function in C++, you need to include the header file and use the syntax getline(inputStream, variable), where inputStream is the stream you want to read from and variable is the variable that will store the input data.

3. What happens if the getline() function fails to read data from the input stream?

If the getline() function fails to read data from the input stream, it will return a false value. This can happen if the end of the file is reached or if there is an error in the input stream. It is important to check the return value to handle such cases properly.

4. Can I specify a delimiter other than the default newline character when using getline()?

Yes, the getline() function allows you to specify a delimiter other than the default newline character. You can use the syntax getline(inputStream, variable, delimiter) to specify the delimiter. The delimiter can be a single character or a string.

5. Is the getline() function safe to use for user input?

Yes, the getline() function is generally considered safe to use for user input. However, it is important to properly validate and handle the input data to avoid potential errors or security vulnerabilities in your code. This can be done by checking the return value of the function and using appropriate error handling techniques.

Similar threads

  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
19
Views
960
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
Back
Top