Ofstream not creating file

I am working on a project. I believe I have fulfilled all of the requirements, but my program does not create a file. I included an ofstream, but no file is created.

The requirements for this code are:

1. Add a switch statement while also including a menu for the user. It should include info for each planet. Also validate the menu input with relevant messages to the user.

2. Add a loop to ask the user to run the program again.

3. Write a function and pass all needed data for calculating weight on the selected planet. Then return that calculated value back and output in main ().

4. Write all of the output to a text file.

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <iostream>
#include <iomanip>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <fstream>
using namespace std;

// function prototype
void showMenu();
void showWeight(double, int);
void showTime (int, double, int);

//Constants for menu choices
 const int Mercury = 1,
           Venus = 2,
           Earth = 3,
           Mars = 4,
           Jupiter = 5,
           Saturn = 6,
           Uranus = 7,
           Neptune = 8,
           Quit = 9;
           
 //Constants for weight
 const double gravityMercury = 0.27,
              gravityVenus = .086,
              gravityEarth = 1.00,
              gravityMars = 0.37,
              gravityJupiter = 2.64,
              gravitySaturn = 1.17,
              gravityUranus = .092,
              gravityNeptune = 1.44;
              
 //Constants for distance
 const int distanceMercury = 93 - 36,
           distanceVenus = 93 - 67,
           distanceEarth = 93 - 93,
           distanceMars = 141 - 93,
           distanceJupiter = 483 - 93,
           distanceSaturn = 886 - 93,
           distanceUranus = 1782 - 93,
           distanceNeptune = 2793 - 93;

int main ()
{
	ofstream outputFile;
	outputFile.open("demofile.txt");
	
 int choice;      // to hold menu choice
 int currentWeight;  // to hold user weight
 double speed;   // to hold user speed
 float time;         // to hold user time
 char restart = 'Y';   // to restart program

 cout << fixed << showpoint << setprecision(2);
 while (restart == 'y' || restart == 'Y')
 {
		system ("cls");
		
	  //display the menu and get the user's choice
  showMenu();
  cin >> choice;
  
  //validate menu selection
  while ( choice < 1 || choice > 9)
  {
   cout << "Please enter a valid menu choice between 1 and 9: ";
   cin >> choice;
  }
  
  if (choice != Quit)
  {
   // get weight
   cout << "How much do you weight in lbs?\n";
   cin >> currentWeight;
   
   // get speed
   cout << "How fast would you like to travel (mph)?\n";
   cin >> speed;
   
   // display new weights
   switch (choice)
   {
    case Mercury:
     showWeight (gravityMercury, currentWeight);
     showTime (distanceMercury, speed, time);
     break;
    case Venus:
     showWeight (gravityVenus, currentWeight);
     showTime (distanceVenus, speed, time);
     break;
    case Earth:
     showWeight (gravityEarth, currentWeight);
     showTime (distanceEarth, speed, time);
     break;
    case Mars:
     showWeight (gravityMars, currentWeight);
     showTime (distanceMars, speed, time);
     break;
    case Jupiter:
     showWeight (gravityJupiter, currentWeight);
     showTime (distanceJupiter, speed, time);
     break;
    case Saturn:
     showWeight (gravitySaturn, currentWeight);
     showTime (distanceSaturn, speed, time);
     break;
    case Uranus:
     showWeight (gravityUranus, currentWeight);
     showTime (distanceUranus, speed, time);
     break;
    case Neptune:
     showWeight (gravityNeptune, currentWeight);
     showTime (distanceNeptune, speed, time);
     break;
    default:
     cout << "Invalid Entry.  Please enter a number between 1 and 8.\n\n";
	}
}
	cout << "Go again? (y/n)" << endl;
	cin >> restart; // change control variable
	}
 // end while loop
 system ("cls");
   cout << "The End";

 return 0;
}

void showMenu()
{
 cout << "To which planet will you be traveling? \n" << endl;
 cout << "1. Mercury \n";
 cout << "2. Venus \n";
 cout << "3. Earth \n";
 cout << "4. Mars \n";
 cout << "5. Jupiter \n";
 cout << "6. Saturn \n";
 cout << "7. Uranus \n";
 cout << "8. Neptune \n";
 cout << "9. Quit Program\n" << endl;
 cout << "Please enter a choice between 1 and 9. \n";
}

