How to create invalid selection.

Im making a program that will find Area/Perimeters.

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
#include <iostream>
#include <string>
using namespace std;
/////SQUARE AREA FUNCTION/////
int Sqareaf (float height, float width){
float sqarea;
sqarea = height*width;
return(sqarea);
}

int main(){
	/////DECLARING VARIABLES/////
	string shape;
	string part;
	/////ENTRY TEXT/////
	cout << "Hello. Welcome to the geometry program.";
	cout << endl << "At any time you may type 'help' to see options";
	cout << endl << "Press return to continue";
	cin.get(); 
	/////ASKING FOR SHAPE/////
	cout << endl << "Please enter the shape we will be working with. -> ";
	getline (cin, shape);
	/////HELP FOR SHAPE SELECTION/////
	do {
	        cout << "You may enter:" << endl << "rectangle" << endl << "circle" << endl << "sphere" << endl << "cube " << endl;
	        cout << endl << "Please enter the shape we will be working with. -> ";
			getline (cin, shape);}
			while (shape=="help");
	
	/////SHAPE INVALID SELECTION/////



	/////NAVIGATING RECTANGLE//////
	if      (shape=="rectangle"){
		    cout << endl << "Would you like to know the 'area' or 'perimeter' of the rectangle? -> ";
		    getline (cin, part);
			/////RECTANGLE HELP/////
			do{ 
			   cout << "You may enter:" << endl << "perimeter" << endl << "area";
			   cout << endl << endl << "Would you like to know the 'area' or 'perimeter' of the rectangle? -> ";
		getline (cin, part);
			                            }while (part=="help");
			/////RECTANGLE PERIMETER/////
			 if(part=="perimeter"){
				
				                        float a;
	                                    float b;
	                                    int c;
	                                    cout << "Enter squares height -> ";
	                                    cin >> a;
	                                    cout << endl << "Enter squares width -> ";
	                                    cin >> b;
										c=(2*a)+(2*b);
										cout << endl << "The perimeter of your rectangle is " << c;
										cin >> c;
}
			 /////RECTANGLE AREA/////
			else if(part=="area"){
	                                    float a;
	                                    float b;
	                                    int c;
	                                    cout << "Enter squares height -> ";
	                                    cin >> a;
	                                    cout << "Enter squares width -> ";
	                                    cin >> b;
	                                    float sqarea;
	                                    sqarea = Sqareaf (a,b);
	                                    cout << "The area of the square is " << sqarea << ".";
	                                    cin >> c;}
			/////INVALID ENTRY FOR RECTANGLE/////



	/////IF CIRCLE IS THE CHOSEN SHAPE/////
	if (shape=="circle"){
		cout << "Would you like to know the circumference, or area of the circle? -> ";
		getline (cin,part);

		/////HELP FOR CIRCLE/////
	   if(part=="help")
		   do {
	             cout << "You may enter:" << endl << "area" << endl << "circumference";
	             cout << endl << "Would you like to know the circumference, or area of the circle? -> ";
	             getline (cin, part);
	             } while (part=="help");
	   /////CIRCLE AREA/////
	   else if(part=="area"){
		   				                float a;
	                                    int c;
	                                    cout << "Circles radius -> ";
	                                    cin >> a;
										c=3.14159*a*a;
										cout << endl << "The area of your circle is " << c;
										cin >> c;}
	   /////CIRCLE CIRCUMFERENCE/////
	   else if(part=="circumference"){
		                                float a;
										int c;
										cout << "Circles radius -> ";
										cin >> a;
										c=2*a*3.14159;
										cout << "Your circles circumference is " << c << ".";
	                                    cin >> c;
	   }



	/////CIRCLE INVALID ENTRY/////




}
}


I am however, not bein able to code in where if they do not input a valid selection, it will prompt for the entry again. You can see where I intend to place them in the /////INVALID ENTRY///// places in the code. Help me out please?
Move the input into a loop (preferably in its own function) and make sure it is repeated as long as no valid input was given.
Thanks. I was tinkering around with it and finally made it work. Ill post it so that if someone reads this with the same problem they can see it....



1
2
3
4
5
if (shape!="help" && shape!="rectangle" && shape!="circle"){
 do {cout << "That is an invalid selection.";
       cout << endl << "Please enter the shape we will be working with -> ";
       getline (cin, shape);}
     while (shape!="help" && shape!="rectangle" && shape!="circle");


Make sure this peice is the if statement. NOT the else if statements.

FWIW, if you find yourself copy/pasting code, you're doing it wrong.

In particular that code you just posted is redundant. It checks the same condition twice (once in an if and again in a do-while).

You could shorten it to this:

1
2
3
4
5
6
while (shape!="help" && shape!="rectangle" && shape!="circle")
{
    cout << "That is an invalid selection.";
    cout << endl << "Please enter the shape we will be working with -> ";
    getline (cin, shape);
}
Hm... never thought about trying that. Thanks. My logic just brought me to do what I posted. :p Well thanks for helping me make my code simpler!! (:
Topic archived. No new replies allowed.