• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

[HELP]C++ Menu Code Error

Junior Spellweaver
Joined
Jun 28, 2012
Messages
150
Reaction score
14
Guys, can you please help me with my Project.. I don't know what is the error.

#include<iostream>
using namespace std;

void main()
{
char choice;

do
{
int program,number;



cout<<"========MENU========\n"
<<"1) PRIME NUMBER\n"
<<"2) RIGHT TRIANGLE\n"
<<"3) FIBONACCI SEQUENCE\n"
<<"4) FACTORIAL\n"
<<"5) EXIT\n"
<<"Enter your Choice:\n";
cin>>program;

system("CLS");

switch(program)
{
case 1:
{
int number,count=0;
cout<<"Enter a number to check if it is PRIME or NOT";
cin>>number;

for(int a=1;a<=number;a++)

{
int(number%a==0)
{
count++;
}
if(count==2)
{
cout<<"That is a Prime Number\n";
}
else
{
cout<<"That is not a Prime Number\n";
}//end else statement

cout<<endl;
break;

}//end case 1

case 2:
{
int a;

cin>>a;
for(int r=1;r<=a;r++)
{
for(int c=1;c<=r;c++)
{
cout<<"*";
}
cout<<endl;
break;
}//end case 2

case 3:
{
int n,c, first=0,second=1,next;

cout<<"Enter the number of terms of Fibonacci Sequence you want"
<<endl;
cin>>n;

cout<<"First"<<n<<"terms of Fibonacci Sequence are:"<<endl;

for(c=0,c<n;c++)
{
if(c<=1)
next=c;
else
{
next=first+second;
first=second;
second=next;
}
cout<<next<<endl;
}
return 0;
break;
}

case 4:
{
int num.factorial=1;
cout<<"Enter the number:";

cin>>num;

for(int i=1;i<=num;i++)
{
factorial=factorial*i;
}
cout<<"The factorial of the given number is:"<<factorial<<endl;

cin.get();

return 0;
break;
}

case 5:

cout<<"Program Exit";
break;

defualt:cerr<<"Invalid Choice!\n";

}

cout<<"Do you wan to try again? [y/n]:";
cin>>choice;

system("CLS");
}
while(choice=='y'||choice=='Y');
cout<<endl;

}//end main



ERROR:

C:\Users\ChiShing\Desktop\MENU\CSKJSWAN.cpp(36) : error C2143: syntax error : missing ';' before '{'
C:\Users\ChiShing\Desktop\MENU\CSKJSWAN.cpp(54) : error C2360: initialization of 'a' is skipped by 'case' label
C:\Users\ChiShing\Desktop\MENU\CSKJSWAN.cpp(33) : see declaration of 'a'
C:\Users\ChiShing\Desktop\MENU\CSKJSWAN.cpp(54) : error C2360: initialization of 'count' is skipped by 'case' label
C:\Users\ChiShing\Desktop\MENU\CSKJSWAN.cpp(29) : see declaration of 'count'
C:\Users\ChiShing\Desktop\MENU\CSKJSWAN.cpp(69) : error C2360: initialization of 'r' is skipped by 'case' label
C:\Users\ChiShing\Desktop\MENU\CSKJSWAN.cpp(59) : see declaration of 'r'
C:\Users\ChiShing\Desktop\MENU\CSKJSWAN.cpp(69) : error C2360: initialization of 'a' is skipped by 'case' label
C:\Users\ChiShing\Desktop\MENU\CSKJSWAN.cpp(33) : see declaration of 'a'
C:\Users\ChiShing\Desktop\MENU\CSKJSWAN.cpp(69) : error C2360: initialization of 'count' is skipped by 'case' label
C:\Users\ChiShing\Desktop\MENU\CSKJSWAN.cpp(29) : see declaration of 'count'
C:\Users\ChiShing\Desktop\MENU\CSKJSWAN.cpp(79) : error C2143: syntax error : missing ';' before ')'
C:\Users\ChiShing\Desktop\MENU\CSKJSWAN.cpp(91) : error C2562: 'main' : 'void' function returning a value
C:\Users\ChiShing\Desktop\MENU\CSKJSWAN.cpp(4) : see declaration of 'main'
C:\Users\ChiShing\Desktop\MENU\CSKJSWAN.cpp(95) : error C2360: initialization of 'r' is skipped by 'case' label
C:\Users\ChiShing\Desktop\MENU\CSKJSWAN.cpp(59) : see declaration of 'r'
C:\Users\ChiShing\Desktop\MENU\CSKJSWAN.cpp(95) : error C2360: initialization of 'a' is skipped by 'case' label
C:\Users\ChiShing\Desktop\MENU\CSKJSWAN.cpp(33) : see declaration of 'a'
C:\Users\ChiShing\Desktop\MENU\CSKJSWAN.cpp(95) : error C2360: initialization of 'count' is skipped by 'case' label
C:\Users\ChiShing\Desktop\MENU\CSKJSWAN.cpp(29) : see declaration of 'count'
C:\Users\ChiShing\Desktop\MENU\CSKJSWAN.cpp(97) : error C2143: syntax error : missing ';' before '.'
C:\Users\ChiShing\Desktop\MENU\CSKJSWAN.cpp(97) : error C2143: syntax error : missing ';' before '.'
C:\Users\ChiShing\Desktop\MENU\CSKJSWAN.cpp(104) : error C2065: 'factorial' : undeclared identifier
C:\Users\ChiShing\Desktop\MENU\CSKJSWAN.cpp(110) : error C2562: 'main' : 'void' function returning a value
C:\Users\ChiShing\Desktop\MENU\CSKJSWAN.cpp(4) : see declaration of 'main'
C:\Users\ChiShing\Desktop\MENU\CSKJSWAN.cpp(114) : error C2360: initialization of 'r' is skipped by 'case' label
C:\Users\ChiShing\Desktop\MENU\CSKJSWAN.cpp(59) : see declaration of 'r'
C:\Users\ChiShing\Desktop\MENU\CSKJSWAN.cpp(114) : error C2360: initialization of 'a' is skipped by 'case' label
C:\Users\ChiShing\Desktop\MENU\CSKJSWAN.cpp(33) : see declaration of 'a'
C:\Users\ChiShing\Desktop\MENU\CSKJSWAN.cpp(114) : error C2360: initialization of 'count' is skipped by 'case' label
C:\Users\ChiShing\Desktop\MENU\CSKJSWAN.cpp(29) : see declaration of 'count'
C:\Users\ChiShing\Desktop\MENU\CSKJSWAN.cpp(140) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

CSKJSWAN.exe - 19 error(s), 0 warning(s)
 
Junior Spellweaver
Joined
Jun 28, 2012
Messages
150
Reaction score
14
Done and still have 18 errors :( .

C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(54) : error C2360: initialization of 'a' is skipped by 'case' label
C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(33) : see declaration of 'a'
C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(54) : error C2360: initialization of 'count' is skipped by 'case' label
C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(29) : see declaration of 'count'
C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(69) : error C2360: initialization of 'r' is skipped by 'case' label
C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(59) : see declaration of 'r'
C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(69) : error C2360: initialization of 'a' is skipped by 'case' label
C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(33) : see declaration of 'a'
C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(69) : error C2360: initialization of 'count' is skipped by 'case' label
C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(29) : see declaration of 'count'
C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(79) : error C2143: syntax error : missing ';' before ')'
C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(91) : error C2562: 'main' : 'void' function returning a value
C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(4) : see declaration of 'main'
C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(95) : error C2360: initialization of 'r' is skipped by 'case' label
C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(59) : see declaration of 'r'
C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(95) : error C2360: initialization of 'a' is skipped by 'case' label
C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(33) : see declaration of 'a'
C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(95) : error C2360: initialization of 'count' is skipped by 'case' label
C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(29) : see declaration of 'count'
C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(97) : error C2143: syntax error : missing ';' before '.'
C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(97) : error C2143: syntax error : missing ';' before '.'
C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(104) : error C2065: 'factorial' : undeclared identifier
C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(110) : error C2562: 'main' : 'void' function returning a value
C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(4) : see declaration of 'main'
C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(114) : error C2360: initialization of 'r' is skipped by 'case' label
C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(59) : see declaration of 'r'
C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(114) : error C2360: initialization of 'a' is skipped by 'case' label
C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(33) : see declaration of 'a'
C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(114) : error C2360: initialization of 'count' is skipped by 'case' label
C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(29) : see declaration of 'count'
C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(132) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

menuhaha.obj - 18 error(s), 0 warning(s)
 
Watching from above
Legend
Joined
Apr 9, 2004
Messages
3,828
Reaction score
752
C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(54) : error C2360: initialization of 'a' is skipped by 'case' label
C:\Users\ChiShing\Desktop\Another\menuhaha.cpp(33) : see declaration of 'a'

The error for the line 54 refers to a line that is meant to be within another code block, but the fact the compiler sees them as the same variable enforces to me what looks like you lack closing curly braces in many places. Focus, man! Pay more attention to detail. I hope you have proper indentation in your original code because this kind of mistakes are TOO DAMN LAME to ask about when simply looking at the indentation should give you proper clues to where you lack braces.

When this is fixed and if you continue to have problems, please upload your code to pastebin or another similar service for easier viewing.
 
Newbie Spellweaver
Joined
Feb 11, 2013
Messages
30
Reaction score
5
Code:
#include <iostream>


using namespace std;


int main()
{
    char choice;


    do
    {
        int program,number;






        cout<<"========MENU========\n"
            <<"1) PRIME NUMBER\n"
            <<"2) RIGHT TRIANGLE\n"
            <<"3) FIBONACCI SEQUENCE\n"
            <<"4) FACTORIAL\n"
            <<"5) EXIT\n"
            <<"Enter your Choice:\n";
        cin>>program;


        //system("CLS");


        switch(program)
        {
        case 1:
        {
            int number, count; //You can't initialize a variable inside a conditional jump.. declare and initializes it on the next line.
            count = 0;


            cout<<"Enter a number to check if it is PRIME or NOT";
            cin>>number;


            for(int a=1; a<=number; a++)


            {
                if (int(number%a) == 0) // lack of the IF
                {
                    count++;
                }
                if(count==2)
                {
                    cout<<"That is a Prime Number\n";
                }
                else
                {
                    cout<<"That is not a Prime Number\n";
                }//end else statement


                cout<<endl;
                break;


            }//end case 1


            case 2:
            {
                int a;


                cin>>a;
                for(int r=1; r<=a; r++) //
                {
                    for(int c=1; c<=r; c++)
                    {
                        cout<<"*";
                    }
                    cout<<endl;
                    break;
                }//end case 2


                case 3:
                {
                    int n, c, first, second, next; // I put the initialization on the next lines.


                    first = 0;
                    second = 1;


                    cout<<"Enter the number of terms of Fibonacci Sequence you want"
                        <<endl;
                    cin>>n;


                    cout<<"First"<<n<<"terms of Fibonacci Sequence are:"<<endl;


                    for(c=0; c<n; c++)
                    {
                        if(c<=1)
                            next=c;
                        else
                        {
                            next=first+second;
                            first=second;
                            second=next;
                        }
                        cout<<next<<endl;
                    }
                    return 0;
                    break;
                }


                case 4:
                {
                    int num, factorial; // you made a mistake here putting "." instead of ",".. put attention! :)
                    factorial = 1;


                    cout<<"Enter the number:";


                    cin>>num;


                    for(int i=1; i<=num; i++)
                    {
                        factorial=factorial*i;
                    }
                    cout<<"The factorial of the given number is:"<<factorial<<endl;


                    cin.get();


                    return 0;
                    break;
                }


                case 5:


                    cout<<"Program Exit";
                    break;


defualt:
                    cerr<<"Invalid Choice!\n";


                }


                cout<<"Do you wan to try again? [y/n]:";
                cin>>choice;


                //system("CLS");
            }
            //while(choice=='y'||choice=='Y');
            //cout<<endl;


        } // ends SWITCH


    } while(choice=='y'||choice=='Y'); // ends DO
    cout<<endl;


    return 0;


}//end main << lack of "}"

It's ok here.. i put some comments on the code to you..
Put more attention when you coding, many things that i fixed was just lack of attention.

regards..
 
Junior Spellweaver
Joined
Jun 28, 2012
Messages
150
Reaction score
14
I Recode it .. No errors but I think my grammar in each Menus are wrong :(:

CPP file
Code:
#include<iostream>
using namespace std;

void main()
{
    char choice;
    
    do
    {
        int program,number;
        


        cout<<"========MENU========\n"
            <<"1) PRIME NUMBER\n"
            <<"2) RIGHT TRIANGLE\n"
            <<"3) FIBONACCI SEQUENCE\n"
            <<"4) FACTORIAL\n"
            <<"5) EXIT\n"
            <<"Enter your Choice:\n";
        cin>>program;

        system("CLS");

        switch(program)
        {
        case 1:
            {
                int count=0;
                cout<<"Enter a number to check if it is PRIME or NOT";
                cin>>number;

                for(int a=1;a<number;a++)

                {
                    if(number%a==0)
                    {
                        count++;
                    }
                    
                }
                if(count!=1)
                    {
                        cout<<"That is a Prime Number\n";
                    }
                else if(count==1)
                    {
                        cout<<"That is not a Prime Number\n";
                    }//end else statement
                    cout<<endl;
                    break;

            }//end case 1

        case 2:
            {
                int x;

                cin>>x;
                for(int r=1;r<=x;r++)
                { 
                    for(int c=1;c<=r;c++)
                    {
                        cout<<"*";

                    }
                }
            }

        case 3:
            {
                int n,c, first=0,second=1,next;

                cout<<"Enter the number of terms of Fibonacci Sequence you want"
                    <<endl;
                cin>>n;

                cout<<"First"<<n<<"terms of Fibonacci Sequence are:"<<endl;

                for(c=0;c<n;c++)
                {
                    if(c<=1)
                        next=c;
                    else
                    {
                        next=first+second;
                        first=second;
                        second=next;
                    }
                    cout<<next<<endl;
                }
                
                break;
            }
        
        case 4:
            {
                int num, factorial=1;
                cout<<"Enter the number:";

                cin>>num;

                for(int i=1;i<=num;i++)
                {
                    factorial=factorial*i;
                }
                cout<<"The factorial of the given number is:"<<factorial<<endl;

                cin.get();
                break;
            }

        case 5:{

            cout<<"Program Exit";
            break;
               }

        default:
            {
                cerr<<"Invalid Choice!\n";
            }

        }
        

            cout<<"Do you wan to try again? [y/n]:";
            cin>>choice;
        
            system("CLS");
    }while(choice=='y'||choice=='Y');
            cout<<endl;    

}
 
Mythic Archon
Joined
Oct 15, 2012
Messages
700
Reaction score
503
Yes, means focus.. He must to pay attention when coding..


Sorry for my bad english..
I thought that "lack of attention" was right..
How i'd should use ?

Unfocused, lack of concentration, idk. Kinda off topic here. ;'p
 
Watching from above
Legend
Joined
Apr 9, 2004
Messages
3,828
Reaction score
752
I actually thought just "attention" was ok and thought of it as "lack of attention for detail". Only pointed out a typo there in the same post that is scolding someone else for lack of concentration. :)
 
Junior Spellweaver
Joined
Jun 28, 2012
Messages
150
Reaction score
14
Problem Solved:

Recoded

Code:
#include<iostream>
using namespace std;

int main()
{
    char ans;
    int option, numberinput, height, factorial, multiples;
    do
    {
        system("cls");
        cout <<"-o-o-o-o-o-MENU-o-o-o-o-o-\n"
             <<"(1) Prime Number\n"
             <<"(2) Right Triangle\n"
             <<"(3) Fibonacci Sequence\n"
             <<"(4) Factorial\n"
             <<"(5) exit\n"
             <<"Enter your choice: ";
    
         cin >>option;
    
	if(option == 1)
    {
			do
			{
				system("cls");
				numberinput = 0;
				multiples = 0;
				cout << "-o-o-o-o-o-Prime Number-o-o-o-o-o-\n"
					 <<"Enter a number to check: ";
				cin >> numberinput;

				int checkprime;

				for(checkprime = 2;checkprime<=numberinput - 1;checkprime++)
				{
					if(numberinput%checkprime==0)
					{
						cout << numberinput << " is not a prime number.\n";
						break;
					}
				}
				if(checkprime==numberinput)
					cout<<numberinput<<" is a prime number.\n";
				else if(numberinput == 1)
					cout<<numberinput<<" is a prime number.\n";
				
				cout<<"would you like to try again(y for yes): ";
				cin>>ans;
			}while(ans=='y'||ans=='Y');
		
    }

	else if(option == 2)
	{
		do
		{
			system("cls");
			height = 0;
			cout<<"-o-o-o-o-o-Right Triangle-o-o-o-o-o-\n"
				<<"Enter the height of the triangle: ";
			cin>>height;

			for(int x = 1; x<=height; x++)
			{
				for(int y = 1; y <= x; y++)
					cout<<"*";

				cout<< endl;
			}
			cout<<"would you like to try again(y for yes): ";
			cin>>ans;
		}while(ans=='y'||ans=='Y');

	}

	else if(option == 3)
	{
		do
		{
			system("cls");
			int NoOfTerms = 0, count, first = 0, second = 1, next;
			cout<<"-o-o-o-o-o-Fibonacci Sequence-o-o-o-o-o-\n"
				"Enter the number of terms of Fibonacci Sequence: "<<endl;
			cin>>NoOfTerms;

			cout<<"First "<<NoOfTerms<<" terms of Fibonacci Sequence are\n";
			for(count = 0; count < NoOfTerms; count++)
			{
				if(count<=1)
					next = count;

				else
				{
					next = first+second;
					first = second;
					second = next;
				}
				cout<<next<<endl;
			}

			cout<<"would you like to try again(y for yes): ";
			cin>>ans;
		}while(ans=='y'||ans=='Y');
	}

	else if(option == 4)
	{
		do
		{
			system("cls");
			numberinput = 0;
			factorial = 1;
			cout<<"-o-o-o-o-o-Factorial-o-o-o-o-o-\n"
				<<"Please enter a number: ";
			cin >> numberinput;
			for(int counter = 1; counter <= numberinput; counter++)
			{
				factorial = factorial*counter;
			}

			cout<<"The factorial of the given number is: " << factorial << endl;

			cout<<"would you like to try again(y for yes): ";
			cin>>ans;

			
		}while(ans=='y'||ans=='Y');
	}

	else if(option == 5)
		cout<<"Goodbye!\n";

	else
	{
		cout<<"Invalid Input! Please try again!\n";
		system("pause");
	}


    }while(option < 1 || (option>=1 && option <5) || option > 5);
    system("pause");
    return 0;
}
 
Joined
Mar 11, 2007
Messages
904
Reaction score
1,254
Problem Solved:

Recoded

Code:
redacted

Why did you remove the switch statement? It was a better way of doing what you were wanting to do than having
Code:
if(option == 1)
{ ... }
else if(option == 2)
{ ... }

If you couldn't solve the problems you were having with switch() than you need to seriously focus and pay attention to whatever source material you are trying to learn from. There is nothing wrong with recoding something to better suit your needs as a programmer, but to recode something only because you can't solve some compile error (recoding to solve runtime errors are different) is the absolutely wrong way of doing it.
 
Junior Spellweaver
Joined
Jun 28, 2012
Messages
150
Reaction score
14
Sorry but the thing is it is rush , I need to pass as soon as possible :)
 
Newbie Spellweaver
Joined
Nov 26, 2011
Messages
29
Reaction score
1
actually, if you decompile the code, you find that switch() is implemented as if() else if()....
 
Newbie Spellweaver
Joined
Nov 26, 2011
Messages
29
Reaction score
1
agreed

however, you gotta be real careful with switch, or you'll get a fall through
 
Back
Top