Im trying to allow the user to input a file into a char. example:
char file[100];
cout << "what is your file?"
cin >> file;
and I am outputting info about the number within the input file.
To do so, I am using an array to store the data in the file.
At the end of the program I ask the user if they have another file to analyze.
I need to figure out how to reset the array as well as the char to store the new info.
Can I set them = to zero at the beginning of each loop. ex:
char file[100]=0;
Don't think that would work, just a thought.
When I got to the second loop, the file failed to open. The function reset_it must not be working quite right. any suggestions? I just included part of the code for simplicity. thanks
void reset_it(char file[], int n);
/*to reset the values of the for a new file to be entered*/
void close_files (ifstream& fin, ofstream& fout);
/*to close the opened files*/
int main(int argc, char *argv[])
{
int count = 0;
double mean, list[1000];
ifstream fin;
ofstream fout;
char ans;
do
{
get_file (fin, fout);
find_mean (fin, fout, mean, list, count);
deviation (fin, fout, list, count, mean);
sort_array(list, count);
median (list, count, fout);
reset_array (list, count);
close_files (fin, fout);
cout << "Would you like to analize another file? Y or N:" << endl;
cin >> ans;
}while (ans == 'Y' || ans == 'y');
system("PAUSE");
return EXIT_SUCCESS;
}
void get_file (ifstream &fin, ofstream &fout)
{
char in_file[40], out_file[40];
reset_it (in_file, 40);
reset_it (out_file, 40);
cout << "What is the name of the file containing data to be analyzed?\n";
cin >> in_file;
fin.open(in_file);
if (fin.fail())
{
cout << "Input file failed.\n";
exit(1);
}
cout << "What is the name of the file where you would like to store data?\n";
cin >> out_file;
fout.open(out_file, ios::app);
if (fout.fail())
{
cout << "imput file failed.\n";
exit(1);
}
void reset_it(char file[], int n)
{
for(int i = 0; i < n; i++)
{
file[i] = ' ';
}
}
Use indentation and put you code inside code tags [code][/code] and it will be easier to read your code.
It looks like the ending bracket of get_file is missing.
A null character '\0' marks the end of a string so to reset a string all you have to do is to make sure the first character in the string is '\0'. If a string is not properly null terminated it is not safe to print the string or pass it to the normal string functions. When you create the array you can do char in_file[40] = {}; if you want all the elements in the string to be '\0'.
So from what you mentioned, my char should be reset each time it is used, but the file fails. Doesn't seem to work on the second loop. first loop works fine. On the second: input file failed.
void get_file (ifstream &fin, ofstream &fout)
{
char in_file[40]= {}, out_file[40]= {};
cout << "What is the name of the file containing data to be analyzed?\n";
cin >> in_file;
fin.open(in_file);
if (fin.fail())
{
cout << "Input file failed.\n";
exit(1);
}
cout << "What is the name of the file where you would like to store data?\n";
cin >> out_file;
fout.open(out_file, ios::app);
if (fout.fail())
{
cout << "imput file failed.\n";
exit(1);
}
}