Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

293
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda