resetting a string and an array for a loop

Apr 10, 2012 at 2:17pm
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.

Thanks.
Apr 10, 2012 at 2:21pm
a simple for loop would work, you could put it in a function called ResetArr(ArrayName)
1
2
3
4
5
void ResetArr(ArrayName){
    for(int i = 0; i < 100; i++){
         file[i] = " ";
    }
}
Apr 10, 2012 at 2:47pm
I don't think you have to reset the string here. cin >> file; doesn't care if there is old garbage in the string.

If you really want to reset the string you just have to set the first character in the string to '\0'. file[0] = '\0';
Apr 10, 2012 at 3:13pm
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] = ' ';
}
}

Apr 10, 2012 at 3:29pm
I also tried it the way Peter87 mentioned, without the function. It file still fails on the second loop.
Apr 10, 2012 at 3:39pm
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'.
Apr 10, 2012 at 3:46pm
try goto and cin.ignore may help. The system("PAUSE"); can be replaced by getch(); .
Last edited on Apr 10, 2012 at 3:47pm
Apr 10, 2012 at 4:34pm
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

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);
              }
}
Last edited on Apr 10, 2012 at 4:37pm
Topic archived. No new replies allowed.