void showWeight (double planet, int currentWeight)
{
	ofstream outputFile;
	outputFile.open("demofile.txt");
 outputFile << "Your total weight is "
   << (planet * currentWeight) << endl;
   outputFile.close();
}

void showTime (int distance, double speed, int time)
{
	ofstream outputFile;
	outputFile.open("demofile.txt");
 outputFile << "Your total time is "
      << (distance / speed) << endl;
      outputFile.close();
}
Last edited on
Post the code in code tag ("<>" from the toolbar on right)

Where is your question?
Morning,
Is this the code you've been given to modify, or is this code you've written? It doesn't make complete sense at the moment.
I edited my code a bit more and updated my question. Thanks for the responses.

This is a code I have written. I apologize if it doesn't make much sense. I'm pretty new at this.
Last edited on
For problem one, you solved it and then added few unnecessary loops.
To fix; get rid of loop at line 56, get rid of line 65-70, change line 117 to read "...between 1 and 9" instead of "...between 1 and 8", get rid of lines 124-129, get rid of function showRestart.

For problem two read on out file streams http://www.cplusplus.com/reference/fstream/ofstream/ofstream/
The example should get you started
Thank you for the comments. I feel like lines 65-70 are necessary to validate the input. You are correct about the other lines needing to be deleted.

Thanks for the link. I am having trouble with the fstream outputting.
This version loops the program, but I need to make it an option after completing it once. I'm trying to make a counter that asks the user to restart once counter goes from 0 to 1.
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
 //Constants for menu choices
 const int Mercury = 1,
           Venus = 2,
           Earth = 3,
           Mars = 4,
           Jupiter = 5,
           Saturn = 6,
           Uranus = 7,
           Neptune = 8,
           Quit = 9;
           
 //Constants for weight
 const double gravityMercury = 0.27,
              gravityVenus = .086,
              gravityEarth = 1.00,
              gravityMars = 0.37,
              gravityJupiter = 2.64,
              gravitySaturn = 1.17,
              gravityUranus = .092,
              gravityNeptune = 1.44;
              
 //Constants for distance
 const int distanceMercury = 93 - 36,
           distanceVenus = 93 - 67,
           distanceEarth = 93 - 93,
           distanceMars = 141 - 93,
           distanceJupiter = 483 - 93,
           distanceSaturn = 886 - 93,
           distanceUranus = 1782 - 93,
           distanceNeptune = 2793 - 93;
...
    case Mercury:
     showWeight (gravityMercury, currentWeight);
     showTime (distanceMercury, speed, time);
     break;
    case Venus:
     showWeight (gravityVenus, currentWeight);
     showTime (distanceVenus, speed, time);
     break;
    case Earth:
     showWeight (gravityEarth, currentWeight);
     showTime (distanceEarth, speed, time);
     break;
    case Mars:
     showWeight (gravityMars, currentWeight);
     showTime (distanceMars, speed, time);
     break;
    case Jupiter:
     showWeight (gravityJupiter, currentWeight);
     showTime (distanceJupiter, speed, time);
     break;
    case Saturn:
     showWeight (gravitySaturn, currentWeight);
     showTime (distanceSaturn, speed, time);
     break;
    case Uranus:
     showWeight (gravityUranus, currentWeight);
     showTime (distanceUranus, speed, time);
     break;
    case Neptune:
     showWeight (gravityNeptune, currentWeight);
     showTime (distanceNeptune, speed, time);


You may want to consider using arrays.
Ok. I just learned about arrays. I'll see if I can implement them. Will they make it easier to have a restart loop?
They will make it a lot easier and then you can do something like
1
2
showWeight(gravity[planet], currentWeight);
showTime(distance[planet], speed, time);
instead of the switch.
That does seem quite a bit simpler. Thanks.

Any ideas what's wrong with my restart loop. Whenever I putwhile count =1 at the start of the function the menu wouldn't show up again.
I believe I have completed all requirements except for the ofstream output to a file. I'm not sure why it's not working. I am running Windows 7 if it matters. Maybe if someone could test this code out on their computer to see if it works.
Topic archived. No new replies allowed.