473,414 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

error C2360: initialization of 'i' is skipped by 'case' label ?

My program has an error C2360: initialization of 'i' is skipped by 'case' label.
I don't know why. Please help me out!
thks!!

the error occurs in Division case 3.


Expand|Select|Wrap|Line Numbers
  1. //Claire's Calculator
  2. #include <iostream.h>
  3. #include <math.h>
  4. double pow(double x, double y);
  5.  
  6.  
  7. main()
  8. {
  9.     int choice, choicee;
  10.     const double PI = 3.14159;
  11.     float a, b, c;
  12.     int n, m;
  13.     double cy;
  14.     double x, y;
  15.     float x_to_the_y;
  16.     double r;
  17.     int re, q;
  18.  
  19.     do //
  20.     {
  21.         cout << "Which calculation you wish to use?" << '\n';
  22.         cout << "1 - Addition" << '\n';
  23.         cout << "2 - Subtraction" << '\n';
  24.         cout << "3 - Multiplication" << '\n';
  25.         cout << "4 - Division " << '\n';
  26.         cout << "5 - Multiplication by a percent " << '\n';
  27.         cout << "6 - Calculate powers" << '\n';
  28.         cout << "7 - Calculate a circle" << '\n';
  29.         cout << "8 - Calculate an exponential" << '\n';
  30.         cin >> choice;    //get choice from the user
  31.         if ((choice < 1 ) || (choice > 8))
  32.         {
  33.             cout << "Re-enter your choice.\n";
  34.         }
  35.     }
  36.     while ((choice < 1 ) || (choice > 8));
  37.  
  38.     switch(choice)
  39.     {
  40.     case 1:   // Addition
  41.         cout << "enter 1st number: \n";
  42.         cin >> a;
  43.         cout << "enter 2nd number: \n";
  44.         cin >> b;
  45.         cout << "a + b =" << a + b << '\n';
  46.         break;
  47.     case 2:    //Subtraction
  48.         cout << "Enter first number: \n";
  49.         cin >> a;
  50.         cout << "Enter second number: \n";
  51.         cin >> b;
  52.         cout << "a - b = " << a - b << '\n';
  53.         break;
  54.     case 3:      //Multiplication
  55.         cout << "Enter first number: \n";
  56.         cin >> a;
  57.         cout << "Enter second number: \n";
  58.         cin >> b;
  59.         cout << "a * b = " << a * b << '\n';
  60.         break;
  61.     case 4:    //Division
  62.         do {
  63.             cout << "1 - answer as a decimal\n"; 
  64.             cout << "2 - answer as a fraction\n";
  65.             cout << "3 - answer as a number with a remainder\n";
  66.             cin >> choicee;
  67.         }        
  68.     while ((choicee < 1 ) || (choicee > 3));
  69.  
  70.     switch(choicee)
  71. {
  72.     case 1:  //answer as a decimal
  73.             cout << "Enter first number: \n";
  74.             cin >> a;
  75.             cout << "Enter second number: \n";
  76.             cin >> b;
  77.             cy = a / b;
  78.             cout << "answer = " << cy << '\n';
  79.     break;
  80.     case 2: //answer as a fraction
  81.             int n, m, i = 0;
  82.             int Denom, Num;
  83.         cout << "Enter first number: \n";
  84.         cin >> n;
  85.         cout << "Enter second number: \n";
  86.         cin >> m;
  87.        n = Denom;
  88.        m = Num;
  89.        for (i = n * m; i > 1; i--)
  90.        {
  91.                if ((n % i == 0) && (m % i == 0))
  92.                {
  93.                        n /= i;
  94.                        m /= i;
  95.                }
  96.        }
  97.        cout << "a=" << n << "/" << m << '\n';
  98.  
  99.     break;
  100.     case 3: //answer as a number with a remainder
  101. // error C2360: initialization of 'i' is skipped by 'case' label
  102.         cout << "Enter first number: \n";
  103.         cin >> a;
  104.         cout << "Enter second number: \n";
  105.         cin >> b;
  106.         q = a / b;
  107.         re = (int)a % (int)b;
  108.         cout << "quotient = " << q << '\n';
  109.         cout << "reminder = " << re << '\n';
  110.         break;
  111. }
  112.  
  113.     case 5:    //Multiplication by a percent
  114.         cout << "Enter first number: \n";
  115.         cin >> a;
  116.         cout << "Enter second number:(percentage) \n";
  117.         cin >> b;
  118.         c = a * (b / 100);
  119.         cout << "answer = " << c << '\n';
  120.         break;
  121.     case 6:    //Calculate powers
  122.         double pow(double x, double y);
  123.         cout << "Enter first number: \n";
  124.         cin >> x;
  125.         cout << "Enter the power: \n";
  126.         cin >> y;    
  127.         x_to_the_y = pow(x, y);
  128.         cout << "answer = " << x_to_the_y << '\n';
  129.         break;    
  130.     case 7:   //Calculate a circle
  131.         cout << "Please enter r = \n";
  132.         cin >> r;
  133.         cout << "The area is " << PI * r * r << '\n';
  134.         break;
  135.     case 8:    // Calculate an exponential
  136.         cout << "Enter first number: \n";
  137.         cin >> a;
  138.         double pow(double x, double y);
  139.         cout << "Enter second number: \n";
  140.         cin >> x;
  141.         cout << "Enter the power: \n";
  142.         cin >> y;    
  143.         x_to_the_y = pow(x, y);
  144.         cout << "answer = " << a * x_to_the_y << '\n';
  145.         break;
  146.     }
  147.     return 0;
  148. }
