HEllo there, pretty much a rookie. Learning C++ for a required class. I'm having a very hard time with ofstream. I want to write the results of a looped statement to a file, then close the program once it has finished.
How does one know that the file is actually being written? If its a .txt file, will there be a .txt somewhere in my documents once its done? I also am not sure if it is working or not in my program....
Here is what I have written. It seems that everything I want to work works like i want it to. Just not sure about the fstream commands.
here is what the assignment instructs if I am not making sense.
" Write a main () program that creates an output text file called "outputSubtraction.txt". THe program will run until the user decides to quit (utilize a loop and menu choice for this). the program asks the user to enter two numbers from 1-3000 as "num1" and "num2", then performs the subtraction of num1 and num2 and outputs the result into the text file. the output should be in the form
//This program asks user for two integers
//between 1 and 3000. It then subtracts
//num2 from num1, then prints it to a file
//called "outputSubtraction.txt". It stays
//open until user ends with menu option,
//or until the results are written to the file.
#include <iostream>
#include <fstream>
usingnamespace std;
int main ()
{
//declaring the variables
double num1=0, num2=0, num3=0;
int x=0;
//promting for user input
cout<<"Enter two numbers from 1-3000"<<endl;
//gives option to end
cout<<"Press -- x -- to end"<<endl;
//user input
cin>>num1;
cin>>num2;
//loop start
//this gives an error is anything less than one
//or greater than 3000 is entered
while (num1, num2 < 1 && num1, num2 > 3000)
{
cout<<"ERROR: please enter numbers between\n";
cout<<"ONE and THREE THOUSAND ya' big dummy.\n";
cin>>num1;
cin>>num2;
}
//loop continues, doing the calculation for subtraction
while (num1, num2 > 1 && num1, num2 < 3000)
{
double num3 = num1-num2; //the subtraction
//prints the subtraction
cout<<"Your subtraction is:"" \n";
cout<<num1<<" ""minus"" "<<num2<<" ""equals"" "<<num3<<" ""!\n"<<endl;
//creates file
ofstream fout;
ofstream ofs ("outputSubtraction.txt");
//gives option to exit once finished
cout<<"Press -- x -- to end"<<endl;
cin>>x;
exit(x); //exits program with "x"
cout<<"\n";
//prints to the file
ofs << num1 << "minus" << num2 << " equals" << num3<<"/n";
ofs.close();
//stop loop
break;
}
return 0;
}
The file that is written to can be found in the working directory of the program (generally the same folder as either the programs executable or the IDE's project file). Also, you don't need to close the file on line 64, and you aren't using the object you create on line 51.
Your while statement is unnecessary, and you can't use the comma operator as you are using it in your if statements - you'll need to test each variable individually using the && operator.
//This program asks user for two integers
//between 1 and 3000. It then subtracts
//num2 from num1, then prints it to a file
//called "outputSubtraction.txt". It stays
//open until user ends with menu option,
//or until the results are written to the file.
#include <iostream>
#include <fstream>
usingnamespace std;
int main ()
{
//creates file
ofstream file;
file.open ("outputSubtraction.txt");
//declaring the variables
double num1=0, num2=0, num3=0;
int x=0;
//prompting for user input
cout<<"Enter two numbers from 1-3000"<<endl;
//gives option to end
cout<<"Press -- x -- to end"<<endl;
//user input
cin>>num1;
cin>>num2;
//loop start
//this gives an error is anything less than one
//or greater than 3000 is entered
while (num1, num2 < 1 && num1, num2 > 3000)
{
cout<<"ERROR: please enter numbers between\n";
cout<<"ONE and THREE THOUSAND ya' big dummy.\n";
cin>>num1;
cin>>num2;
}
//loop continues, doing the calculation for subtraction
while (num1, num2 > 1 && num1, num2 < 3000)
{
double num3 = num1-num2; //the subtraction
//prints the subtraction
cout<<"Your subtraction is:"" \n";
cout<<num1<<" ""minus"" "<<num2<<" ""equals"" "<<num3<<" ""!\n"<<endl;
//prints to the file
file<< num1 <<" ""minus"" "<< num2 <<" "" equals"" " << num3<<" ""\n";
file.close();
//gives option to exit once finished
cout<<"Press -- x -- to end"<<endl;
cin>>x;
exit(x); //exits program with "x"
cout<<"\n";
//stop loop
break;
}
return 0;
}