Could someone check my code, its not working

Hi there I dont suppose anyones got anytime to just to go through my code and stop any mistakes, im trying to get it to compile but its not working and also when i pick an option in the menu, it for some reason doesnt want to go to it1

Thanks

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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <algorithm>

using namespace std;

int main()
{
   int choice;
   start:
   cout <<"Choose Option ";
   cin>>choice;

     int numb1;   
     int numb2;
	 int numb3;
     int sum;
	 int sum1;

	 int get_max(int a,int b,int c)
{
    int tmpVar = (b>c)?b:c;
    return (a>tmpVar)?a:tmpVar;
}

int get_min(int a,int b,int c)
{
    int tmpVar = (b<c)?b:c;
    return (a<tmpVar)?a:tmpVar;
}

int get_mid(int a,int b,int c)
{
    if(a==b&&a==c)
    return a;

    if(a==b||a==c||b==c)
    {
        if(a==b)
            return a;
        if(a==c)
            return a;
        if(b==c)
            return b;
    }

    if((a>b&&a<c)||(a<b&&a>c))
        return a;
    else if((b>a&&b<c)||(b<a&&b>c))
        return b;
    else return c;
}


{
    
    int Max
	int Min
	int Mid;

    //option 1
   if (choice ==1)

    
     cout << "Please Enter The first integer: ";
     cin >> numb1; 

     cout << "Please Enter The second integer: "; 
     cin >> numb2; 

	 cout << "Please Enter The third integer: "; 
     cin >> numb3; 
     sum = numb1 + numb2 + numb3; 

     cout << "And The Sum is " << sum << endl;
     
}

  
  cout << "OPTION 1"<<endl;


  //Option 2

  if ( choice ==2 )
  
	   cout << "Please Enter The first integer: ";
     cin >> numb1; 

     cout << "Please Enter The second integer: "; 
     cin >> numb2; 

	 cout << "Please Enter The third integer: "; 
     cin >> numb3; 
     sum = numb1 - numb2 - numb3; 

     cout << "And The Sum is " << sum << endl;

 cout << "OPTION 2"<<endl;

 

//Choice 3 of application
  if (choice==3)

  cout << "Please Enter The first integer: ";
     cin >> numb1; 

     cout << "Please Enter The second integer: "; 
     cin >> numb2;  

	 sum = numb1 - numb2

	 cout << "Please Enter The third integer: "; 
     cin >> numb3; 

	 sum1= sum - numb3

	
 if(sum < sum1)   //if the first sum is less than the second
{
    std::cout << "The lowest sum is: " << sum; //print the first sum
}else if(sum1 < sum)  //else if the second sum is smaller
{
    std::cout << "The lowest sum is: " << sum1; //print the second sum
}else     //accounts for the case that they're equal
{
    std::cout << "They're both equal at " << sum;
}




 cout << "OPTION 3"<<endl;
 
  //OPTION 4

if (choice == 4)
	 

     cout << "Please Enter The first integer: ";
     cin >> numb1; 

     cout << "Please Enter The second integer: "; 
     cin >> numb2; 

	 cout << "Please Enter The third integer: "; 
     cin >> numb3; 

     Max = get_max(numb1,numb2,numb3);
     cout << "The max : " << Max << endl;
	 
	 Min = get_min(numb1,numb2,numb3);
	 cout << "The min : " << Min << endl;


     Mid = get_mid(numb1,numb2,numb3);
     cout << "The mid : " << Mid << endl;

    return 0;
}
 
     cout << "OPTION 4"<<endl;


    //OPTION 5
    if (choice == 5)

	cout << "Please Enter The first integer: ";
    cin >> numb1; 

    cout << "Please Enter The second integer: "; 
    cin >> numb2; 

    cout << "Please Enter The third integer: "; 
    cin >> numb3; 
    sum = numb1 * numb2; 

    if (sum > 0)
    cout << "Sum is greater than 0 "<<sum <<endl;

    if (numb3 > numb1)
    cout <<"numb3 is greater than numb1" << numb3<< endl;


    else if((sum>0) && (numb3 <numb1))
    cout <<"No test are true" <<endl;


    //SELECTION ERROR
    if ((choice > 5) || (choice < 1))
    {
      
      cout << "\n\nERROR. PLEASE SELECT 1 - 5 ONLY.";
      cout << "\n\n\n\nPress a key to return to selection menu...";
      _getch();
      goto start;
    }
}
closed account (28poGNh0)
I'll site only some dangerouse! essus you have

