Reputation: 5
I've been messing around with my very basic C++ knowledge (been programming for two days), attempting to write a program that calculates user input around phi (1.61803399).
Here's the code, apologies if its a mess:
#include <iostream>
#include <math.h>
using namespace std;
//Prototypes:
float phiExpo;
float phiNegExpo;
float opt1f(float phi, float userInput){
return userInput * phi;}
float opt2f(float phi, float userInput){
return userInput / phi;}
float opt3f(){
return phiExpo;}
float opt4f(){
return phiNegExpo;}
float phiExpof(float phi, float userInput){
pow(phi,userInput);}
float phiNegExpof(float phi, float userInput){
pow(phi,-userInput);}
//Execute program:
int main(){
float userInput;
int userChoice;
float phi = 1.61803399;
float phiExpo;
float phiNegExpo;
cout<<"I want to (press corresponding number, then enter):"<<endl;
cout<<endl;
startchoices:
cout<<"1. Multiply by phi:"<<endl;
cout<<"2. Divide by phi:"<<endl;
cout<<"3. Exponentiate phi:"<<endl;
cout<<"4. Negatively exponentiate phi:"<<endl;
cout<<endl;
cin>>userChoice;
cout<<endl;
switch (userChoice){
case 1:
cout<<"Enter number for multiplication: ";
cin>>userInput;
return opt1f(phi, userInput);
case 2:
cout<<"Enter number for division: ";
cin>>userInput;
return opt2f(phi, userInput);
case 3:
cout<<"Enter number for to exponetiate phi by: ";
cin>>userInput;
return opt3f();
case 4:
cout<<"Enter number for negatively exponentiate phi by: ";
cin>>userInput;
return opt4f();
default:
cout<<"Please enter a number from 1 to 4.";
cout<<endl;
cout<<endl;
goto startchoices;
}
cin.get();
}
Anyway, upon entering a number at the first prompt (1-4), the program simply crashes to the desktop, and I can't figure out why.
Any help would be much appreciated.
Upvotes: 0
Views: 786
Reputation: 2603
Your program doesn't close on the switch statement when I run it; I get your second text. There are a few problems I see though:
First, as faster people have noted, your returns quit out of the application instead of outputting the answer.
Secondly, after you change your code to cout rather than return, you will want to put in a "break;" so you don't run the code in every condition after the current one.
Thirdly, you might want to change the goto into an input loop and add a quit option to your menu. This is more of a stylistic choice, but I think you will find that goto's in c/c++ are harder to debug in the future.
-edit: for formatting- Well, assuming you wanted to be able to do more than one thing per program run, and to get rid of the goto, you could do something like:
boolean quitting = false;
do {
cout << "1) Menu item 1" << endl << "2) Quit" << endl;
cin.get(userchoice);
switch(userchoice) {
case 1:
cout << "Please enter input for option 1: ";
cin >> userInput;
cout << case1function(userInput);
break;
case 2:
quitting = true;
break;
default:
cout << "Please read menu" << endl;
}
}while (!quitting);
Upvotes: 1
Reputation: 20272
The program doesn't crash, it exits because that's what it is supposed to do. The return
statement means that the execution will exit the current function, and in case of the current function being main
- it will exit the program.
The value of the return is the value which the program will return to the OS. If it's not 0 - the OS will think the program exited abnormally, but in your case - it's just the value of the calculation.
Upvotes: 0
Reputation: 26124
Are you sure it crashes? The code simply returns the value of the operation (casted to an int, as this is the return type of main
). I would suggest that you should print it using cout << opt4f()
.
Upvotes: 3
Reputation: 14233
The problem comes with the return statements in your switch.
The function main()
is special in that the return value tells the operating system whether the program was successful. If the return value of main()
is 0, then everything worked fine. If it is nonzero, then there was an error.
In your code, you are returning the value of opt1f(phi, userInput)
, optef(phi, userInput)
, etc. These values are likely non-zero, thus telling the operating system that your program failed.
Upvotes: 2
Reputation: 25759
In this case, the return
s exit the main() function, leading to a clean program termination.
Upvotes: 1