cin.getline for int, or cin for strings?

Nov 23, 2009 at 7:37pm
Okay, so I'm supposed to be writing a program that will take data in from the keyboard and then output it to a data file. I was trying to use cin for the strings, but it doesn't seem to like when I have white space in the input. So I used cin.getline, but that doesn't seem to want to like int for me. So anybody know how I can get both a string and an int from the keyboard?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* CreateMyFile.cpp by James Cnossen 11/23/2009 */

/* This program prompts the user for input using cout and
 * accepts input from the user using cin.  It will store
 * this input in a data structure, and then it will write
 * this data to a file called Cnossen.dat.
 */

#include <iostream>
using namespace std;

int main(void) {

//Data structure for the inputted data
struct roadsStruct {
int recordNum;
string routeName;
string startPoint;
string endPoint;
double length;
int dateCompleted;
} roads;


roads.recordNum += 1;
cout << "Highway Trip calculator by James Cnossen";
cout << endl << "Enter the route name: ";
cin >> roads.routeName;
cout << endl << "Enter the starting point: ";
cin >> roads.startPoint;
cout << endl << "Enter the ending point: ";
cin >> roads.endPoint;
cout << endl << "Enter the length (in miles): ";
cin >> roads.length;
cout << endl << "Enter the date completed: ";
cin >> roads.dateCompleted;
cout << endl;

}
Nov 23, 2009 at 7:40pm
Topic archived. No new replies allowed.