/// Line 20 : you forget to close the body of the function instead you implemented a function inside
/// Line 58,59 you forget the semi colon
/// In general you have an essu with curly braces ,and with semi colon finaly with function

if you want to use functions ,will this is how we create function

1
2
3
4
5
6
7
8
9
10
type function();// function prototypes

int main()
{
    function();// function call
}

type function()//function implementation
{
}// and I dont forget to close javascript:editbox1.editSend()the curly brace 

javascript:editbox1.editTag('code')
this is another version of your program(I already answer solved your problem check your old post)
http://www.cplusplus.com/forum/beginner/108341/
First you cannot define a function inside main, put your functions outside your main function, and then call them inside main.


Since you provided some code(Unlike most people who don't have an idea what to do) I'll give you a hand.
Wow so many weird things done. I'll take a wild guess and say you rather came from a scripting language like lua or just new in general.
Things that strike me/ bad habits.

1: Generally people don't like using the goto due to it's lack of garbage control. What do I mean? Well let's say you declare an integer in line 12 of some program. Lets say you jump to line 1 from line 30. Well you're program not only will make a new variable(Line12) but never gets rid of the old variable, it can be conflicting(Depending on your compiler)
2:Functions cannot be declared in the main function. This isn't really clear to me as in why.
3:There are code blocks in parenthesis that don't really have a purpose(Given the compiler won't complain) but it's weird to read especially since your indentation is weird(IMO)

But regardless to me being dumbfounded by this code. Here yah go.
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
165
166
167
168
169
170
171
172
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <algorithm>
/*These are function prototypes.
The whole purpose of having these is to tell the compiler that these
functions do exist but their code is listed somewhere else in the program
(That way the compiler doesn't complain about functions that don't exist)*/
int get_mid(int,int,int);
int get_max(int,int,int);
int get_min(int,int,int);

using namespace std;

