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 24917
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.
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
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...
|
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:...
|
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...
|
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...
|
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: 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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |