C++ Project Help

The last thing I need to make happen with this project is get the chosen planet name to be outputted. Since I did a switch statement I'm not sure how to make this happen. I can make it display 1-8, but not the actual name of the planet. Any help would be appreciated.

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
#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 showResults(char[50], double, int, int, int, double);

//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 ()
{	
 char name [50];	// to hold 50 characters of user's name
 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
 int planetName;

// set precision of answer to two decimal places
 cout << fixed << showpoint << setprecision(2);
 // while loop to restart program if user answers 'y' or 'Y'
 while (restart == 'y' || restart == 'Y')
 {
 	//clear screen when restarting program
		system ("cls");
	//greet user and get name
cout << "Greetings, space traveler.  What is your first name? \n";
cin >> name;
	  //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;
  }
  //continue program if user does not wish to quit
  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;
   
   // switch menu choice to relavent planet 
   switch (choice)
   {
    case Mercury:
     showResults (name, gravityMercury, currentWeight, planetName, distanceMercury, speed);
     break;
    case Venus:
     showResults (name, gravityVenus, currentWeight, planetName, distanceVenus, speed);
     break;
    case Earth:
     showResults (name, gravityEarth, currentWeight, planetName, distanceEarth, speed);
     break;
    case Mars:
     showResults (name, gravityMars, currentWeight, planetName, distanceMars, speed);
     break;
    case Jupiter:
     showResults (name, gravityJupiter, currentWeight, planetName, distanceJupiter, speed);
     break;
    case Saturn:
     showResults (name, gravitySaturn, currentWeight, planetName, distanceSaturn, speed);
     break;
    case Uranus:
     showResults (name, gravityUranus, currentWeight, planetName, distanceUranus, speed);
     break;
    case Neptune:
     showResults (name, gravityNeptune, currentWeight, planetName, distanceNeptune, speed);
     break;
    default:
     cout << "Invalid Entry.  Please enter a number between 1 and 8.\n\n";
	}

}
// ask user to restart program
	cout << "Go again? (y/n)" << endl;
	cin >> restart; // change control variable
	}
 // end while loop
 system ("cls");
   cout << "The End";
   

 return 0;
 

}
// menu function
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";
}

//results function
void showResults (char name[50], double planet, int currentWeight, int planetName, int distance, double speed)
{
	ofstream outputFile;
	outputFile.open("E-ticket.txt");
	outputFile << "Name: " << name << endl;
	outputFile << "Weight on Earth: " << currentWeight << " lbs" << endl;
	outputFile << "Planet Destination: " << planetName << endl;
	outputFile << "Weight on Planet: " << (planet * currentWeight) << " lbs" << endl;
	outputFile << "Travel Time: " << (distance / speed) << " million hours" << endl;
	outputFile.close();
}
Last edited on
You mean display on screen?

Aceix.
Yes I need to display the planet name next to Planet Destination: in the output file.
Check the function signature of showResults. You are passing type int instead of type const char

Aceix.
The model now with const char was an attempt to have it show the planet name. I used int before, but it would only show the number 1-8 corresponding to the planet.
I updated the code, but now it just outputs "Planet Destination: 0"
Call the function this way(repeat for all cases):
showResults (name, gravityNeptune, currentWeight, choice, distanceNeptune, speed);

Then in the showResult function, use a switch or a couple of ifs, to convert the planetName:int parameter to string.
eg(repeat for all):
1
2
3
4
string str;
if(planetName==1)
  str="Mercury";
outputFile<<"Planet Destination: "<<str;


Aceix.
Last edited on
Topic archived. No new replies allowed.