int main()
{
    //Get all the characters out of the way(Okay practice but w.e)
    int choice;
    int numb1;
    int numb2;
    int numb3;
    int sum;
    int sum1;
    int Max;
    int Min;
    int Mid;
    //Print the menu and the get the persons selection
    start:
    cout <<"Choose Option ";
    cin>>choice;
    /*Remember that if you have an "If" statement or a 
    statement in general with more than
    One line of code in it then you'll need to put it in a block or 
    parenthesis(Depends who you are) {}*/
    //Option 1
    if (choice ==1)
    {
        cout << "Please Enter The first integer: ";
        cin >> numb1;
        cout << "Please Enter The second integer: ";
        cin >> numb2;
        cout << "Please Enter The third integer: ";
        cin >> numb3;
        sum = numb1 + numb2 + numb3;
        cout << "And The Sum is " << sum << endl;
        cout << "OPTION 1"<<endl;
        return 0;
    }

    //Option 2
    if ( choice ==2 )
    {
        cout << "Please Enter The first integer: ";
        cin >> numb1;
        cout << "Please Enter The second integer: ";
        cin >> numb2;
        cout << "Please Enter The third integer: ";
        cin >> numb3;
        sum = numb1 - numb2 - numb3;
        cout << "And The Sum is " << sum << endl;
        cout << "OPTION 2"<<endl;
        return 0;
    }
    //Choice 3 of application
    if (choice==3)
    {
        cout << "Please Enter The first integer: ";
        cin >> numb1;
        cout << "Please Enter The second integer: ";
        cin >> numb2;
        sum = numb1 - numb2;//Don't forget your semicolon
        cout << "Please Enter The third integer: ";
        cin >> numb3;
        sum1= sum - numb3;//Once again...
        if(sum < sum1)   //if the first sum is less than the second
        {
            std::cout << "The lowest sum is: " << sum; //print the first sum
        }
        else if(sum1 < sum)  //else if the second sum is smaller
        {
            std::cout << "The lowest sum is: " << sum1; //print the second sum
        }
        else     //accounts for the case that they're equal
        {
            std::cout << "They're both equal at " << sum;
        }
        cout << "OPTION 3"<<endl;
        return 0;
    }
    //OPTION 4
    if (choice == 4)
    {
    cout << "Please Enter The first integer: ";
    cin >> numb1;
    cout << "Please Enter The second integer: ";
    cin >> numb2;
    cout << "Please Enter The third integer: ";
    cin >> numb3;
    Max = get_max(numb1,numb2,numb3);
    cout << "The max : " << Max << endl;
    Min = get_min(numb1,numb2,numb3);
    cout << "The min : " << Min << endl;
    Mid = get_mid(numb1,numb2,numb3);
    cout << "The mid : " << Mid << endl;
    cout << "OPTION 4"<<endl;
    return 0;
    }

    //OPTION 5
    if (choice == 5)
    {
        cout << "Please Enter The first integer: ";
        cin >> numb1;
        cout << "Please Enter The second integer: ";
        cin >> numb2;
        cout << "Please Enter The third integer: ";
        cin >> numb3;
        sum = numb1 * numb2;
        if (sum > 0)
        {cout << "Sum is greater than 0 "<<sum <<endl;}
        if (numb3 > numb1)
        {cout <<"numb3 is greater than numb1" << numb3<< endl;}
        else if((sum>0) && (numb3 <numb1))
        {cout <<"No test are true" <<endl;}
        return 0;
    }
    if ((choice > 5) || (choice < 1))
    {
        cout << "\n\nERROR. PLEASE SELECT 1 - 5 ONLY.";
        cout << "\n\n\n\nPress a key to return to selection menu...\n";
        _getch();
        goto start;
    }
    return 0;
}

int get_mid(int a,int b,int c)
{
    if(a==b&&a==c)
    return a;

    if(a==b||a==c||b==c)
    {
        if(a==b)
            return a;
        if(a==c)
            return a;
        if(b==c)
            return b;
    }
    if((a>b&&a<c)||(a<b&&a>c))
    {
        return a;
    }
    else if((b>a&&b<c)||(b<a&&b>c))
    {
        return b;
    }

    else return c;
}
int get_max(int a,int b,int c)
{
    int tmpVar = (b>c)?b:c;
    return (a>tmpVar)?a:tmpVar;
}

int get_min(int a,int b,int c)
{
    int tmpVar = (b<c)?b:c;
    return (a<tmpVar)?a:tmpVar;
}


I tried my best to figure out what you where doing and the minimal way to confuse you.
Next time try a switch statement :P.
Have questions? Ask away.
Last edited on
Thanks very much, much appreciated!!!!
closed account (28poGNh0)
@Factors

Generally people don't like using the goto
not all of them in fact sometimes goto stamtement became very usefull
Last edited on
closed account (3qX21hU5)
Factors wrote:
1: Generally people don't like using the goto due to it's lack of garbage control. What do I mean? Well let's say you declare an integer in line 12 of some program. Lets say you jump to line 1 from line 30. Well you're program not only will make a new variable(Line12) but never gets rid of the old variable, it can be conflicting(Depending on your compiler)


C++ doesn't even have a garbage collector... And I really have no idea what you are talking about in that quote.

Just for the OP information the main reason people don't like goto's and they are generally avoided (Except for very very rare situations) is because they ruin the whole flow of a program and it can be very hard to debug a program with a bunch of goto's.

Generally you will want to avoid goto's at all costs but not because of what Factors said. You want to avoid them because of structuring issues and the readability of code.
Last edited on
Topic archived. No new replies allowed.