0

I don't remember how to do this in C++:

#include <iostream>
#include <conio.h>
#include <string.h>

int main(){
    char categorias[3][20];
    /*char pais[3][20];
    char movimiento[3][50];
    char obras[100][50]; */

    categorias[0]="Alta";
    categorias[1]="Media";
    categorias[2]="Baja";
}

This throws this error: 19 15 C:\Users\dell\Desktop\Subasta.cpp [Error] incompatible types in assignment of 'const char [5]' to 'char [20]';

Long time ago I don't use C++ and I can´t solve the problem.

5
  • 4
    Use std::string instead of char[] and your life will be much easier. Commented Apr 25, 2020 at 16:38
  • 1
    This isn't array of strings but array of characters. To copy to an array of characters use strcopy or something. Though, I advise to use std::array<std::string,3> categorias instead of char[3][20]. Commented Apr 25, 2020 at 16:41
  • Note that Dev C++ is an IDE and not a compiler. I assume you are using some version of mingw. Commented Apr 25, 2020 at 16:42
  • 1
    main(){ in c++ you should not omit the int before main. Commented Apr 25, 2020 at 16:43
  • 1
    categorias[0]="Alta"; c-strings are not assignable. Replace char categorias[3][20]; with std::string categorias[3]; and the assignment will work. Commented Apr 25, 2020 at 16:43

2 Answers 2

3

Use C++ abstractions and containers from the standard library:

int main()
{
    using namespace std::string_literals;

    auto categorias = std::vector{"Alta"s, "Media"s, "Baja"s};

    // Or if you know you have a fixed number of categories:
    auto categorias = std::array{"Alta"s, "Media"s, "Baja"s};
}
Sign up to request clarification or add additional context in comments.

Comments

1

To copy the string literal into the char array

strcpy(categorias[0], "Alta");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.