Trying to understand this error

Mar 11, 2018 at 10:55pm
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 11:05pm
When is the 'ot' initialized, if the user does not choose 1?

Either declare variables before the switch, or add a local scope:
1
2
3
4
5
6
7
8
9
10
11
	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;
Mar 11, 2018 at 11:07pm
Thanks, will give that a shot. Thanks again for the quick reply, will let you know.
Mar 11, 2018 at 11:17pm
It loops this statement now, I attempted on defining the variables after the local scope, however it just repeats the following menu.
Main Menu
1.) Sum calculator
2.) Difference Calculator
3.) Trig Calculator
Make Selection 1-3:


1
2
3
... int main(int choice) {
	double one;
	double two; ...
Mar 12, 2018 at 12:11am
closed account (E0p9LyTq)
Don't call main() in a program, only the OS should do it.

Calling main() in your program results in undefined behavior; it might work as you expect, it might not.

Just don't do it.

C++ ISO Standard, §3.6.1.3:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3690.pdf#subsection.3.6.1

The function main shall not be used within a program.

Redefining main() as you have done is also not allowed according to the standard, §3.6.1.2.
Mar 12, 2018 at 12:13am
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:57am
closed account (E0p9LyTq)
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
Topic archived. No new replies allowed.