我不太知道如何声明字符串,(前典当程序员学习C++)

I don't know really well how to declare a string, (former pawn programmer learning C++)

提问人:Nicolas Ayala 提问时间:3/28/2022 最后编辑:Vlad from MoscowNicolas Ayala 更新时间:3/28/2022 访问量:177

问:

几年前,我是一名C++派生语言的程序员,这种语言叫做Pawn,现在我想尝试从C++开始,写一些东西来摆脱编程这个美好的东西

不,我以前从未使用过 C++ 编码,也从未使用过任何其他非 Pawn 的语言。

这是我第一次使用 stackoverflow,如果我问了一个愚蠢的问题,对不起。

所以,这是我的代码:

我试图像在 Pawn 中那样做代码所说的,几乎,好吧,我有错误。

#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; 
}

错误是这样的:

错误:将“const char [11]”赋值为“char [64]”的类型不兼容 20 |案例 1: finalMessage = “没关系”;

你可以想象其他的都是一样的。

我尝试阅读了很多“如何在 C++ 中声明字符串,但因为 2 件事......

  1. 我不懂高级编程术语(即初始化、常量、cout 等),我正在研究
  2. 英语不是我的第一语言(它是西班牙语),是的,也许我应该使用西班牙语论坛,但我正在努力习惯英语论坛,因为我将来应该使用这个论坛

...我无法解决这个问题。

C++ 数组 隐式转换 赋值运算符 字符串文字

评论

0赞 Some programmer dude 3/28/2022
你不能分配给一个数组,只能复制到它(例如 对于字符串)。或者,您可以创建一个指针 (),您可以将其分配给它。strcpyfinalMessagechar const* finalMessage;
5赞 Some programmer dude 3/28/2022
话虽如此,无论你用什么资源来学习C++,它似乎都在教你C,而不是C++。请投资一些像样的C++书籍来学习“正确的”C++。
0赞 user17732522 3/28/2022
您的代码看起来像您正在尝试编写 C,而不是 C++。所有特定于 C++ 的部分都不相关。从结构化材料中学习C++,例如推荐的书籍之一。
1赞 joni 3/28/2022
在我看来更像C。如果你真的想使用 C++,你应该创建一个而不是 char 数组,即 .然后,您只需要将调用替换为 s。finalMessagestd::stringstd::string finalmessage;printfstd::cout
2赞 Swift - Friday Pie 3/28/2022
你的一个错误是试图通过它们的相似性来关联两种语言。首先,Pawn 从来都不是基于 C++,它使用了 C 语法,但函数的相似性是由于相似的环境,而不是由于语言相似性。C++ 与 Pawn 和 C 有很大不同。

答:

0赞 QWERTYL 3/28/2022 #1

似乎您主要使用 C 概念而不是 C++。

在 C++ 中,我们将使用字符数组来代替。此外,我们将使用 (C 输入) 和 (C 输出) 作为输入和输出。请尝试以下操作:stringcincout

#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; 
}

评论

1赞 Some programmer dude 3/28/2022
#include <stdio.h>不需要。并且您缺少 .breakswitch
1赞 Drew Dormann 3/28/2022
我强烈建议不要推荐使用命名空间 std;。Stack Overflow 收到了无数关于因包含该讨厌的行而引起的神秘错误的问题。
0赞 QWERTYL 3/28/2022
@DrewDormann 我个人绝对从不使用命名空间 std,但作为初学者,查看可能比 .stringstd::string
0赞 Marko Borković 3/28/2022 #2

好吧,既然您正在尝试进入 C++,我建议放弃纯字符数组和 C 样式 IO( 和 )。取而代之的是,用更现代的 IO ( 和 ) 替换它们。printfscanfstd::stringstd::coutstd::cin

用现代 C++ 重写它看起来像这样

#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;
}

我不熟悉 pawn,但在 C++ 的 switch 语句中,如果您不想失败,则必须在下一个案例之前使用。break

此外,还只是指定默认情况下要使用标准命名空间。允许您键入而不是 。但是,这被认为是不良做法using namespace std;cout << "Something";std::cout << "Something"

1赞 Ted Lyngmo 3/28/2022 #3

由于你的代码中包含了相当多的问题,我做了一些更改并评论了它们:

//#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';
}
1赞 Vlad from Moscow 3/28/2022 #4

数组没有复制赋值运算符。

所以这样的任务

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

无效。

在这些声明中

    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.";

您使用的是具有静态存储持续时间的字符串文本。要使代码正确,只需将变量声明为具有以下类型:finalMessageconst char *

const char *finalMessage;

在这种情况下,在这样的作业中

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

字符串文本被隐式转换为指向其第一个元素的指针,并且此指针将被分配给指针。 无需在简单程序中使用该类型的对象。finalMessagestd::string

此外,您还需要使用语句中断在 switch 语句之外的每个标记语句之后传递控件

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;
}

至于这一行的评论

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

那么在你的程序中它是多余的,因为你的程序中没有使用命名空间中的名称。std

例如,标准 C++ 名称或在命名空间中声明。如果没有 using 指令,则需要使用限定名称,例如coutstringstd

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

包括您可以编写的 using 指令

using namespace std;
//...

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

还要注意 C 的标题名称,您应该以字母“c”为前缀,例如

#include <stdio.h>

你应该写

#include <cstdio>