trying to create dynamic file names

Mar 31, 2009 at 12:55am
hey guys, im trying to create a program for an assignment that allows the user to create a file on the hard disk. I'm trying to let the user set a bunch of attributes about a certain vehicle like make, model, model year, VIN, etc. and i want create a dynamic file name containing the model year, make and model variables. The format would be modelYear_make_model (2003_Ford_Focus for example).

Is this possible? I tried and it didn't work for me so i thought it may be too many variables and i tried to cut it down to one like this,

Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
void vehicle::WriteFile()
{
	string fileName = "";

	ofstream outFile;
	cout << "Please give this file a name." << endl;
	cin << fileName;

	outFile.open(fileName);



}

I have the correct preprocessor statements for file i/o.
Can anyone tell me if creating dynamic file names is possible, if so, how do i do it, and if not, can you suggest an alternative?
Mar 31, 2009 at 1:02am
cin << fileName;
should be
cin >> fileName;
a common mistake.

and open takes a cstring, not a string, so
 
outFile.open(fileName.c_str());
Last edited on Mar 31, 2009 at 1:04am
Mar 31, 2009 at 1:13am
wow that was a dumb mistake. I fixed that, but im not sure what a cstring is, it seems to work in the code, but what does it mean? Also im getting one remaining error,

Error 1 error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) c:\ogre3d projects\ethanhohensee_labweek4\vehicle.cpp 29

it is at the line,
cin >> fileName;
Mar 31, 2009 at 1:47am
A cstring (often too called "string" which cases some confusion) is just an array of characters. I'm not sure why cin >> fileName; isn't working, perhaps it's an older library, but there should be a string getline function, so cin >> fileName; becomesgetline(cin, fileName);.
Mar 31, 2009 at 2:50am
Thanks a lot for the help. Just one more question, some of my getline statements dont pause for the user input? Here is my code. Am i doing anything wrong?
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
41
42
43
44
45
46
47
48
49
50
51
52
void vehicle::VehicleEntry()
{
	string fileName = "";

	ofstream outFile;
	cout << "Please give this file a name." << endl;
	getline(cin, fileName);

	outFile.open(fileName.c_str());


	char yesOrNo = ' ';

	cout << "What is the make of the vehicle?\n";
	getline(cin, make);
	cout << "What is the model of the vehicle?\n";
	getline(cin, model);
	cout << "What is the model year of the vehicle?\n";
	cin >> modelYear;
	cout << "What is the VIN of the vehicle?\n";
	getline(cin, VIN);
	cout << "Is the vehicle domestic(Y/N)?\n";
	cin >> yesOrNo;
	if(yesOrNo == 'Y' || yesOrNo == 'y')
	{
		domestic = true;
	}
	else
	{
		domestic = false;
	}
	cout << "What color is the vehicle?\n";
	getline(cin, color);
	cout << "What type of fuel does the vehicle take?\n";
	getline(cin, fuelType);
	cout << "What size is the vehicles engine(in liters)?\n";
	cin >> engineSize;
	cout << "What type of interior does the vehicle have?\n";
	getline(cin, interiorType);
	cout << "Is the vehicle turbocharged or supercharged(Y/N)?\n";
	cin >> yesOrNo;
	if(yesOrNo == 'Y' || yesOrNo == 'y')
	{
		turboCharged = true;
	}
	else
	{
		turboCharged = false;
	}

}

The code doesent stop of the file name or the VIN number but they dont look any different to me?
Mar 31, 2009 at 3:09am
getline won't promt the user if there is remaining text in the stream, and instead extract from that. Make sure the stream is clear before using it with cin.sync();
Mar 31, 2009 at 6:24am
The reason why cin >> (string) does not work is because it is not defined...you will just have to use getline(cin, mystring) like you said.
Topic archived. No new replies allowed.