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. - //Claire's Calculator
-
#include <iostream.h>
-
#include <math.h>
-
double pow(double x, double y);
-
-
-
main()
-
{
-
int choice, choicee;
-
const double PI = 3.14159;
-
float a, b, c;
-
int n, m;
-
double cy;
-
double x, y;
-
float x_to_the_y;
-
double r;
-
int re, q;
-
-
do //
-
{
-
cout << "Which calculation you wish to use?" << '\n';
-
cout << "1 - Addition" << '\n';
-
cout << "2 - Subtraction" << '\n';
-
cout << "3 - Multiplication" << '\n';
-
cout << "4 - Division " << '\n';
-
cout << "5 - Multiplication by a percent " << '\n';
-
cout << "6 - Calculate powers" << '\n';
-
cout << "7 - Calculate a circle" << '\n';
-
cout << "8 - Calculate an exponential" << '\n';
-
cin >> choice; //get choice from the user
-
if ((choice < 1 ) || (choice > 8))
-
{
-
cout << "Re-enter your choice.\n";
-
}
-
}
-
while ((choice < 1 ) || (choice > 8));
-
-
switch(choice)
-
{
-
case 1: // Addition
-
cout << "enter 1st number: \n";
-
cin >> a;
-
cout << "enter 2nd number: \n";
-
cin >> b;
-
cout << "a + b =" << a + b << '\n';
-
break;
-
case 2: //Subtraction
-
cout << "Enter first number: \n";
-
cin >> a;
-
cout << "Enter second number: \n";
-
cin >> b;
-
cout << "a - b = " << a - b << '\n';
-
break;
-
case 3: //Multiplication
-
cout << "Enter first number: \n";
-
cin >> a;
-
cout << "Enter second number: \n";
-
cin >> b;
-
cout << "a * b = " << a * b << '\n';
-
break;
-
case 4: //Division
-
do {
-
cout << "1 - answer as a decimal\n";
-
cout << "2 - answer as a fraction\n";
-
cout << "3 - answer as a number with a remainder\n";
-
cin >> choicee;
-
}
-
while ((choicee < 1 ) || (choicee > 3));
-
-
switch(choicee)
-
{
-
case 1: //answer as a decimal
-
cout << "Enter first number: \n";
-
cin >> a;
-
cout << "Enter second number: \n";
-
cin >> b;
-
cy = a / b;
-
cout << "answer = " << cy << '\n';
-
break;
-
case 2: //answer as a fraction
-
int n, m, i = 0;
-
int Denom, Num;
-
cout << "Enter first number: \n";
-
cin >> n;
-
cout << "Enter second number: \n";
-
cin >> m;
-
n = Denom;
-
m = Num;
-
for (i = n * m; i > 1; i--)
-
{
-
if ((n % i == 0) && (m % i == 0))
-
{
-
n /= i;
-
m /= i;
-
}
-
}
-
cout << "a=" << n << "/" << m << '\n';
-
-
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";
-
cin >> a;
-
cout << "Enter second number: \n";
-
cin >> b;
-
q = a / b;
-
re = (int)a % (int)b;
-
cout << "quotient = " << q << '\n';
-
cout << "reminder = " << re << '\n';
-
break;
-
}
-
-
case 5: //Multiplication by a percent
-
cout << "Enter first number: \n";
-
cin >> a;
-
cout << "Enter second number:(percentage) \n";
-
cin >> b;
-
c = a * (b / 100);
-
cout << "answer = " << c << '\n';
-
break;
-
case 6: //Calculate powers
-
double pow(double x, double y);
-
cout << "Enter first number: \n";
-
cin >> x;
-
cout << "Enter the power: \n";
-
cin >> y;
-
x_to_the_y = pow(x, y);
-
cout << "answer = " << x_to_the_y << '\n';
-
break;
-
case 7: //Calculate a circle
-
cout << "Please enter r = \n";
-
cin >> r;
-
cout << "The area is " << PI * r * r << '\n';
-
break;
-
case 8: // Calculate an exponential
-
cout << "Enter first number: \n";
-
cin >> a;
-
double pow(double x, double y);
-
cout << "Enter second number: \n";
-
cin >> x;
-
cout << "Enter the power: \n";
-
cin >> y;
-
x_to_the_y = pow(x, y);
-
cout << "answer = " << a * x_to_the_y << '\n';
-
break;
-
}
-
return 0;
-
}
4 24961
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.
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 -
case 2:
-
{
-
int n,m,i=0;
-
....
-
}
-
break;
-
Raghuram
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++.
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: -
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;
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
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,
|
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
--
|
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) {
|
by: subramanian100in |
last post by:
Consider the following program:
#include <iostream>
using namespace std;
int main()
{
int i;
|
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:
...
| |
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)
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |
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();...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |