Files

Hello, I have written a program that I need a user to put in as many double values as they want, and then it saves those values to a file. I am stuck with what I have, the program gets the values but wont execute. What is wrong with the code?
#include <iostream>
#include <fstream>
#include <iomanip>
# include <string>

using namespace std;

int main ()
{
string end;
double UserInput = 0;
while(UserInput != 'end')
{
cin >> UserInput;
}
while(UserInput == 'end')
{
ofstream myfile ("example.txt");
if (myfile.is_open())
{
myfile << UserInput << endl;
myfile.close();
}
else cout << "Unable to open file";
}
cout << endl;
system("pause");
return 0;

}
Thanks!
double UserInput

UserInput is a double value, and you then try to compare it to the multi-character constant 'end'.
Ok, so I changed that so that when they enter 1 it continues, however it was the same result.
Just a few things that come to mind.. What were you planning to do with the string end? You declare it but do nothing with it. Also, the first loop is going to be infinite because UserInput is a double, not a string like you check it against (which should be in double quotes by the way). Maybe you could have a terminating value, like -1 or something. Better yet, since it's a double which usually shouldn't be compared with a '==', you could tell the user to input anything less than -100000, or any number you want. Next, if the first loop ever meets a terminating condition, the second one would be infinite, since the value of UserInput is never changed. You shouldget rid of the second while loop, put all the contents into the first loop, not close the file, and move the ofstream statement to the beginning of main().

EDIT: sorry didn't see the reply already
Last edited on
1
2
3
4
5
6
7
8
9
10
while(UserInput == 1) // used to be while(UserInput == 'end')
{
  ofstream myfile ("example.txt");
  if (myfile.is_open())
    {
      myfile << UserInput << endl;
      myfile.close();
    }
  else cout << "Unable to open file";
} 


So when will this while loop exit? Why does this while loop even exist?

If you want to store more than one value, you're going to have to store the input values. At the moment, you only store one input value.
Last edited on
Yeah, thats where i am stuck, on the code to do that. I got the program to work and it just saves the last values entered.
That's because you only have one variable for storing numbers.

double UserInput = 0; Creates a single variable called UserInput

cin >> UserInput; Writes the entered value into the variable UserInput

You only have one variable, and you always put the entered value into that variable (which will overwrite the previous value stored there).

You either need to store all the variables the user enters (perhaps in some kind of array, or since this is C++ maybe in a proper grown-up container like a vector), or write each entered value to file as they come in, so that it doesn't matter that you keep no record of them.
Last edited on
So how would I write each value when they come in? Really lost on this, sorry.
No worries. I think the real problem here is that you started coding before you actually worked out what the task is and how to solve it.

So, if you're going to write each value as it comes in, the algorithm will be something like:

- Get value from user
- Write value to file
- Repeat if needed

1
2
3
4
5
while (userInput != 1)
{
  cin >> userInput; // Get value from user
  myfile << UserInput << endl; // Write value to file
}


This will loop until the user enters 1, which is what you have chosen as your means for the user to indicate the final value to be entered.
Last edited on
Topic archived. No new replies allowed.