Mar 11, 2018 at 10:55pm Mar 11, 2018 at 10:55pm UTC
So guys, I was trying to apply the knowledge I have so far of C++ from this semester... and to try to create something a little better then my original program... but for some reason, my IDE throws back this error (look below)
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
#include <iostream>
#include <ctime>
#include <cmath>
#include <iomanip>
using namespace std;
double calc(double , double , int );
void print_sum(double );
int main(int choice) {
cout << "Main Menu" << endl <<
"1.) Sum calculator" << endl <<
"2.) Difference Calculator" << endl <<
"3.) Trig Calculator" << endl;
cout << "Make Selection 1-3: " ;
cin >> choice;
while (choice > 3 || choice < 1)
{
cout << "Main Menu"
"1.) Sum calculator"
"2.) Difference Calculator"
"3.) Trig Calculator" << endl;
cout << "Enter 1 for option one, 2 for option two, and 3 for option three: " ;
cin >> choice;
}
switch (choice) {
double one;
double two;
case 1:
cout << "Enter the first number that you want to add: " ;
cin >> one;
cout << "Enter the second number that you want to add: " ;
cin >> two;
double ot = calc(one, two, choice);
print_sum(ot);
system("pause" );
break ;
case 2:
cout << "Enter the first number that you want to add: " ;
cin >> one;
cout << "Enter the second number that you want to add: " ;
cin >> two;
double difference = calc(one, two, choice);
print_sum(difference);
system("pause" );
break ;
}
}
double calc(double one, double two, int choice) {
int c1 = main(choice);
if (c1 == 1)
return one + two;
if (c1 == 2)
return one - two;
}
void print_sum(double sum) {
cout << "The sum is: " << sum << endl;
}
Error C2360 initialization of 'ot' is skipped by 'case' label
Last edited on Mar 11, 2018 at 10:56pm Mar 11, 2018 at 10:56pm UTC
Mar 11, 2018 at 11:07pm Mar 11, 2018 at 11:07pm UTC
Thanks, will give that a shot. Thanks again for the quick reply, will let you know.
Mar 12, 2018 at 12:13am Mar 12, 2018 at 12:13am UTC
Thank you. @FurryGuy I shall work it another way. I appreciate the help!
EDIT:
Modified the calc function as follows:
1 2 3 4 5 6
double calc(double one, double two, int choice) {
int c1 = choice;
if (c1 == 1)
return one + two;
if (c1 == 2)
return one - two;
Program works now. :)
Last edited on Mar 12, 2018 at 12:16am Mar 12, 2018 at 12:16am UTC
Mar 12, 2018 at 12:57am Mar 12, 2018 at 12:57am UTC
Not how I would recommend writing it, but if you have to call the "menu" function with each iteration you could do this:
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
#include <iostream>
void menu();
double calc(double , double , int );
void print_sum(double );
int main()
{
menu();
}
void menu()
{
int choice;
do
{
std::cout << "Main Menu\n"
<< "1.) Sum calculator\n"
<< "2.) Difference Calculator\n"
<< "3.) Trig Calculator\n"
<< "0.) Quit\n"
<< "Make Selection 0 - 3: " ;
std::cin >> choice;
double one;
double two;
double answer = 0.0;
switch (choice)
{
case 1:
{
std::cout << "Enter the first number for addition: " ;
std::cin >> one;
std::cout << "Enter the second number for addition: " ;
std::cin >> two;
answer = calc(one, two, choice);
print_sum(answer);
menu();
}
case 2:
std::cout << "Enter the first number for subtraction: " ;
std::cin >> one;
std::cout << "Enter the second number for subtraction: " ;
std::cin >> two;
answer = calc(one, two, choice);
print_sum(answer);
menu();
case 3:
std::cout << "Trig Calculator on vacation\n\n" ;
menu();
case 0:
std::cout << "Goodbye!\n" ;
exit(0);
default :
std::cout << "Invalid Input!\n\n" ;
}
} while (choice > 3 || choice < 0);
}
double calc(double one, double two, int choice)
{
double sum = -1.0;
if (choice < 3)
{
(choice == 1) ? (sum = one + two) : (sum = one - two);
}
// code for calulating the trig
return sum;
}
void print_sum(double sum)
{
std::cout << "The answer is: " << sum << "\n\n" ;
}
Last edited on Mar 12, 2018 at 1:10am Mar 12, 2018 at 1:10am UTC