else if question

Simply, why does entering "-1" not trigger the summary and main menu?

Thanks so much I would be lost if it weren't for here lol

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
  case 1: adcnt++; 

			cout << '\n';
			cout << "How much is " << num1 << " plus " << num2 << '\n';
			cin  >> ans1;
		   do
		   {	
			 
			   if (ans1 != num1 + num2)
				{
				 incorr++;
			     cout << "No. Please try again. \n";
				 cout << "Enter your answer (-1 to return to the menu): \n";
				 cout << "Please add " << num1 << " and " << num2 << '\n';
				 cin >> ans1;
				 incorr++;
			    }

			    else if (ans1 = -1)
			    {
				 cout << "SUMMARY: \n";
		                 cout << "Addition Problems Played: " << adcnt << '\n';
			         cout << "Number of times answered correctly: " << corr << '\n';
			         cout << "Number of times answered incorrectly: " << incorr << '\n';

					return main();
			    }

		    }while (ans1 != num1 + num2);
Look at:

else if (ans1 = -1)

Do you see anything wrong there that is a common mistake?
Using the same variable?
You are using the assignment operator instead of the equality operator.
For example:
var1=var2; //assign var2 to var1

if(var1==var2) //if var1 is equal to var2
//do something
.
Last edited on
It works if I make the else if just an If. Any reason why?
1.-Can you explain the entire exercise?
2.-I am seeing something scary, you are calling main -recursively i guess-
"return main();"
3.-Are there some important -heavy- reason to do it in this way?
4.-Can you call itself main function in your compiler?
1.We have to write a program that lets kids lean to add, subtract, etc.

If they hit the sentinel you have to give a summary and go to the main menu.

2.I'm using main() because we haven't gotten to functions (modules) yet, and I don't want to screw up my syntax even further.

3/4. From what I see yes, since it looks like how to call a module from my intro class.

Thanks for the help!

edit: took out return, still works!
Last edited on
2.I'm using main() because we haven't gotten to functions (modules) yet, and I don't want to screw up my syntax even further.

Unfortunately calling main() as if it was a normal function is not valid code. It is only possible to attempt this by using a quirk of the compiler since it is not permitted by the C++ standard. If you attempt to compile this code with a different compiler it could simply fail to compile and give an error message.

A valid alternative is to use a loop. For example a while loop, which will repeat while a particular condition is true.
@dp13
2.I'm using main() because we haven't gotten to functions (modules) yet, and I don't want to screw up my syntax even further.

You can use a while loop,
isn't?

1
2
3
4
5
6
7
8

//pseudocode style not true c++
while(option isNot exit_key){
//show menu exercises 1-add 2-subtract 3.-....5-exit_key
//if(1 or 2 or 3 or 4...)
     //execute choose with clever modules -add,subtract,etc- 
}//end while loop


what you think?
I'll try it, gotta leave for work, I'll let you know.
OK!

@dp13
check this out!
even that i use functions -modules-
i think that this may help to you!

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
//DiscoveryKids.cpp
//Program that lets kids to learn math basics 

#include <iostream>
using std::cout;
using std::cin;
using std::endl;


#include <string>
using std::string;
using std::getline;

#include <cstdlib>
using std::srand;
using std::rand;

#include <ctime>
using std::time;


bool addition(string name);
bool subtraction(string name);
bool multiplication(string name);
bool division(string name);  


