Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

288
Visualizações
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";
        }
    }
}
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

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.

over 4 years ago · Santiago Trujillo Relatório

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;
        }
    }
}
over 4 years ago · Santiago Trujillo Relatório

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";
    }
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda