dont laugh i just started this but y isnt this working

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
#include <iostream> 
#include <cmath>
using namespace std;
int main() 
{

char input; 
char choice; 
double a,b,c,d,e;  
for (;;) 

{ 

do 


{  
     cout << "\tCaculator Version- ID 001\n";
     cout << "\tThis Will caculate your basics\n";
     cout << "\tMy First Program\n";
     cout << "Opt. 1 = Addition\n";
     cout << "\n"; // produces a blank line
     cout << "Opt. 2 = Subtraction\n";
     cout << "\n";
     cout << "Opt. 3 = Multipulcation\n";
     cout << "\n";
     cout << "Opt. 4 = Division\n";
     
     
     
     
     
     cin >> choice; 
}
while (choice < '1' || choice > '4' && choice!= '1'); //d ont know
if (choice == 'Q')
system ("CLS");
switch (choice) 
{
case '1': // label
cout << "Input the number you want to add.\n";
cin >> a;
cout << "Input the number you want to add 'X' by.\n";
cin >> b; 
cout << "Your answer is:\t" << (a)+(b) << "\n"; 
system ("PAUSE");
system ("CLS");
break;
case '2':
cout << "Input the number you want to subtract.\n";
cin >> a;
cout << "Input the number you want to subtract x by"; 
cin >> b;
cout << "Your answer is\t" << (a-b) << "\n";
system ("PAUSE"); 
system ("CLS");
break;     
case '3':
cout << "Input the number you want to multiply.\n";
cin >> a;
cout << "Input the number you want to multiply x by.\n"; 
cin >> b;
cout << "Your answer is:\t" << (a)*(b) << "\n";
system ("PAUSE");
system ("CLS");
break;
case '4':
cout << "Input the number you want to divide.\n";
cin >> a;
cout << "Input the number you want to divide x by.\n"; 
cin >> b;
cout << "Your answer is:\t" << (a)/(b) << "\n";
system ("PAUSE");
system ("CLS");
break;     

case '5':
cout << "Opt. 1 for area of a square" << endl;
cin >> a;
cout << "Opt. 2 for area of a rectangle" << endl;
cin >> (input);
{
if (input == 1)
{  goto asquare;
} else if (input == 2)
{ goto arectangle;
} else if (input == false)
{ goto end;
}
asquare:
        cout << "Input the mesurement of one side of the square";
        cin >> a;
        cout << "The area of the square is:\t" << (a) * (a);
        system ("PAUSE");
        goto end; 
arectangle:
cout << "Input the length of the rectangle";
cin a;
cout << "Input the width of the rectangle";
cin b; 
cout << "The area of the rectangle is:\t" << (a) * (b);
system ("PAUSE")
goto end;
end: 
     exit(0)
}
}
return 0;
}
}
}
 
cin is used like this:

cin >> variable;
Not:
cin variable;

What's going on at line 35?

NEVER use the system ( ) function. Use cin.get ( ) instead of system ( "PAUSE" ), and there are alternative methods to using system ( "CLS" ) that I'll link you to in a bit.

Why the opening bracket at line 82?

At line 102, there should be a semicolon at the end.
At line 105, exit(0) should also have a semicolon.

The last 2 brackets aren't needed.



If you don't mind me asking, where are you learning C++ from?

And also, DON'T use goto...
Last edited on
Don't use goto or system statments.
don't use end or exit (0) either.
This program could be shortened quite a bit through the use of function calls.
umm im learning c++ from all over and i dont know how to do multiple menus without the goto function btw what is so bad about goto
Ugh, don't ask that question...

closed account (z05DSL3A)
what is so bad about goto

Nothing is bad about goto if it is used correctly. The use of any language construct is 'bad' if used incorrectly.

Most programmers see a goto and immediately have a spasm, because they are trained to, kind of like pavlov's dog[1].

