Running Program Again??

Hi everyone,
I created the code below in response to an assignment I had, the code runs fine and its good to go as far as the assignment goes. However, I was curious as to how would I get the program to keep running. For instance, I put in my weight and planet and I get my answer. Great but how do I do that again without closing the window and running a new window or (debugger)??
I've looked online and found several ways but none worked, just trying to get up my code experience and try new things.
Thanks for the help.

Code is running in Microsoft Visual Studio 2017.

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
  #include <iostream>
#include <string>
#include <cstring>
#include <istream>


using namespace std;


// Global declarations: Constants and type definitions only -- no variables 
enum planetType {Mercury, Venus, Earth, Moon, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto};

// Function Prototypes 
void GetUserInput (float &weight, string &planet)
{
	cout << "Enter You Weight: " << endl;
	cin >> weight;

	cout << "Enter Name of You wish to Check [Mercury, Venus, Earth, Moon, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto] : " << endl;
	cin >> planet;
}


planetType ConvertInputToPlanetType(string planet) {
	planetType p;

	if (planet.compare("Mercury") == 0 || planet.compare("mercury") == 0) {
		p = Mercury;
	}
	if (planet.compare("Venus") == 0 || planet.compare("venus") == 0) {
		p = Venus;
	}
	if (planet.compare("Earth") == 0 || planet.compare("earth") == 0) {
		p = Earth;
	}
	if (planet.compare("Moon") == 0 || planet.compare("moon") == 0) {
		p = Moon;
	}
	if (planet.compare("Mars") == 0 || planet.compare("mars") == 0) {
		p = Mars;
	}
	if (planet.compare("Jupiter") == 0 || planet.compare("jupiter") == 0) {
		p = Jupiter;
	}
	if (planet.compare("Saturn") == 0 || planet.compare("saturn") == 0) {
		p = Saturn;
	}
	if (planet.compare("Uranus") == 0 || planet.compare("uranus") == 0) {
		p = Uranus;
	}
	if (planet.compare("Neptune") == 0 || planet.compare("neptune") == 0) {
		p = Neptune;
	}
	if (planet.compare("Pluto") == 0 || planet.compare("pluto") == 0) {
		p = Pluto;
	}

	return p;
}


void OutputWeight(planetType p, float weight) {
	switch (p) {
	case Mercury:
		cout << "Your weight on planet Mercury would be " << (weight * 0.4155);
		break;
	case Venus:
		cout << "Your weight on planet Venus would be " << (weight * 0.8975);
		break;
	case Earth:
		cout << "Your weight on planet Earth would be " << (weight * 1.0);
		break;
	case Moon:
		cout << "Your weight on planet Moon would be " << (weight * 0.166);
		break;
	case Mars:
		cout << "Your weight on planet Mars would be " << (weight * 0.3507);
		break;
	case Jupiter:
		cout << "Your weight on planet Jupiter would be " << (weight * 2.5374);
		break;
	case Saturn:
		cout << "Your weight on planet Saturn would be " << (weight * 1.067);
		break;
	case Uranus:
		cout << "Your weight on planet Uranus would be " << (weight * 0.8947);
		break;
	case Neptune:
		cout << "Your weight on planet Neptune would be " << (weight * 1.1794);
		break;
	case Pluto:
		cout << "Your weight on planet Pluto would be " << (weight * 0.0899);
	}
}

