Hey guys,
I am writing a program with functions that does different things for a user. This is a program i am writing for school and my lab teachers are horrible at helping us. The problem is that i get an illegal case error for all my cases (five cases). CAn anyone please help?

#include <iostream>
#include <cmath>
using namespace std;

int userChoice,choiceCount (0);

int displayMenu ();
//displays menu
void drawTriangle();
// draws an inverted triangle
void sumOfNumbers();
// takes 2 numbers and averages them and gives min and max
void sumOfNumbersSentinel();
//takes numbers gives averages and min and max
void powerOfNumbers();
//takes base and raises it to the power
void quitProgram ();
//quits program


// prompts the user
int main ()
{
    cout << "Welcome to the my program\n"
        << "I can do several things for you.\n"
        << "When you are ready to enter my program,\n"
        << "Hit any key\n"
        << endl;
    system("pause");
    system("cls");

  //Gives the user choices and asks for a choice

        do
        {
            cout << "Please choose from:\n"
                << "1. draw an inverted triangle of your chosen height using your chosen letter\n"
                << "2. enter some numbers and learn the sum, average, min and max of your inputs\n"
                << "3. same 2. but with a different way to end inputting\n"
                << "4. let me calculate a^b (a raised to the b) for whatever a and b you'd like\n"
                << "5. quit this program\n"
                << endl ;
            cin >> userChoice;
            if ((userChoice>5||userChoice<1))
                cout << "Not a valid choice try again: " << endl ;
                cin >> userChoice ;
            
            system("pause");
            system("cls");
            
            //Switch for user choice
            switch (userChoice);
             {

              case 1:  
                 drawTriangle ();
                break;


              case 2:  
                sumOfNumbers();
                break;

              case 3: 
                sumOfNumbersSentinel();
                break;

              case 4: 
                powerOfNumbers();
                break;

              case 5: 
                cout << "End of program" << endl;
                cout << "You have made "
                     << choiceCount
                     << " choice(s)"
                     <<endl;
                break;

            } 
 

            


        }while ((userChoice<=4||userChoice>=1)) ;// braces for do
    
}//main brace

void drawTriangle () 
 {     
    char letterChoice ;
    int length ;

    cout << "What letter do you want to use for your triangle?:  " << endl;
    cin >> letterChoice;
    cout << " What is your desired length:  " << endl;
    cin >> length;

    for( int outer=length; outer > 0; outer--) 
    {
        for(int inner=0; inner<outer; inner ++) 
        {
            cout << letterChoice ;
        }
        cout << endl;
        choiceCount++ ;
    }
}

void sumOfNumbers( )
{
    int num1, num2 ;
    double average ;

    cout << "Please enter your first number: " << endl;
    cin >> num1;
    cout << "Please enter your second number: " << endl;
    cin >> num2;

    average = (num1+num2)/2;

    cout <<" The average of your two numbers is: " 
        << average 
        <<endl ;
    if(num1>num2)
        cout << "The max number is "
        <<num1
        << " and the minimum is"
        <<num2
        <<endl ;

    else if (num2>num1)
        cout << "The max number is "
        <<num2
        << " and the minimum is"
        <<num1
        <<endl;
    choiceCount++ ;

}

void sumOfNumbersSentinel()

{
    const int stopValue (-999);
    int num1, num2,sum,numCount(0), numberCounter,numbers,max,min;
    double average;

    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision (2);

    while (numbers!=stopValue)
        cout << "Please enter the numbers you wish to average(enters -999 to end input): "<< endl ;
    cin >> numbers;
    sum+=numbers ;
    numCount++ ;

    if (numbers==stopValue)
        cout << "Processing numbers..." << endl;

    average=sum/numCount ;

    cout << "The average of your numbers are "
        <<average
        << endl;
    if(num1<num2)
        min=num1 ;
    else if (num1>max)
    max=num1 ;
    
        cout << "The max number is "
        <<max
        << " and the minimum is"
        <<min
        <<endl;
    choiceCount++ ;
}

    void powerOfNumbers ()
        {
  double base, exponent,result ;

      cout << "What will your base number be?; " << endl;    
      cin >> base;
      cout << "What will your exponent be?: " << endl;
      cin >> exponent;
      result=pow(base,exponent) ;
          cout << "Your final number is "
               << result
               << endl;
        }

Recommended Answers

All 5 Replies

Hi ,

switch (userChoice);

The problem is due to ';' at switch statement.

writing

switch (userChoice);

is same as

switch(userChoice) { }

that is the case statements are not belonging to this switch statement where it should be.

You should also put brackets around your first if statement, the way it is now it waits for two user inputs before moving on.

Also, your main loop control is a bit off.

do
        {
            //show the menu

            cin >> userChoice;

            if ((userChoice>5||userChoice<1))
                cout << "Not a valid choice try again: " << endl ;
                cin >> userChoice ;
            
            system("pause");  //  I'm not gonna comment on how annoying this is!
            system("cls");
            
            //Switch for user choice
            switch (userChoice);
             {
                  //cases 1-5

        
                break;    //break not needed after last case

            } 

        }while ((userChoice<=4||userChoice>=1)) ;// braces for do

You get input from user, then test for valid range. If not valid, you get another input. What if it's still bad input? Code falls into your switch, finds no match, loops back to top and shows menu. Awkward.

Why not let the switch do the work for you? Eliminate the if( ) test and add default case which gives the error message for out of range input. This gets rid of redundant processing.

Your do...while condition will never halt the loop. Any number less than equal to 4 or any number greater than equal to 1 will make the condition true. That includes all possible numbers. Better to test for the specific quit value:

}while( userChoice != 5 );

hey just a small question .. jus started out programmin in c ++ ..what does this command do in the above program??
system("pause");
system("cls");

thanx.,..
tonykjose@gmail.com

tonykjose.....welcome aboard once more...system("pause"); it will pause a program before it exits.The system("cls");...is a call to the OS command line, "cls" is a command on some operating systems that clears the screen.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.