int main(){

srand(time(0));

int option;
string name;
bool endExercise=false;

cout<<"\nHello what's your name? ";
getline(cin,name);


while(!endExercise){
	cout<<'\n'<<name<<" choose an exercise:\n"
		<<"1.-Addition\n"<<"2.-Subtraction\n"
		<<"3.-Multiplication\n"<<"4.-Division\n"
		<<"5.-Ends exercise\n\n";
	cin>>option;
	while(option<1||option>5){
	cout<<'\n'<<name<<" please choose an exercise between 1-5:\n"
                <<"1.-Addition\n"<<"2.-Subtraction\n"
                <<"3.-Multiplication\n"<<"4.-Division\n"
                <<"5.-Ends exercise\n\n";
	cin>>option;
	}//end inner while

	switch(option){
		case 1:
			if(!addition(name)){
				cout<<'\n'<<"Bad luck "<<name<<" try again!"<<endl;
			}else{
				cout<<'\n'<<"Good job "<<name<<'!'<<endl;
			}//end else
		break;
		case 2:
			if(!subtraction(name)){
                                cout<<'\n'<<"Bad luck "<<name<<" try again!"<<endl;
                        }else{
                                cout<<'\n'<<"Good job "<<name<<'!'<<endl;
                        }//end else
		break;
		case 3:
			if(!multiplication(name)){
                                cout<<'\n'<<"Bad luck "<<name<<" try again!"<<endl;
                        }else{
                                cout<<'\n'<<"Good job "<<name<<'!'<<endl;
                        }//end else
		break;
		case 4:
			if(!division(name)){
                                cout<<'\n'<<"Bad luck "<<name<<" try again!"<<endl;
                        }else{
                                cout<<'\n'<<"Good job "<<name<<'!'<<endl;
                        }//end else
		break;
		case 5:
			cout<<"\nGood bye "<<name<<'!'<<endl;
			endExercise=true;
		break;
		default:
		break;
	}//end switch
	
}//end while



return 0; //indicates success
}//end main

bool addition(string name){
int value1=1+rand()%100;
int value2=1+rand()%100;
int answer=value1+value2;
int user_answer;

	cout<<'\n'<<name<<"\nCalculate:\n "<<value1 <<" + "<<value2<<"\n";
	cin>>user_answer;
if(user_answer==answer)
return true;
else
return false;
}//end function addition

bool subtraction(string name){
int value1=1+rand()%100;
int value2=value1+1;

while(value1<value2){
	value2=1+rand()%100;
}//end while

int answer=value1-value2;
int user_answer;

        cout<<'\n'<<name<<"\nCalculate:\n "<<value1 <<" - "<<value2<<"\n";
        cin>>user_answer;
if(user_answer==answer)
return true;
else
return false;
}//end function subtraction

bool multiplication(string name){
int value1=1+rand()%100;
int value2=1+rand()%100;
int answer=value1*value2;
int user_answer;

        cout<<'\n'<<name<<"\nCalculate:\n "<<value1 <<" x "<<value2<<"\n";
        cin>>user_answer;
if(user_answer==answer)
return true;
else
return false;
}//end function multiplication

bool division(string name){

int value1=1+rand()%100;
int value2=1+rand()%100;
int answer;

while(value1%value2!=0||value1*value2==value1||value1==value2){
	value1=1+rand()%100;
	value2=1+rand()%100;
}//end while

answer=value1/value2;
	
int user_answer;

        cout<<'\n'<<name<<"\nCalculate:\n "<<value1 <<" / "<<value2<<"\n";
        cin>>user_answer;
if(user_answer==answer)
return true;
else
return false;
}//end function division



Hello what's your name? Eyenrique

Eyenrique choose an exercise:
1.-Addition
2.-Subtraction
3.-Multiplication
4.-Division
5.-Ends exercise

1

Eyenrique
Calculate:
 68 + 49
117

Good job Eyenrique!

Eyenrique choose an exercise:
1.-Addition
2.-Subtraction
3.-Multiplication
4.-Division
5.-Ends exercise

4

Eyenrique
Calculate:
 78 / 13
6

Good job Eyenrique!

Eyenrique choose an exercise:
1.-Addition
2.-Subtraction
3.-Multiplication
4.-Division
5.-Ends exercise

5

Good bye Eyenrique!
Wow! thanks man,seeing code is the best way to learn so I understand the actual mechanics. I got it solved with way less advanced (incorrect) methods.

I was messing around with the while loop but I need to read the functions chapter more in depth. I got half way and then ran into syntax errors lol. This will definitely help.

Now there is one assignment where I cant figure out how to use the user input to limit the range of the oct, hex and binary numbers it's supposed to put out.

Heck I can't even get the binary to print lol

You've helped more than enough though, THANKS AGAIN!
You are welcome @dp13


how to use the user input to limit the range of the oct, hex and binary numbers it's supposed to put out.


i don't understand what you need to do.
I already handed it in so I'll see the code soon enough lol.

Thanks again! Trust me I'll have more questions in the future.

LOVE THIS FORUM, NICE AND HELPFUL PEOPLE ALL AROUND
Ok, yes this forum is great to share knowledge!
Best regards!
Topic archived. No new replies allowed.