int main()
{
	// In cout statment below SUBSTITUTE your name and lab number
	cout << "" << endl;

	// Variable Declarations
	float weight;
	string planet;
	planetType p;
	
	// Program Logic
	GetUserInput(weight, planet);
	if (!(planet.compare("Mercury") == 0 || planet.compare("mercury") == 0 ||
		planet.compare("Venus") == 0 || planet.compare("venus") == 0 ||
		planet.compare("Earth") == 0 || planet.compare("earth") == 0 ||
		planet.compare("Moon") == 0 || planet.compare("moon") == 0 ||
		planet.compare("Mars") == 0 || planet.compare("mars") == 0 ||
		planet.compare("Jupiter") == 0 || planet.compare("jupiter") == 0 ||
		planet.compare("Saturn") == 0 || planet.compare("saturn") == 0 ||
		planet.compare("Uranus") == 0 || planet.compare("uranus") == 0 ||
		planet.compare("Neptune") == 0 || planet.compare("neptune") == 0 ||
		planet.compare("Pluto") == 0 || planet.compare("pluto") == 0)) {
		cout << "Error : planet name should be one of [Mercury, Venus, Earth, Moon, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto] : " << endl;
	}
	else {
		
		p = ConvertInputToPlanetType(planet);

	
		OutputWeight(p, weight);
	}
	


	// Closing Program Statements 
	system("Pause");

	return 0;

}
put a do-while loop around most of main, end before the return 0 though that will end the program when it hits the return.

do
{
... your code

//add
cout do you want to go again
cin answer
} while(answer == 'y') //whatever

return 0;
All your checks in main are duplicated in ConvertInputToPlanetType.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
enum planetType {Unknown, Mercury, Venus, Earth, Moon, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto};

planetType ConvertInputToPlanetType(string planet) {
	planetType p = Unknown;
... etc

int main()
{
	float weight;
	string planet;
	planetType p;
	
	// Program Logic
	GetUserInput(weight, planet);
	p = ConvertInputToPlanetType(planet);
	if ( p == Unknown ) {
		cout << "Error : planet name should be one of "
			"[Mercury, Venus, Earth, Moon, Mars, "
			"Jupiter, Saturn, Uranus, Neptune, Pluto] : " << endl;
	} else {
		OutputWeight(p, weight);
	}

Consider something like C++17:

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
#include <iostream>
#include <string>
#include <cctype>

using namespace std;

enum PlanetType { Bad = -1, Mercury = 0, Venus, Earth, Moon, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto, MAXPLAN };

struct Names {
	string name;
	double conv {};
};

const Names planets[10] {{"Mercury", 0.4155}, {"Venus", 0.8975}, {"Earth", 1}, {"Moon", 0.166}, {"Mars", 0.3507}, {"Jupiter", 2.5375}, {"Saturn", 1.067},
	{"Uranus", 0.8947}, {"Neptune", 1.1794}, {"Pluto", 0.0899}};

// Display the planets
void disNames()
{
	cout << "[ ";
	for (const auto& p : planets)
		cout << p.name << ' ';

	cout << "]";
}

void GetUserInput(double& weight, string& planet)
{
	// Make sure only number entered
	while ((cout << "Enter You Weight: ") && !(cin >> weight)) {
		cout << "Invalid number\n";
		cin.clear();
		cin.ignore(1000, '\n');
	}

	cout << "Enter Name of You wish to Check ";
	disNames();
	cout << " : ";
	cin >> planet;
	cin.ignore(1000, '\n');

	// Convert input to Capitalised lower case
	if (!planet.empty())
		for (auto& ch : planet)
			ch = (char)std::tolower(ch);

	planet.front() = (char)std::toupper(planet.front());
}

void OutputWeight(PlanetType p, double weight)
{
	cout << "Your weight on planet " << planets[p].name << " would be " << weight * planets[p].conv << '\n';
}

PlanetType fndPlanet(const string& planet)
{
	for (PlanetType i = Mercury; i < MAXPLAN; i = (PlanetType)(i + 1))	// To increment an enum type
		if (planets[i].name == planet)
			return i;

	return Bad;
}

int main()
{
	char ans {};

	do {
		double weight {};
		string planet;

		GetUserInput(weight, planet);

		if (auto p = fndPlanet(planet); p != Bad)
			OutputWeight(p, weight);
		else {
			cout << "Error : planet name should be one of ";
			disNames();
			cout << '\n';
		}

		cout << "Again? (y/n): ";
		cin >> ans;
		cin.ignore(1000, '\n');
	} while (ans == 'y' || ans == 'Y');
}


using a struct to associate planet name with its conversion factor.
Last edited on
Topic archived. No new replies allowed.