ofstream, writing to files ?

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

"4 minus 2 equals 2", etc.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  //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>
using namespace 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.
I changed a few things, and it appears to work now. Complies, run, closes. I even found the .txt file it created. I'm excited.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//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>
using namespace 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;
}


Topic archived. No new replies allowed.