-1

years ago i was a programmer in a C++ derivated language that was called Pawn, now i want to try to start in C++, write some things to unrust myself from this beatiful thing that is programming

No, i have never coded with C++ before and with any other language that was not Pawn.

This is the first time i use stackoverflow, sorry if i'm asking a stupid question.

So, this is my code:

I tried to do what the code says like its done in Pawn, almost, and well, i have errors.

#include <stdio.h>
#include <iostream> // I don't know if this is needed
#include <string> // The same for this
using namespace std; // I don't know what this means


int main()
{  
    int number;   

    printf("Write the number of croisssants you eat a day\n");
    scanf("%d", &number);
    printf("The number of croissants that you eat a day is %d", number);

    char finalMessage[64]; // This is the way it was made in Pawn, you declare a string and put a max for text

    switch(number)
    {
        // So here i try to use a switch to do this, like i was used to, yes i know how to use if but i'm trying to see if it's this metod is the same thing as it is in Pawn.
        case 0: finalMessage = "Oh, so you don't like croissants";
        case 1: finalMessage = "Thats okay";
        case 2: finalMessage = "Well, if one, why not two?";
        default: finalMessage = "Mmmm, i think they are too much.";
    }

    printf("%s", finalMessage);

    return 0; 
}

The errors are all this:

error: incompatible types in assignment of 'const char [11]' to 'char [64]' 20 | case 1: finalMessage = "Thats okay";

You can imagine the other ones being the same.

