c++ - Vscode/C++ - cout 无法输出完整句子且无法与整数连接

标签 c++

我正在学习 C++,并面临与 C++ 中的 std::cout 相关的问题。我正在使用 vscode 来编程。 下面是我的代码

    int main(){
    std::string sender_name;
    std::cout<<"Enter the sender name: ";
    std::getline(std::cin >> std::ws, sender_name);

    std::string recipient_name;
    std::cout<<"Enter the recipient name: ";
    std::getline(std::cin >> std::ws, recipient_name);

    int recipient_age=0;
    std::cout<<"Enter the recipient_age of recipient: ";
    std::cin>>recipient_age;

    std::string friend_name;
    std::cout<<"Enter your friend's name: ";
    std::getline(std::cin >> std::ws, friend_name);

    char friend_sex = 0;
    std::cout<<"Enter 'm' for friend male and 'f' for the other: ";
    std::cin>>friend_sex;

    std::cout<<"Dear " + recipient_name+"," << std::endl;
    std::cout<<std::endl;
    std::cout<<"How are you? I am fine. I miss you."<<std::endl;
    std::cout<<"Have you seen " +friend_name+" lately?"<<std::endl;

    if (friend_sex == 'm'){
        std::cout<<"if you see " + friend_name + " please ask him to call me."<<std::endl;
    }
    else if(friend_sex == 'f'){
        std::cout<<"if you see " + friend_name + " please ask her to call me."<<std::endl;
    }
    else{
        std::cout<<"if you see " + friend_name + " please ask him/her to call me."<<std::endl;
    }
    if(recipient_age <= 0 || recipient_age >= 110){
        std::cout<<"you're kidding!"<<std::endl;
    }
    else{
        std::cout<<"I heard that you just had a birthday and you are " + recipient_age;
        std::cout<<" years old." << std::endl;

        if(recipient_age < 12){
            std::cout<<"Next year you will be " + (recipient_age+1);
            std::cout<<"."<<std::endl;
        }
        if(recipient_age == 17)
            std::cout<<"Next year you will be able to vote." << std::endl;
        if(recipient_age > 70)
            std::cout<<"I hope you are enjoying retirement."<<std::endl;
    }
    std::cout<<std::endl;
    std::cout<<"Yours sincerely,"<<std::endl;
    std::cout<<std::endl;
    std::cout<<std::endl;
    std::cout<<sender_name<<std::endl;

    return 0;
}

我的终端输出缺少一些打印语句

Enter the sender name: Sender
Enter the recipient name: Receiver
Enter the recipient_age of recipient: 30
Enter your friend's name: Neighbour
Enter 'm' for friend male and 'f' for the other: f
Dear Receiver,

How are you? I am fine. I miss you.
Have you seen Neighbour lately?
if you see Neighbour please ask her to call me.
rthday and you are  years old.

Yours sincerely,

rthday and you are old. 应该是我听说你刚刚过了生日,你已经 30 岁了

我认为cout打印没有限制。有人可以给我一些关于这个问题的想法吗?

最佳答案

线路

std::cout<<"I heard that you just had a birthday and you are " + recipient_age;

是错误的,因为 +仅当给出两个 std::string 时才连接或std::string一侧是字符串文字(或其他以 null 结尾的 char* )。

如果一侧是字符串文字,另一侧是整数,则它不会执行您认为的操作,如下所示。

避免使用+用于串联。无论如何,它只适用于字符串。输出多个对象到std::cout的正确方法连续,格式化为字符串,重复 << :

std::cout << "I heard that you just had a birthday and you are " << recipient_age;

我建议您在使用 + 的其他地方执行相同的操作以避免此类误解。


什么+相反,当一侧是字符串文字而另一侧是整数时,它将获取字符串文字的地址并将整数添加到该地址,返回指针。因此,您将一个从字符串文字中间某处开始的指针传递给 std::cout << ,导致您所看到的行为。

关于c++ - Vscode/C++ - cout 无法输出完整句子且无法与整数连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59485788/

相关文章:

c++ - 我使用 ifstream 的模板函数有什么问题?

C++从相同的基本模板歧义多重继承

c++ - 如何检测 OSX 上的 <filesystem> 可用性?

c++ - 如何摆脱手动类模板参数规范

c++ - 组合 std::strings 和 C-Strings 导致缓冲区溢出

c++ - (Im) 使用可变参数模板完美转发

c++ - 如何在 GPU 上并行化 PDF 到 HTML 的转换?

c++ - 我应该对 float 使用位操作吗

c++ - 从派生引用到基引用的 static_cast 编译器错误

c++ - 容器 STL 的 typeid