I am writing a program for class but stuck. I need my program to pick adult, child, student or senior, if I enter any of them.

#include<iostream>
#include<iomanip>
#include<string.h>
#include<fstream>
using namespace std;

int main()
{
    int choice;
    int months;
    int Adult, Child, Student, Senior;

    cout<<"Enter the number of months that you want to sign up for?";
    cin>> months;
    cout<<"Enter membership type Adult, Child, Student, or Senior";
    cin>>choice;
    switch (choice)
    {
    case 'Adult': 

Recommended Answers

All 3 Replies

Switch cases only work for integral values. Ideally you'd somehow check the string and convert it to a number, or just compare directly. For example:

#include <cctype>
#include <iostream>
#include <string>

using namespace std;

int type_index(string type)
{
    // Normalize the string to a consistent case
    for (auto& ch : type) {
        ch = (char)tolower((unsigned char)ch);
    }

    // Check the value and convert to an index
    if (type == "adult") {
        return 0;
    }
    else if (type == "child") {
        return 1;
    }
    else if (type == "student") {
        return 2;
    }
    else if (type == "senior") {
        return 3;
    }
    else {
        return -1;
    }
}

int main()
{
    string type;

    cout << "Enter membership type (Adult, Child, Student or Senior): ";

    if (cin >> type) {
        cout << "The membership type index is " << type_index(type) << '\n';
    }
}

Ok I decided to do it like this but it is saying that "type" is undefined at the bottom

#include<iostream>
#include<iomanip>
#include<string.h>
#include<fstream>
#include<cctype>
using namespace std;

int main()
{
    const double ADULT = 40.0,
                 CHILD = 20.0,
                 STUDENT = 25.0,
                 SENIOR = 30.0;


    int choice;
    int months;
    char again;
    int Adult, Child, Student, Senior;



    cout<<"Enter the number of months that you want to sign up for?";
    cin>> months;
    cout<<"Enter membership type Adult, Child, Student, or Senior";
    cout<< "Do you need Yoga lesson? Y or y";
    cin>> again;
    while (again== 'Y'|| again== 'y');
    cout<< "Do you need Karate lesson? Y or y";
    cin>>again;
    while (again== 'Y'|| again== 'y');
    cout<<" Do you need Personal Trainer lesson? Y or y";
    cin>>again;
    while (again== 'Y'|| again== 'y');
     if (type == "adult") {
return 0;
}
else if (type == "child") {
return 1;
}
else if (type == "student") {
return 2;
}
else if (type == "senior") {
return 3;
}
else {
return -1;}}

type is a variable that you neglected to define. Take a look at the main() function in my example, type is a string that's filled in from the user after being prompted for a membership type.

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.