| Thread Closed |
Need C++ getline() Help |
Share Thread | Thread Tools |
| Oct16-08, 06:18 PM | #1 |
|
|
Need C++ getline() Help
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! |
| Oct16-08, 06:31 PM | #2 |
|
Recognitions:
|
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. |
| Oct16-08, 06:33 PM | #3 |
|
|
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.
|
| Oct16-08, 06:40 PM | #4 |
|
Recognitions:
|
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 |
| Oct16-08, 06:43 PM | #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 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.
|
| Oct16-08, 07:04 PM | #6 |
|
Recognitions:
|
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 |
| Oct16-08, 07:07 PM | #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 cant use stringstream, then i dont know how I can assign the numbers from just one string to three.
|
| Oct16-08, 09:16 PM | #8 |
|
|
Is getline(cin, s1, s2, s3); legal???
I am taking a C++ course this semester and it seems like a logical extension...... |
| Oct16-08, 09:37 PM | #9 |
|
Recognitions:
|
No, you could write one but the std::lib function takes either a string, or a char* and a ize.
|
| Oct16-08, 10:13 PM | #10 |
|
|
|
| Oct17-08, 07:26 AM | #11 |
|
Mentor
|
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. |
| Oct17-08, 05:16 PM | #12 |
|
|
That is true, but then one would still need to convert the string to a number.
|
| Oct19-08, 07:36 PM | #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 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? |
| Oct19-08, 11:01 PM | #14 |
|
Mentor
|
|
| Oct19-08, 11:50 PM | #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; |
| Oct20-08, 06:19 PM | #16 |
|
|
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? |
| Oct20-08, 07:14 PM | #17 |
|
Mentor
|
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.
|
| Thread Closed |
| Thread Tools | |
Similar Threads for: Need C++ getline() Help
|
||||
| Thread | Forum | Replies | ||
| difference between getline() and get()? | Programming & Comp Sci | 9 | ||
| getline help | Programming & Comp Sci | 0 | ||