|
-
Sep 13th, 2002, 01:53 PM
#1
Thread Starter
Stuck in the 80s
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.
-
Sep 13th, 2002, 03:28 PM
#2
Thread Starter
Stuck in the 80s
Anybody?
-
Sep 13th, 2002, 03:32 PM
#3
Ya ya Baby!!!Me is Back
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.
-
Sep 13th, 2002, 03:35 PM
#4
Frenzied Member
It seems to work if you make the destination array 21 not 20 Is that because you need a \0 there are well?
-
Sep 13th, 2002, 03:36 PM
#5
Thread Starter
Stuck in the 80s
.flush doesn't exist...I tried .clear but it didn't help at all. I put it right before the .getline.
Any other ideas?
-
Sep 13th, 2002, 04:17 PM
#6
Thread Starter
Stuck in the 80s
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?
-
Sep 13th, 2002, 05:22 PM
#7
Ya ya Baby!!!Me is Back
Check what's goind on in debug, I will check why it does that when I will come back of the gym.
-
Sep 13th, 2002, 05:26 PM
#8
Thread Starter
Stuck in the 80s
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.
-
Sep 13th, 2002, 06:34 PM
#9
Frenzied Member
Try something along the lines of "cin.ignore('\n', 100)".
Z.
-
Sep 13th, 2002, 11:15 PM
#10
Thread Starter
Stuck in the 80s
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?
-
Sep 13th, 2002, 11:41 PM
#11
Thread Starter
Stuck in the 80s
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!?
-
Sep 13th, 2002, 11:52 PM
#12
Fanatic Member
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
-
Sep 14th, 2002, 06:35 AM
#13
Monday Morning Lunatic
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
-
Sep 14th, 2002, 09:04 AM
#14
Frenzied Member
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.
-
Sep 14th, 2002, 09:29 AM
#15
Monday Morning Lunatic
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
-
Sep 14th, 2002, 10:39 AM
#16
Thread Starter
Stuck in the 80s
Thanks a lot, Matt. That works great!
parksie: I have to use char arrays for now because my teacher says so.
-
Sep 14th, 2002, 10:45 AM
#17
Monday Morning Lunatic
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
-
Sep 14th, 2002, 10:47 AM
#18
Thread Starter
Stuck in the 80s
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.
-
Sep 14th, 2002, 10:48 AM
#19
Monday Morning Lunatic
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
-
Sep 14th, 2002, 10:51 AM
#20
Thread Starter
Stuck in the 80s
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.
-
Sep 14th, 2002, 10:53 AM
#21
Monday Morning Lunatic
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
-
Sep 14th, 2002, 10:56 AM
#22
Thread Starter
Stuck in the 80s
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
-
Sep 14th, 2002, 11:38 AM
#23
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|