Results 1 to 23 of 23

Thread: cin.getline() Not Working?

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    cin.getline() Not Working?

    It's been awhile since I've used C++. Why doesn't this work?

    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
    
    	//input variables:	
    	char destination[20], datedepart[8], datereturn[8];
    	double ticketprice;
    	int ticketcount;
    
    	cout << "Destination: ";
    	cin.getline(destination, 20);
    
    	cout << "Departure Date: ";
    	cin >> datedepart;
    	
    	cout << "Return Date: ";
    	cin >> datereturn;
    	cout << "Number of Tickets: ";
    	cin >> ticketcount;
    	cout << "Price per Ticket: ";
    	cin >> ticketprice; 
    
    	cout << endl << destination << endl << endl;
    
    	return 0;
    
    }
    For destination it outputs nothing?

    Example Input would be:

    Destination: Florida
    Depart Date: 12/01/02
    Return Date: 12/11/02
    Number of Tickets: 2
    Price per Ticket: 345.00
    Last edited by The Hobo; Sep 13th, 2002 at 03:06 PM.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Anybody?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    flush what it have in cin with cin.flush(); before using getline... But I am not sure about .flush() but I know that you have to clear what cin have to use getline properly.

  4. #4
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    It seems to work if you make the destination array 21 not 20 Is that because you need a \0 there are well?

  5. #5

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    .flush doesn't exist...I tried .clear but it didn't help at all. I put it right before the .getline.

    Any other ideas?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  6. #6

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    When I change it to this:

    Code:
    cout << "Destination: ";
    cin.getline(destination, 20);
    cout << destination << endl;
    It outputs fine. So at what point is the value being erased?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  7. #7
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    Check what's goind on in debug, I will check why it does that when I will come back of the gym.

  8. #8

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I need to go to work now. Thanks for your help so far!

    I'll try a few things when I get home and maybe one of us can find a solution. I've never encountered anything like this before, though.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  9. #9
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Try something along the lines of "cin.ignore('\n', 100)".

    Z.

  10. #10

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by Zaei
    Try something along the lines of "cin.ignore('\n', 100)".

    Z.
    I tried. Where exactly should I put it? And don't the arguments go the other way around?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  11. #11

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Okay, this is my full code. Just run it to see what's wrong. It's very obvious:

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main() {
    	
    	double flightfee = 10.00, taxrate = 0.06;
    
    	//input variables:	
    	char destination[20], datedepart[8], datereturn[8];
    	double ticketprice = 0, ticketcount = 0;
    	
    	double totaltax = 0, tickettotal = 0, grandtotal = 0;
    	
    	//*****THIS IS WHERE WE GET IT********
    	cout << "Destination: ";
    	cin.getline(destination, 20, '\n');
    
    	cout << "Departure Date: ";
    	cin >> datedepart;
    	
    	cout << "Return Date: ";
    	cin >> datereturn;
    
    	cout << "Number of Tickets: ";
    	cin >> ticketcount;
    	cout << "Price per Ticket: ";
    	cin >> ticketprice; 
    	
    	//tickettotal = ticketprice * ticketcount;
    	//totaltax = tickettotal * taxrate;
    	//grandtotal = tickettotal + totaltax + flightfee;
    
    	cout << endl;
    	cout << "********************************************" << endl;
    	cout << "Destination: " << destination << endl; //*****DOESN'T WORK
    	cout << "Departure Date: " << datedepart << endl;
    	cout << "Return Date: " << datereturn << endl << endl;
    	cout << "Number of Tickets: " << ticketcount << endl;
    
    	cout << setprecision(2) << setiosflags(ios::fixed);
    	cout << "Total Ticket Price: $" << tickettotal << endl;
    	cout << "Flight Fees: $" << flightfee << endl;
    	cout << "Tax: $" << totaltax << endl;
    	cout << "Grand Total: $" << grandtotal << endl;
    	cout << "********************************************" << endl << endl;
    
    
    	return 0;
    
    }
    However when you comment out the two lines:

    Code:
    cout << "Departure Date: ";
    cin >> datedepart;
    It works perfectly!?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  12. #12
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    This works for me:
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main() {
    	
    	double flightfee = 10.00, taxrate = 0.06;
    
    	//input variables:	
    	char destination[20], datedepart[11], datereturn[11];
    	double ticketprice = 0, ticketcount = 0;
    	
    	double totaltax = 0, tickettotal = 0, grandtotal = 0;
    	
    	//*****THIS IS WHERE WE GET IT********
    	cout << "Destination: ";
    	cin.getline(destination, 20, '\n');
    
    	cout << "Departure Date: ";
    	cin.getline(datedepart, 10, '\n');
    	
    	cout << "Return Date: ";
    	cin.getline(datereturn, 10, '\n');
    
    	cout << "Number of Tickets: ";
    	cin >> ticketcount;
    	cout << "Price per Ticket: ";
    	cin >> ticketprice; 
    	
    	tickettotal = ticketprice * ticketcount;
    	totaltax = tickettotal * taxrate;
    	grandtotal = tickettotal + totaltax + flightfee;
    
    	cout << endl;
    	cout << "********************************************" << endl;
    	cout << "Destination: " << destination << endl; //*****DOESN'T WORK
    	cout << "Departure Date: " << datedepart << endl;
    	cout << "Return Date: " << datereturn << endl << endl;
    	cout << "Number of Tickets: " << ticketcount << endl;
    
    	cout << setprecision(2) << setiosflags(ios::fixed);
    	cout << "Total Ticket Price: $" << tickettotal << endl;
    	cout << "Flight Fees: $" << flightfee << endl;
    	cout << "Tax: $" << totaltax << endl;
    	cout << "Grand Total: $" << grandtotal << endl;
    	cout << "********************************************" << endl << endl;
    
    
    	return 0;
    
    }
    Using:
    Florida
    12-01-02 (or 12/01/02)
    12-11-02 (or 12/11/02)
    2
    345.00

  13. #13
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Try using this:
    Code:
    #include <iostream>
    #include <string>
    
    void code() {
        string lin;
    
        getline(cin, lin);
    }
    This way you don't need to worry about array sizes
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  14. #14
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Know what mine does when I try that, parksie?

    "Blch Blah Blah, function getline() does not take 2 arguments... blah blah blah".

    So, I go and add an argument:
    "blah blah blah, function getline() does not take 3 arguments....blah blah"

    =).

    So, we just write our own =).

    Z.

  15. #15
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Hmmm. It should be a global function that takes an istream& and a string& ...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  16. #16

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Thanks a lot, Matt. That works great!

    parksie: I have to use char arrays for now because my teacher says so.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  17. #17
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Char arrays are for C. In C++ you should learn strings first, THEN move to the arrays once you understand why you don't normally use them anyway.

    *sigh*
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  18. #18

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by parksie
    Char arrays are for C. In C++ you should learn strings first, THEN move to the arrays once you understand why you don't normally use them anyway.

    *sigh*
    Well the class is called C/C++ Programming.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  19. #19
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    There is no C/C++. You either write C or you write C++.

    And people wonder why I hate programming courses
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  20. #20

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by parksie
    There is no C/C++. You either write C or you write C++.

    And people wonder why I hate programming courses
    I didn't name it. Not my fault.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  21. #21
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I'm not blaming you. I'm blaming people who teach things or write courses on them without knowing the details first.

    It's the same pattern everywhere, and it's beginning to *really* annoy me
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  22. #22

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by parksie
    I'm not blaming you. I'm blaming people who teach things or write courses on them without knowing the details first.

    It's the same pattern everywhere, and it's beginning to *really* annoy me
    I'm just annoyed by the fact that they're making me take this course. I already took an almost identical one in high school, plus I had previous knowledge.

    Damn counselors
    My evil laugh has a squeak in it.

    kristopherwilson.com

  23. #23
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Yeah... Lets teach the kiddies C, and then just add classes on top. Anyone who takes a course is nearly guarenteed to not know how to use classes effectivly.

    Z.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width