1 Replies - 12874 Views - Last Post: 01 December 2005 - 12:08 PM Rate Topic: -----

#1 ladi419  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 27-October 05

Case Statement Issues

Posted 01 December 2005 - 11:39 AM

can someone help me fix these codes.its pretty olong though.i have been getting an error message.thats it below.

rror C2360: initialization of 'j' is skipped by 'case' label
Z:\C++\LadiProject12\Class_project[1].cpp(137) : see declaration of 'j'
Z:\C++\LadiProject12\Class_project[1].cpp(152) : error C2361: initialization of 'j' is skipped by 'default' label
Z:\C++\LadiProject12\Class_project[1].cpp(137) : see declaration of 'j'



/* Write a 
#include <iostream> // allows program to input and output data

using namespace std; // indentifier

// function prototype
double Temperature_Conversion (double, double);
int Even_Odd (int);
double Grade_Conversion (double);
double Factorial(double);
int Square_Sum (int);
int Exit (int);

int main()
{
	double celcius; // declare variable
	double fahrenheit; // declare variable
	double grade; // declare variable
	double result; // declare variable
	int menu; // declare variable
	int int_number; // declare variable
	
	cout << endl; // cursor to a newline
	
	cout << "\t\t\t" << "  WELCOME TO MY C++ PROGRAM  \n" << endl; // display messege
	// prompt user to push key to continue
	cout << "Please enter any numerical key \nfollowed by the return key to continue: "; 


	cin >> menu; //prompt for input
	cout << endl; // cursor to a newline

	do
	{
  // Operation Menu / Function - prints menu screen
  while (menu != 6)
  {
 	 cout << "OPERATIONS" << endl;
 	 cout << endl;
 	 cout << "\t" << "(1)-Temperature Conversion" << endl;
 	 cout << "\t" << "(2)-Even Odd" << endl;
 	 cout << "\t" << "(3)-Grade Conversion" << endl;
 	 cout << "\t" << "(4)-Factorial" << endl;
 	 cout << "\t" << "(5)-Square Sum" << endl;
 	 cout << "\t" << "(6)-Exit" << endl;

 	 cout << endl; // cursor to a newline

 	 cout << "Please select an operation from the menu: ";
 	 cin >> menu; // read input to select menu
 	 cout << endl; // cursor to a newline

 	 switch (menu)
 	 {
 	 case 1:
   	 cout << "Enter Fahrenheit temperature: "; // taken if menu == 1 & prompts for input
   	 cin >> fahrenheit; // reads input
      
   	 cout << endl; // cursor to a newline

   	 celcius = (5/9)*(fahrenheit - 32); // formular convert Fahrenheit to Celsius

   	 cout << endl; // cursor to a newline
   	 cout << "The Temperature in Celsius is " << celcius << endl; // taken if menu == 1

   	 cout << endl; // cursor to a newline
      
   	 break;

 	 case 2:
   	 cout << "Enter Integer: "; // taken if menu == 2 & prompts for input
   	 cin >> int_number; // reads input
      
   	 cout << endl; // cursor to a newline

   	 if ( int_number % 2 == 0)
      cout << "Integer " << int_number << " is Even" << endl; // taken if menu == 2
   	 else
      cout << "Integer " << int_number << " is Odd" << endl; // taken if menu == 2

   	 cout << endl; // cursor to a newline

   	 break;

 	 case 3:
   	 cout << "Enter Grade: "; // taken if menu == 3 & prompts for input
   	 cin >> grade; // reads input
      
   	 cout << endl; // cursor to a newline

   	 // taken if menu == 3 
   	 if(grade <=100 && grade >=90)
      cout << "Your letter Grade is an A" << endl; // display messege
   	 else if (grade >= 89 && grade >= 80)
      cout << "Your letter Grade is a B" << endl; // display messege
   	 else if (grade <= 79 && grade >= 70)
      cout << "Your letter Grade is a C" << endl; // display messege
   	 else if (grade <= 69 && grade >= 60)
      cout << "Your letter Grade is a D" << endl; // display messege
   	 else if (grade <= 59 && grade >= 0)
      cout << "Your letter Grade is an F" << endl; // display messege

   	 cout << endl; // cursor to a newline

   	 break;

 	 case 4:
   	 cout << "Enter Integer: "; // taken if menu == 4 & prompts for input
   	 cin >> int_number; // reads input
   	 
   	 cout << endl; // cursor to a newline

   	 result = 1; // intialize variable

   	 for (int i = 1; i <= int_number; i++)

      result *= i; // assign value to result

   	 cout << "The Factorial " << int_number << " is " << result << endl; // taken if menu == 4 & display messege

   	 cout << endl; // cursor to a newline

   	 break;

 	 case 5:
   	 result = 0; // initialize variable

   	 for(int j = 1; j <= 100; j++)

      result += j * j; // add value to result when squared 
   	 
   	 cout << " The sum of the squares from 1 - 100 is " << result << endl; // taken if menu == 5 & display messege
  
   	 cout << endl; // cursor to a newline

   	 break;

 	 case 6:
   	 cout << "You have come to The End of This Operation.Hope you enjoyed it." << endl; // taken if menu == 6 & display messege

   	 break;

 	 default: // default setting of the program
   	 cout << "Please enter a valid menu choice." << endl; // Prompts for input
   	 cout << endl;

   	 break;

 	 } // end switch

  } // end while
	}
	while(menu >= 1 && menu <= 5); // end do while and set its parameter

	return 0; // execute and return value
}// ended function double

// definition of function Temperature Conversion
double Temperature_Conversion (double fahrenheit, double celcius)
{	
	celcius = (5/9)*(fahrenheit - 32); // formular convert Fahrenheit to Celsius

	return Temperature_Conversion (fahrenheit, celcius);
}

// definition of function Even Odd
int Even_Odd (int int_number)
{
	if ( int_number % 2 == 0)
  cout << "Integer " << int_number << " is Even" << endl;
	else
  cout << "Integer " << int_number << " is Odd" << endl;

	return Even_Odd (int_number);
}

// definition of function Grade Conversion
double Grade_Conversion (double grade)
{
	if(grade <=100 && grade >=90)
  cout << "Your letter Grade is an A" << endl;
	else if (grade >= 89 && grade >= 80)
  cout << "Your letter Grade is a B" << endl;
	else if (grade <= 79 && grade >= 70)
  cout << "Your letter Grade is a C" << endl;
	else if (grade <= 69 && grade >= 60)
  cout << "Your letter Grade is a D" << endl;
	else if (grade <= 59 && grade >= 0)
  cout << "Your letter Grade is an F" << endl;

	return Grade_Conversion(grade);
}

// definition of function factorial
double Factorial(double int_number)
{
	double result;

	result = 1; // intialize variable

	for (int i = 1; i <= int_number; i++)

  result *= i;

	return Factorial (int_number);
}

// definition of function Square_Sum
int Square_Sum (int result)
{
	result = 0; // initialize variable

	for(int j = 1; j <= 100; j++)

	result += j * j;

	return Square_Sum (result);
}

// definition of function Exit
int Exit (int exit)
{
	return Exit (exit);
}



Is This A Good Question/Topic? 0
  • +

Replies To: Case Statement Issues

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Case Statement Issues

Posted 01 December 2005 - 12:08 PM

Given your error codes, I'm going to assume that you are using Microsoft C++...as such, initializer and declarations inside a case statement have to be enclosed in blocks...see here and here. You'll have to block, by which I mean {} any variable initializations and declarations you may have in the switch statement.

As an FYI, you may run into a few other problems as well...each of your functions recursively calls itself in the return statements.
Was This Post Helpful? 1

Page 1 of 1