0

In this code I am basically building a program for a health club. The user inputs the desired membership and months and then it prints the total fees. The program runs perfectly for option 2&4 however for 1 it gives two outputs and for 3 none at all. I guess there is a slight bug in my code which I really cant find. The program is fine for 2 but tit calculates two values for 1 and it displays none for 3.

#include <iostream>
using namespace std;

void menu ()
{
    cout << "\n\t\tHealth Club Membership Menu\n\n";
    cout << "1. Standard Adult Membership\n";
    cout << "2. Child Membership\n";
    cout << "3. Senior Citizen Membership\n";
    cout << "4. Quit the Program\n\n";
    cout << "Enter your choice: ";

}
void fees(int member,int months)
{
    cout<<"The fees is Rs.";
    cout<<(member*months);
}

int main()
{

    int  months, choice;
    int adult=40;
    int child=20;
    int senior=50;
     do{menu();

    {

        cin>>choice;
        if (choice>=4)
        {
            cout<<"You have quit your membership\n";
            return 0;
        }
        cout<<"Please enter the number of months you want"<<endl;
        cin>>months;


        switch (choice)
        {
            case 0:
            choice==1;
            fees(adult,months);
        }
            case 1:
                {
                    choice==2;
                    fees(child,months);
                    }
        case 2:
            {
                choice==3;
                    fees(senior,months);

            }



    }
    }




}
    while((choice>=1) || (choice<4));
    return 0;
}
2
  • 1) Fix your indenting and general code formatting. 2) Review the break statement. Commented Oct 2, 2020 at 3:13
  • choice==1; does nothing. Commented Oct 2, 2020 at 3:54

2 Answers 2

1

I have corrected some parts of your program. Here is mine.

My Program

#include <iostream>
using namespace std;

void fees(int member, int months); //function prototype to calculate fees for members of differing age groups
void menu(); //function prototype to display options

void menu()
{
    cout << "\n\t\tHealth Club Membership Menu\n\n";
    cout << "1. Standard Adult Membership\n";
    cout << "2. Child Membership\n";
    cout << "3. Senior Citizen Membership\n";
    cout << "4. Quit the Program\n\n";
    cout << "Enter your choice: ";

}

void fees(int member,int months)
{
    cout<<"The fees is Rs.";
    cout<<(member*months);
}

int main()
{
    
    int months, choice;
    int senior=50;
    int adult=40;
    int child=20;
    
    menu(); //invoke menu method
    
    cin>>choice;
    if (choice == 4)
        {
            cout<<"You have quit your membership\n";
            return 0;
        }
        
        cout<<"Please enter the number of months you want"<<endl;
        cin>>months;


        switch (choice)
        {
            case 1:
            fees(adult,months);
            break;
        
            case 2:
            fees(child,months);
            break;
                    
            case 3:
            fees(senior,months);
            break;
            
            default:
            return 0; //exit program if choice is out of range
        }    
    return 0;
}

Output for choice 1

Enter your choice: 1
Please enter the number of months you want
3
The fees is Rs.120

Output for choice 3

Enter your choice: 3
Please enter the number of months you want
5
The fees is Rs.250

Output for choice 2

Enter your choice: 2
Please enter the number of months you want
6
The fees is Rs.120

As you can see I have tried indenting the code to make sure is neat. Like what v.p. said in the previous comment, since you have declared choice in the switch statement, there is no need to include choice==1 or etc. You will only put that in the if-else parentheses when you want to implement conditions. Eventually, all your choices will work. Most importantly, please read up C++ documentations properly and don't do for the sake of doing it or else you will struggle. Hope this answers your question:)

Sign up to request clarification or add additional context in comments.

3 Comments

Two things. 1) I wanted the program in a loop so that after completion once it automatically displays the menu and the program runs again. 2) Can you please guide me towards the authentic resources.
@Kh.Murad it doesn't make sense to run the whole thing again even if the choice is correct. Like it's not gonna make any sense from a logical perspective. You only need to loop when there is at least 1 set of instructions to execute in this instance here
And also the fact that this is a simple program, this can be done while loop for loop etc even try-catch exception will work too.
0

You need break statements. That's probably why you get two outputs for a choice of 1.

You have no case statement for a choice of 3. That's why you get no output for 3.

Why are you implementing a switch case for 0? Seems unnecessary.

By the way, All of your lines like choice==1 don't actually do anything in your code, nor do you need it. Get rid of them.

Lastly, fix your indentation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.