Need C++ getline() Help

  1. 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!
  2. jcsd
  3. mgb_phys

    mgb_phys 8,809
    Science Advisor
    Homework Helper

    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.
  4. 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.
  5. mgb_phys

    mgb_phys 8,809
    Science Advisor
    Homework Helper

    getline takes the input and puts it into a single string
    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
  6. 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.
  7. mgb_phys

    mgb_phys 8,809
    Science Advisor
    Homework Helper

    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
  8. 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.
  9. Dr Transport

    Dr Transport 1,535
    Science Advisor
    Gold Member

    Is getline(cin, s1, s2, s3); legal???

    I am taking a C++ course this semester and it seems like a logical extension......
  10. mgb_phys

    mgb_phys 8,809
    Science Advisor
    Homework Helper

    No, you could write one but the std::lib function takes either a string, or a char* and a ize.
  11. 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.
  12. jtbell

    Staff: 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.
  13. That is true, but then one would still need to convert the string to a number.
  14. 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: ";
    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: Oct 19, 2008
  15. jtbell

    Staff: Mentor

    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.
  16. The previous line doesn't do much either, and only compile if the 3 identifier had already been declared.
    Valid syntax C++, get evaluated as
    Code (Text):
    getline(cin, sales,',');
  17. 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?
  18. jtbell

    Staff: Mentor

    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.
  19. mgb_phys

    mgb_phys 8,809
    Science Advisor
    Homework Helper

    There's a convert.ToDouble() in c#, are confusing it with this?
Know someone interested in this topic? Share this thead via email, Google+, Twitter, or Facebook

Have something to add?
Similar discussions for: Need C++ getline() Help