I tried reading a lot of "How to declare a string in C++ but because 2 things...

  1. I don't understand advanced programming terms (i.e. initialization, constant, cout, etc), i'm working in that
  2. English is not my first language (It's Spanish), yes, maybe i should use the Spanish forum but i'm trying to get used to the English one because i'm supposed to work with this one in the future

... i can't resolve this.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • You can't assign to an array, only copy to it (with e.g. `strcpy` for strings). *Or* you could make `finalMessage` a pointer (`char const* finalMessage;`), which you can assign to. – Some programmer dude Mar 27 '22 at 20:51
  • 5
    With that said, whatever resource you're using to learn C++, it seems to teach you C, not C++. Please invest in [some decent C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to learn "proper" C++. – Some programmer dude Mar 27 '22 at 20:51
  • Your code looks like you are trying to write C, not C++. All the C++-specific parts are not relevant. Learn C++ from structured material, e.g. one of the [recommended books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – user17732522 Mar 27 '22 at 20:52
  • 1
    Looks more like C to me. If you really want to use C++, you should make `finalMessage` a `std::string` instead of a char array, i.e. `std::string finalmessage;`. Then you only need to replace the `printf`calls by `std::cout`s. – joni Mar 27 '22 at 20:52
  • `using namespace std; // I don't know what this means` [Let me help you understand](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – Drew Dormann Mar 27 '22 at 20:59
  • 2
    One of your mistakes is to try associate two languages by their similarity. First, Pawn never was based on C++, it used C-syntax though and similarity in functions are due to similar environment, not due to language similarity. C++ is very different from Pawn and C. – Swift - Friday Pie Mar 27 '22 at 21:10

4 Answers4

1

Since you had quite a few questions packed into your code, I've made some changes and commented on them:

//#include <cstdio>    // this is the correct C++ header, I won't use it though

#include <iostream>  // I don't know if this is needed
// it's needed if you are using the C++ i/o streams, like std::cin and std::cout

#include <string>  // The same for this
// it's needed to be able to use std::string, which you are not, but you should

// using namespace std; // I don't know what this means
//  it means that you can skip writing std:: infront of all
//  classes/functions/constants that are placed in the std namespace
//  Don't use it for now. It's often causing problems until you know when to use it.

int main() {
    int number;

    //puts("Write the number of croisssants you eat a day");
    std::cout << "Write the number of croisssants you eat a day\n";

    /* The C way:
    if (scanf("%d", &number) != 1) {
        fprintf(stderr, "invalid number, bye bye\n");
        return 1;
    }
    */
    if(!(std::cin >> number)) { // the C++ way:
        std::cerr << "invalid number, bye bye\n";
        return 1;
    }

    // printf("The number of croissants that you eat a day is %d", number);
    // Using std::cout instead:
    std::cout << "The number of croissants that you eat a day is " << number << '\n';

    // char finalMessage[64];
    std::string finalMessage; // use a std::string instead

    switch (number) {
        case 0:
            finalMessage = "Oh, so you don't like croissants";
            break; // needed, or else it'll continue to the next case
        case 1:
            finalMessage = "Thats okay";
            break; // needed, or else it'll continue to the next case
        case 2:
            finalMessage = "Well, if one, why not two?";
            break; // needed, or else it'll continue to the next case
        default:
            finalMessage = "Mmmm, i think they are too much.";
    }

    //printf("%s\n", finalMessage.c_str());  // or
    //puts(finalMessage.c_str());            // or the c++ way:
    std::cout << finalMessage << '\n';
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
1

Arrays do not have the copy assignment operator.

So such an assignment like this

finalMessage = "Oh, so you don't like croissants";

is invalid.

In these statements

    case 0: finalMessage = "Oh, so you don't like croissants";
    case 1: finalMessage = "Thats okay";
    case 2: finalMessage = "Well, if one, why not two?";
    default: finalMessage = "Mmmm, i think they are too much.";

you are using string literals that have static storage duration. To make the code correct just declare the variable finalMessage as having the type const char *:

const char *finalMessage;

In this case in assignments like this

finalMessage = "Oh, so you don't like croissants";

the string literal is implicitly converted to a pointer to its first element and this pointer will be assigned to the pointer finalMessage. There is no need to use objects of the type std::string in your simple program.

Also you need to use the statement break to pass the control after each labeled statement outside the switch statement

switch(number)
{
    // So here i try to use a switch to do this, like i was used to, yes i know how to use if but i'm trying to see if it's this metod is the same thing as it is in Pawn.
    case 0: finalMessage = "Oh, so you don't like croissants";
            break;
    case 1: finalMessage = "Thats okay";
            break;
    case 2: finalMessage = "Well, if one, why not two?";
            break;
    default: finalMessage = "Mmmm, i think they are too much.";
            break;
}

As for the comment in this line

using namespace std; // I don't know what this means

then in your program it is redundant because neither name from the namespace std is used in your program.

Standard C++ names as for example cout or string are declared in the namespace std. Without the using directive you need to use qualified names as for example

std::string s( "Hello" );
std::cout << s << '\n';

Including the using directive you may write

using namespace std;
//...

string s( "Hello" );
cout << s << '\n';

Also pay attention to that names of headers from C you should prefix with the letter 'c' that is instead for example

#include <stdio.h>

you should write

#include <cstdio>
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

It seems like you're using mostly C concepts rather than C++.

In C++, we would use string instead of an array of chars. Additionally, we would use cin (C in) and cout (C out) for input and output. Try the following:

#include <iostream> // This is needed for cin/cout
#include <string> // This is needed to use string
using namespace std; // Basically this means you can write cin instead of std::cin, string instead of std::string, etc.


int main()
{  
    int number;   

    cout << "Write the number of croisssants you eat a day\n"; //cout instead of printf
    cin >> number; //cin instead of scanf. Note that you don't need to specify types
    cout << "The number of croissants that you eat a day is " << number;

    string finalMessage; // In C++, we use strings instead of char arrays

    switch(number)
    {
        // Switches should work the same way
        case 0: finalMessage = "Oh, so you don't like croissants"; break;
        case 1: finalMessage = "Thats okay"; break;
        case 2: finalMessage = "Well, if one, why not two?"; break;
        default: finalMessage = "Mmmm, i think they are too much.";
    }

    cout << finalMessage;

    return 0; 
}
QWERTYL
  • 1,355
  • 1
  • 7
  • 11
  • 1
    `#include ` isn't needed. And you're missing `break` statements in the `switch`. – Some programmer dude Mar 27 '22 at 21:04
  • 1
    I would urge [against recommending `using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). Stack Overflow gets countless questions about mysterious bugs caused by the inclusion of that detested line. – Drew Dormann Mar 27 '22 at 21:05
  • @DrewDormann I definitely never use namespace std personally, but as a beginner it may make more sense to see `string` rather than `std::string`. – QWERTYL Mar 27 '22 at 21:29
0

Well since you are trying to get into C++ I would recommend ditching the pure char arrays and the C style IO (printf and scanf). Instead replace them with the more modern std::string and IO (std::cout and std::cin).

Rewriting this in modern C++ would look like this

#include <iostream>
#include <string> 

using namespace std;

int main()
{  
    int number;   

    cout << "Write the number of croisssants you eat a day" << endl;
    cin >> number;
    cout << "The number of croissants that you eat a day is " << number << endl;

    string finalMessage;

    switch(number)
    {
        case 0: 
            finalMessage = "Oh, so you don't like croissants";
            break;
        case 1: 
            finalMessage = "Thats okay";
            break;
        case 2: 
            finalMessage = "Well, if one, why not two?";
            break;
        default: 
            finalMessage = "Mmmm, i think they are too much.";
    }

    cout << finalMessage << endl;
    
    return 0;
}

I am not familiar with pawn but in C++ in switch statements you have to use break before the next case if you don't want fallthrough.

Also using namespace std; just specifies that you are going to use the standard namespace by default. Allows you to type cout << "Something"; instead of std::cout << "Something". However it is considered bad practice

Marko Borković
  • 1,884
  • 1
  • 7
  • 22