NEED HELP URGENT

Hey guys,

I need help with this program I've been working on for the past hour. I'm trying to make it loop(with a sentinel value to end the program), and when I enter a 9 for the planet code, cout an error message. But every time I try to do this, It goes straight to the error message. Here's the base code. The program is supposed to show what your weight would be on one of these planets.

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
//declaring variables and arrays
double planet[8] = {0.5,0.6 ,0.7, 0.8,0.9,0.1,0.2,0.3};
int weight = 0;
string name = "";
double answer = 0.0;
int planetNum= 0;

cout << "1- Mars, 2-Earth, 3-Mercury, 4-Venus, 5-Jupiter, 6-Pluto, 7-Uranus, 8-Saturn" << endl;

cout << "What planet code would you like to use: ";
cin >> planetNum;
cout << "What is your weight: ";
cin >> weight;
cout << "What is your name>: ";
cin >> name;

switch (planetNum)
{
case 1:
answer = weight * planet[0];
cout << name << " ,your weight on " << planetNum << " is: " << answer << endl;
break;
case 2:
answer = weight * planet[1];
cout << name << " ,your weight on " << planetNum << " is: " << answer << endl;
break;
case 3:
answer = weight * planet[2];
cout << name << " ,your weight on " << planetNum << " is: " << answer << endl;
break;
case 4:
answer = weight * planet [3];
cout << name << " ,your weight on " << planetNum << " is: " << answer << endl;
break;
case 5:
answer = weight * planet[4];
cout << name << " ,your weight on " << planetNum << " is: " << answer << endl;
break;
case 6:
answer = weight * planet[5];
cout << name << " ,your weight on " << planetNum << " is: " << answer << endl;
break;
case 7:
answer = weight * planet[6];
cout << name << " ,your weight on " << planetNum << " is: " << answer << endl;
break;
case 8:
answer = weight * planet[7];
cout << name << " ,your weight on " << planetNum << " is: " << answer << endl;
break;
default:
} //end switch


system("pause");
return 0;
} //end of main function
First of all, there is nothing in your "default" case. So when you hit 9 there is nothing for it do. Try putting a cout in your "default" case.

I'm trying to make it loop(with a sentinel value to end the program)

I don't know what you mean by a sentinel value or how you want it to loop, but a do-while loop would seem most appropriate to me.

Also, take a look at this website. :)
http://www.catb.org/~esr/faqs/smart-questions.html#code
By sentinel value, I mean if I type -1, the program will end. And how would I implement a 'do while'. I'm new to coding :/

Thank you!
Try this:

Add a planetName array, to store the names of the planets so you can output planet name according to each case.
 
string planetName[8] = { "Mars", "Earth", "Mercury", "Venus", "Jupiter", "Pluto", "Uranus", "Saturn" };


Also define repeat variable for loop that will repeat the program until specified:
 
bool repeat = true;


and create a while (repeat == true) loop to enclose the rest of the code, and create an escape conditional somewhere after cin >> planetNum to determine if the program should escape when 9 is entered.

Try this:
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
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
//declaring variables and arrays
double planetCalc[8] = {0.5,0.6 ,0.7, 0.8,0.9,0.1,0.2,0.3};
string planetName[8] = { "Mars", "Earth", "Mercury", "Venus", "Jupiter", "Pluto", "Uranus", "Saturn" };
int weight = 0;
string name = "";
double answer = 0.0;
int planetNum= 0;
bool repeat = true;

while (repeat == true) {

		cout << "1- Mars, 2-Earth, 3-Mercury, 4-Venus, 5-Jupiter, 6-Pluto, 7-Uranus, 8-Saturn -- Enter -1 to Quit" << endl;
		cout << "What planet code would you like to use: ";
		cin >> planetNum;

		if (planetNum == -1){
			repeat = false;
			break;
		}

		cout << "What is your weight: ";
		cin >> weight;
		cout << "What is your name>: ";
		cin >> name;

		switch (planetNum)
		{
		case 1:
			answer = weight * planetCalc[0];
			cout << name << " ,your weight on " << planetName[planetNum-1] << " is: " << answer << endl;
			break;
		case 2:
			answer = weight * planetCalc[1];
			cout << name << " ,your weight on " << planetName[planetNum-1] << " is: " << answer << endl;
			break;
		case 3:
			answer = weight * planetCalc[2];
			cout << name << " ,your weight on " << planetName[planetNum-1] << " is: " << answer << endl;
			break;
		case 4:
			answer = weight * planetCalc [3];
			cout << name << " ,your weight on " << planetName[planetNum-1] << " is: " << answer << endl;
			break;
		case 5:
			answer = weight * planetCalc[4];
			cout << name << " ,your weight on " << planetName[planetNum-1] << " is: " << answer << endl;
			break;
		case 6:
			answer = weight * planetCalc[5];
			cout << name << " ,your weight on " << planetName[planetNum-1] << " is: " << answer << endl;
			break;
		case 7:
			answer = weight * planetCalc[6];
			cout << name << " ,your weight on " << planetName[planetNum-1] << " is: " << answer << endl;
			break;
		case 8:
			answer = weight * planetCalc[7];
			cout << name << " ,your weight on " << planetName[planetNum-1] << " is: " << answer << endl;
			break;
		case 9:
			cout << "unnecessary error msg" << endl;
		default:
			cout << "You didn't select a valid option" << endl;
		} //end switch
}


