473,657 Members | 2,555 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

13 New Member
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 24961
teddarr
143 New Member
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 Recognized Expert Top Contributor
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 New Member
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 Recognized Expert Moderator Expert
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
17302
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 setjmp/longjmp or gotos in my program. Thanks,
29
15659
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: case label does not reduce to an integer constant --
9
24170
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 the function is as follows: Vertex& IGEdge::Mate (Vertex const& vertex) {
14
13048
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; int main() { int i;
3
3607
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 the first case IDB_BUTTON it runs fine, but with it there are several compile errors. LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { switch(Msg) { case WM_COMMAND: ...
5
421
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
8403
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8316
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8737
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8509
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7345
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5636
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2735
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1730
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.