49 Unique Guild Members
14 Level 100 characters
461 Website/Forum Members
0 Posts in 24 hours
0 Posts in 7 days
84316 Total Posts
Nelthilta is the last poster
49 Unique Guild Members
14 Level 100 characters
461 Website/Forum Members
0 Posts in 24 hours
0 Posts in 7 days
84316 Total Posts
Nelthilta is the last poster

programming question
programming question
[Back to Index]
Thread Tags
Primary: [Off-topic]
Secondary: None

I have a program which reads in 3 file names given by the user and then uses the data on the files to do parallel array sorting.

there is an option for the user to save the organized data to a file that they choose. But when I run the program and try to save out it does not let me enter the name of the file.

here is the part of the code dealing with the file saving


char sname[50];
else if(choice == 3)
{
cout << "Enter name of output file: ";
cin.getline(sname, 50);
fout.open(sname);
for(int i = 0; i < nUnits; i++)
fout << nameArray[i] << "\t" << popArray[i] << "\t" << areaArray[i] << endl;
cout << "Wrote data to file \"" << sname << "\"\n" << endl;
fout.close();
}


I think that problem has something to do with the returns from when the user types in the other file names not being used up and just sitting there until now...but I'm not sure.

If anybody can see what my problem may be, I'd really appreciate it.

So when it prompts for "enter name of the output file", it just skips over that line and moves right into the fout.open(sname) line? Or does the program hang?

You could try flushing the input before reading by issuing cin.flush().


--
Quote
So when it prompts for "enter name of the output file", it just skips over that line and moves right into the fout.open(sname) line? Or does the program hang?

I guess it would help if i said that i initialized
istream fin;
ostream fout; (but you probably figured that out)
all i see are the cout statements and get no chance to input, so im guessing it is just going right into the fout.open line.

Quote

You could try flushing the input before reading by issuing cin.flush().


what library is cin.flush() found in?

I currently have cstring library, iostream, and fstream.
and using namespace std;

if you need more code to understand or want me to explain something just let me know.

and thanks for the help.

well, with Adam's help I got it working. we ended up using cin.sync().

Thanks for the help jesse


[Back to Index]