### 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

 ``1234567891011121314151617181920212223242526272829`` `````` 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?

 ``12345678`` `````` //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!

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167`` ``````//DiscoveryKids.cpp //Program that lets kids to learn math basics #include using std::cout; using std::cin; using std::endl; #include using std::string; using std::getline; #include using std::srand; using std::rand; #include 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'<>option; while(option<1||option>5){ cout<<'\n'<>option; }//end inner while switch(option){ case 1: if(!addition(name)){ cout<<'\n'<<"Bad luck "<>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>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'<>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'<>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.