[1]http://en.wikipedia.org/wiki/Classical_conditioning#Pavlov.27s_experiment
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
164
#include <iostream> 
#include <cmath> 
using namespace std; 
#define pi 3.14159265359
#define new '\n'
//line 9 is global: b=base c=base2 l=length w=width r=radius s=side (b&a will be used regular variables
int input; //global
int choice; //global 
double b,h,l,r,a,c,s,w,t;//global t=r2
int main()  
{
top:
     cout << "\tCaculator Version- ID 002" << new;
     cout << "\tThis Will caculate your basics" << new;
     cout << "\tMy First Program" << new;
     cout << "Opt. 1 = Addition" << new;
     cout << new; 
     cout << "Opt. 2 = Subtraction" << new;
     cout << new;
     cout << "Opt. 3 = Multipulcation" << new;
     cout << "\n";
     cout << "Opt. 4 = Division" << new;
     cout << new;
     cout << "Opt. 5 For area" << new;
     /* 
                  /\ /\ /\ /\ /\ 
                 MAIN MENU ABOVE 
     */
     
     
     cin >> choice ; 
   {
if (choice == 1)
{  goto a1;
} else if (choice == 2)
{ goto a2;
} else if (choice == 3)
{ goto a3;
} else if (choice == 4)
{  goto a4;
} else if (choice == 5)
{ goto a5;
}
a1: 
cout << "Input the number you want to add." << new;
cin >> a;
cout << "Input the number you want to add 'X' by." << new;
cin >> b; 
cout << "Your answer is:\t" << (a)+(b) << new; 
system ("PAUSE");
system ("CLS");
goto top;
a2:
cout << "Input the number you want to subtract. " << new;
cin >> a;
cout << "Input the number you want to subtract x by" << new; 
cin >> b;
cout << "Your answer is\t" << (a-b) << new;
system ("PAUSE"); 
system ("CLS");
goto top;     
a3:
cout << "Input the number you want to multiply." << new ;
cin >> a;
cout << "Input the number you want to multiply x by. " << new; 
cin >> b;
cout << "Your answer is:\t" << (a)*(b) << new;
system ("PAUSE");
system ("CLS");
goto top;
a4:
cout << "Input the number you want to divide. " << new;
cin >> a;
cout << "Input the number you want to divide x by." << new; 
cin >> b;
cout << "Your answer is:\t" << (a)/(b) << new;
system ("PAUSE");
system ("CLS");
goto top;     
/* 
   
   /\ /\ /\ /\ /\ /\ /\ /\
   BASIC MATH MENU ABOVE


   AREA MENU BELOW
\/ \/ \/ \/ \/ \/ \/
*/
a5:
cout << "Opt. 1 for area of a square" << new;
cout << new;
cout << "Opt. 2 for area of a rectangle" << new;
cin >> (input);
{
if (input == 1)
{  goto asquare;
} else if (input == 2)
{ goto arectangle;
} else if (input == 3
{ goto arectangle;
} else if (input == 4
{ goto arectangle;
} else if (input == 5
{ goto arectangle;
} else if (input == 6)
{ goto arectangle;
} else if (input == 7)
{ goto arectangle;
} 
asquare:
        cout << "Input the mesurement of one side of the square" << new;
        cin >> s; 
        cout << "The area of the square is:" << (a) * (a) << new;
        system ("PAUSE");
        system ("CLS");
        goto top; 
arectangle:
cout << "Input the length of the rectangle" << new;
cin >> a;
cout << "Input the width of the rectangle" << new;
cin >> b; 
cout << "The area of the rectangle is:" << (a) * (b) << new;
system ("PAUSE");
system ("CLS");
goto top;
aparallelogram:
cout << "Input the base of the parallelogram" << new;
cin >> b;
cout << "Input the hight of the parallelogram" << new;
cin >> h; 
cout << "The area of the parallelogram is:" << (b) * (h) << new;
system ("PAUSE");
system ("CLS");
atrapezoid:
cout << "Input the base of the " << new;
cin >> b;
cout << "Input base 2 of the parallelogram" << new;
cin >> c; 
cout << "Input the hight of the parallelogram" << new;
cin >> h;
cout << "The area of the parallelogram is:" << (h/2*(b+c)) << new;
system ("PAUSE");
system ("CLS");
atrapezoid:
cout << "Input the base of the trapezoid" << new;
cin >> b;
cout << "Input base 2 of the trapezoid" << new;
cin >> c; 
cout << "Input the hight of the trapezoid" << new;
cin >> h;
cout << "The area of the trapezoid is:" << (h/2*(b+c)) << new;
system ("PAUSE");
system ("CLS");
:acircle
cout << "Input the radius of the circle" << new;
cin >> r;
cout << "The area of the circle is:" << (pi*r*r) << new;
system ("PAUSE");
system ("CLS");
}
}
return 0;
}
 

umm i fixed it but i need to know how to add more to it like volume, surface area, and im going to add more shapes. my goal is by the end of the year make it do every thing in it can possibly do in algebra...
btw is there a maximum line limit for posting code
Last edited on
#define new '\n'
Whoa whoa whoa, please don't use #define, especially not to define a C++ keyword. For newlines you ought to just use std::endl as in std::cout<<"Hiya world"<<std::endl;

Might you be able to indent the code and get rid of some of the excess spaces? (Specifically, areas like lines 17~20.) Also, I don't think you need a goto top anywhere as you are already in an infinite loop (the for(;;) statement).
oh and how do u calculate exponents ]with user input variables as simple as can be. pref. in a cout statement like i was doing at the top with addition and subtraction
edit Zhuge ill try that but do you really need the std::cout and then the std::endl in the line
Last edited on
I don't get what's wrong with defines. Or is that another thing that's only bad if used incorrectly? Although you're right about not using it with a keyword.

And no, since you have the using namespace std; code, you don't need the std::.
Last edited on
Gotos are generally a bad idea. They create what is called "spaghetti code". You should rarely use gotos. I've found only one reason so far to use gotos. In order to get rid of your gotos, you should put the code that you've put under "arectangle:" "atrapezoid:", etc into their own functions, and call those functions in the if statement. This way, the structure of the code is preserved, and it's more readable. Also, it looks like you're missing some parens around lines 99-103.

As for the "top:" goto, you don't need it. Just put the whole thing in a "while(true) {}". Indent & format your code better, it looks very messy. For example, this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if (input == 1)
{  goto asquare;
} else if (input == 2)
{ goto arectangle;
} else if (input == 3
{ goto arectangle;
} else if (input == 4
{ goto arectangle;
} else if (input == 5
{ goto arectangle;
} else if (input == 6)
{ goto arectangle;
} else if (input == 7)
{ goto arectangle;
}


Can be rewritten like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if (input == 1)
        goto asquare;
else if (input == 2)
        goto arectangle;
else if (input == 3
        goto arectangle;
else if (input == 4
        goto arectangle;
else if (input == 5
        goto arectangle;
else if (input == 6)
        goto arectangle;
else if (input == 7)
        goto arectangle;


See? Much more readable.

Nothing is bad about goto if it is used correctly. The use of any language construct is 'bad' if used incorrectly.

Most programmers see a goto and immediately have a spasm, because they are trained to, kind of like pavlov's dog[1].

[1]http://en.wikipedia.org/wiki/Classical_conditioning#Pavlov.27s_experiment


True, but it's not being used correctly. I prefer reading code from said spastic people than someone who uses gotos in place of basic program structure. I've actually had times when a goto was a good solution, but not often.
Topic archived. No new replies allowed.