• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

220
Views
how to use character in array on switch statement?

when I compile the program it says 'd' is not an int so if anyone has idea how to change it to an int or make it work?

#include <iostream>
using namespace std;
int main()
{
    int k;
    cin >> k;
    if(k==2)
    {
        cout << "we have 3 main sub companies and other 2 companies that are growing\n";
        cout << "the 3 main sub companies are\n";
        cout << "        a-Aegre Food and Drinks\n";
        cout << "        b-Future Tech\n";
        cout << "        c-Hope Energy\n";
        char d[]={'a','b','c'};
        int d[0]=1,d[1]=2,d[2]=3;
        cout << "In which one of our companies do you want to invest plase enter the letters befor them ";
        cin  >>d;
        switch(d)//here it says d is n't an int//
        {
            case a:cout <<"we have 5 bonds";
            case b:cout <<"we have 3 bonds";
            case c:cout <<"we have 2 bonds";
        }
    }
}
about 3 years ago · Santiago Trujillo
3 answers
Answer question

0

It would be easier to solve this problem without an array:

#include <iostream>
using namespace std;
int main()
{
    int k;
    cin >> k;
    if(k==2)
    {
        cout << "We have 3 main sub-companies and other 2 companies that are growing.\n";
        cout << "The 3 main sub-companies are\n";
        cout << "        a-Aegre Food and Drinks\n";
        cout << "        b-Future Tech\n";
        cout << "        c-Hope Energy\n";

        char d;
        cout << "In which one of our companies do you want to invest? Please enter the letters before them: ";
        cin >> d;

        switch(d)
        {
            case 'a':
                cout << "We have 5 bonds";
                break;
            case 'b':
                cout << "We have 3 bonds";
                break;
            case 'c':
                cout << "We have 2 bonds";
                break;
            default:
                cout << "Invalid input!\n";
        }
    }
}

A char is an integer type, which can be used in a switch statement.

Note that I am using character literals in the case labels of the switch statement. Also, if you don't want to fall through to the next case, you must use break after every case in the switch statement.

about 3 years ago · Santiago Trujillo Report

0

You seem to be trying to make d an array of characters, an array of integers and a single value all at the same time? I think what you actually want to do is read a single character and compare it in a switch? You don't need an array at all:

#include <iostream>
using namespace std;
int main()
{
    int k;
    cin >> k;
    if(k==2)
    {
        cout << "we have 3 main sub companies and other 2 companies that are growing\n";
        cout << "the 3 main sub companies are\n";
        cout << "        a-Aegre Food and Drinks\n";
        cout << "        b-Future Tech\n";
        cout << "        c-Hope Energy\n";
        char company;
        cout << "In which one of our companies do you want to invest plase enter the letters befor them ";
        cin  >>company;
        switch(company)
        {
            case 'a':cout <<"we have 5 bonds";
                break;
            case 'b':cout <<"we have 3 bonds";
                break;
            case 'c':cout <<"we have 2 bonds";
                break;
        }
    }
}

Note you need a break between your cases otherwise a will trigger all three cases.

If what you were trying to do was map a company letter to a number then a std::map is what you need:

#include <iostream>
#include <map>
using namespace std;
int main()
{
    int k;
    cin >> k;
    if(k==2)
    {
        cout << "we have 3 main sub companies and other 2 companies that are growing\n";
        cout << "the 3 main sub companies are\n";
        cout << "        a-Aegre Food and Drinks\n";
        cout << "        b-Future Tech\n";
        cout << "        c-Hope Energy\n";
        std::map<char, int> companies = {{'a', 1}, {'b', 2}, {'c', 3}};
        char company;
        cout << "In which one of our companies do you want to invest plase enter the letters befor them ";
        cin  >>company;
        auto companyNumber = companies.find(company);
        if (companyNumber == companies.end())
        {
            cout << "invalid company\n";
            return 1;
        }
        switch(companyNumber->second)
        {
            case 1:cout <<"we have 5 bonds";
                break;
            case 2:cout <<"we have 3 bonds";
                break;
            case 3:cout <<"we have 2 bonds";
                break;
        }
    }
}
about 3 years ago · Santiago Trujillo Report

0

d is a character array, you can change elements like this, d[0] = '1', d[1] = '2', d[2] = '3';

But you can't do it like this, int d[0]=1,d[1]=2,d[2]=3;

because d is already declared as a character array. You can't declare it in another variable.

Some Examples:

char d[]={'a','b','c'}; // char array
    d[0]='1',d[1]='2',d[2]='3'; // update char array       
    cin  >>d[0]; // you can also take input like this (cin >> d[0]) or (cin >> d[1]) or (cin >> d[2]).
    switch(d[0]) // pass input 
    {
        case 'a':cout <<"we have 5 bonds";
        case 'b':cout <<"we have 3 bonds";
        case 'c':cout <<"we have 2 bonds";
    }
about 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error