Jan 22 '08 #1
4 24917
teddarr
143 100+
In case 2 try using:

int n, m, i;
n, m, i= 0;

instead of:
int n, m, i = 0;

not sure why it works this way.
Jan 22 '08 #2
gpraghuram
1,275 Expert 1GB
Hi,
I think the error is coming becos the n,m declartion in case 2 is clashing with the declartion in the beginning of the program.
Enclose the case 2 inside curly brace like this

Expand|Select|Wrap|Line Numbers
  1. case 2:
  2. {
  3.    int n,m,i=0;
  4. ....
  5. }
  6. break;
  7.  
Raghuram
Jan 23 '08 #3
ppd
4
Hi,
I don't know about the tags. I just started today.

The variables defined at the top of the program
are in scope for the duration of the entire program.
So declaring n and m again is not necessary in case 2.
You could declare i at the top of the program and then
you may not have a problem. If you want to keep i local,
it is correct to enclose the contents of the case in curly brackerts. That makes i a local variable for case 2.
It would not hurt to enclose each case in curly brackets and define your local variables for each case, if you would like to. Anyway, whatever is at the top is in scope to the end of the program. C++ allows you to declare
your variables near the applicable code, whereas C does not. So in your case, it would not hurt to take advantage of the capabilities of C++.
Jan 23 '08 #4
weaknessforcats
9,208 Expert Mod 8TB
The problem is here:
case 2: //answer as a fraction
int n, m, i = 0;
etc...
break;
case 3: //answer as a number with a remainder
// error C2360: initialization of 'i' is skipped by 'case' label
cout << "Enter first number: \n";
etc...
break;
The variable i has the scope of the switch, not the case. Hence you can use i in case 3 but you would miss the initializaton of the variable.

If i is used only in case 2, then use braces to give it a local scope and your error will go away:
Expand|Select|Wrap|Line Numbers
  1. case 2: //answer as a fraction
  2.       {
  3.             int n, m, i = 0;
  4.       }
  5. etc... 
  6.     break;
  7.     case 3: //answer as a number with a remainder
  8. // error C2360: initialization of 'i' is skipped by 'case' label
  9.         cout << "Enter first number: \n";
  10.         etc... 
  11.        break;
  12.  
Jan 23 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Neil Zanella | last post by:
Hello, Does anyone know what the following g++ compiler error message means: error: jump to case label I get this error when switching two case labels together with their bodies. I have no...
29
by: SysSpider | last post by:
Hi again, This is my problem: when i try to compile the code that contains the function below, i get this: -- gcc:21: error: case label does not reduce to an integer constant gcc:24: error:...
9
by: CQ | last post by:
Hi everyone, I get the following error when compiling my code: error: invalid initialization of non-const reference of type 'Vertex&' from a temporary of type 'int' The implementation of...
14
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; int main() { int i;
3
by: newguy194 | last post by:
I am currently writing a Win32 application and when I compile I get a "Jump to case label" error and another error tellling me that switch(LOWORD(wParam)) is unreachable in the switch, when I remove...
5
by: Rex Mottram | last post by:
Can anyone explain this? % cat t.c #include <stdio.h> #define FIRST "first" #define SECOND "second" int main(int argc, char *argv)
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.