How to enter variables in system?


I am trying to make a code in which the user enters a number and through the system (color) command can change the color. I already tried to change the variables to char, string, (I do not know if I was doing it right), and they give me errors related to the types of variables. When it manages to work, what happens is that the characters are running, if I enter the number two, I will "lor" instead of "color" This is the code:

#include <iostream>  
#include <cstdlib>
#include <string>

int opcion;

using namespace std;
int main()
    int seleccion;
    char color[20];
    do {
        cout<<"\n 1. Cambiar color "<<endl<<" 2. Salir"<<endl<<"\n Seleccione una opcion: ";
            case 1: {
                cout<<"\n\t - CAMBIAR COLOR - ";
                cout<<"\n 0. Negro"<<endl<<" 1. Azul"<<endl<<" 2. Verde"<<endl<<" 3. Aguamarina"<<endl<<" 4. Rojo"<<endl<<" 5. Purpura"<<endl<<" 6. Amarillo"<<endl<<" 7. Blanco"<<endl<<" 8. Gris"<<endl<<" 9. Azul claro"<<endl;
                cout<<"\n Ingrese el color que desea: ";
                seleccion = itoa(seleccion.c_str());
                color[20]="color "+seleccion;
            case 2:
                cout<<"\n Aplicacion finalizada! "<<endl;
                cout<<"\n Opcion incorrecta!!";
    } while (opcion!=2);

I would appreciate an answer.

asked by Juan Tinoco 09.09.2018 в 05:48

1 answer

color[20]="color "+seleccion;

+ does not serve to concatenate char * .

The compiler identifies "color " as a pointer to character, ( char * ) that points to a buffer containing the characters' c ',' o ',' l ',' o ',' r ',' \ 0 '.

The% co_of% adds 2. To the pointer (arithmetic of pointers). So the pointer advances two characters and is pointing to the 'l' of the buffer mentioned above.

In any case, the expression is still + seleccion and you assign it to char * , which is color[20] . This is undefined behavior ( undefined behavior , UB). C does not explain how the program has to behave, so the result can be anything.

If you want to work with character strings like C ( char and char[] ), use the existing functions for it ( char * , strcpy , etc). Besides, in C ++ you can use with strcat (which does overload string to mean concatenation) and do + when you need to get the value as a c_str() .

The experts that we have in C ++ I see that they recommend the use of char * , so I recommend the same thing. In any case, do one thing or the other, please review what are pointers and arrays in C / C ++ and the differences between the two.

An important note is that this code that you sample has to be showing many warnings but it seems that you do not pay attention to them, possibly due to ignorance. warnings usually give useful information; many things that in other programming languages would be compilation errors in C / C ++ are warnings .

Do not ignore the warnings unless you know what they are indicating and see that they will not affect your program.

answered by 09.09.2018 / 12:55