system("pause");
return 0;
} //end of main function
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main ()
{
  unsigned long n;
  do {
    cout << "Enter number (0 to end): ";
    cin >> n;
    cout << "You entered: " << n << "\n";
  } while (n != 0);
  return 0;
}


Here's a handy example code of a do-while loop. Copied directly from the tutorials on this website found in Documentation -> C++ Tutorials. :)

Basically, this loop will execute over and over as long as the condition at the bottom of the loop is satisfied. What is significant about this loop is the fact that the condition is checked at the end of the loop. This means that the code in the loop will always be ran AT LEAST once.

Hope this helps.

OH! The easiest way to achieve the sentinel value is to just put an if statement to check it right before the switch.
Last edited on
It keeps giving me errors around the strings and 'cout' statements...Even though I added #include <iostream>, and #include <string> :/
For which code? If it's for mine, try removing the #include "stdafx.h"; if your not using visual c++. I updated my code to use -1 as exit as well.
Last edited on
1
2
3
4
if (planetNum == -1){
    repeat = false;
    break;
}


The repeat = false is completely unecessary. The break statement will successfully yank the program from the loop, so you don't even need the boolean variable to exist.

1
2
3
4
5
do 
{
    if (planetNum == -1)
        break;
} while (true);

will suffice.
Last edited on
Yeah your right.

This is it then:
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
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
//declaring variables and arrays
double planetCalc[8] = {0.5,0.6 ,0.7, 0.8,0.9,0.1,0.2,0.3};
string planetName[8] = { "Mars", "Earth", "Mercury", "Venus", "Jupiter", "Pluto", "Uranus", "Saturn" };
int weight = 0;
string name = "";
double answer = 0.0;
int planetNum= 0;

	while (true) {

		cout << "1- Mars, 2-Earth, 3-Mercury, 4-Venus, 5-Jupiter, 6-Pluto, 7-Uranus, 8-Saturn -- Enter 9 to Quit" << endl;
		cout << "What planet code would you like to use: ";
		cin >> planetNum;

		if (planetNum == -1){
			break;
		}

		cout << "What is your weight: ";
		cin >> weight;
		cout << "What is your name>: ";
		cin >> name;

		switch (planetNum)
		{
		case 1:
			answer = weight * planetCalc[0];
			cout << name << " ,your weight on " << planetName[planetNum-1] << " is: " << answer << endl;
			break;
		case 2:
			answer = weight * planetCalc[1];
			cout << name << " ,your weight on " << planetName[planetNum-1] << " is: " << answer << endl;
			break;
		case 3:
			answer = weight * planetCalc[2];
			cout << name << " ,your weight on " << planetName[planetNum-1] << " is: " << answer << endl;
			break;
		case 4:
			answer = weight * planetCalc [3];
			cout << name << " ,your weight on " << planetName[planetNum-1] << " is: " << answer << endl;
			break;
		case 5:
			answer = weight * planetCalc[4];
			cout << name << " ,your weight on " << planetName[planetNum-1] << " is: " << answer << endl;
			break;
		case 6:
			answer = weight * planetCalc[5];
			cout << name << " ,your weight on " << planetName[planetNum-1] << " is: " << answer << endl;
			break;
		case 7:
			answer = weight * planetCalc[6];
			cout << name << " ,your weight on " << planetName[planetNum-1] << " is: " << answer << endl;
			break;
		case 8:
			answer = weight * planetCalc[7];
			cout << name << " ,your weight on " << planetName[planetNum-1] << " is: " << answer << endl;
			break;
		case 9:
			cout << "Unecessary error msg" << endl;
			break;
		default:
			cout << "You didn't select a valid option" << endl;
		} //end switch
}


system("pause");
return 0;
} //end of main function
Last edited on
@Barneszy: What's the error?
Thanks guys! I got the program to work. You all were a big help!

*I really need to read over my text book haha
@Tresky, it was just an error with the varriables/array, but I got it to work
No problem. Read that text book and use Google. Google is your best friend. If you're experiencing a problem, chances are... someones had it before. Haha.

And here are some tips on getting good results on forums. It works, I promise, so take a look.
http://www.catb.org/~esr/faqs/smart-questions.html
Topic archived. No new replies allowed.