VSCODE / C ++-COUT无法输出完整的句子且无法与整数连接

时间:2019-12-26 08:28:17

标签: 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,

生日那天,您已经几岁了。应该是听说您刚过生日,并且您已经30岁

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

2 个答案:

答案 0 :(得分:3)

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 <<,从而导致您看到的行为。

答案 1 :(得分:1)

不能将整数连接到字符串常量的末尾。线

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

表示当recipient_age等于30时,从字符串开头的30个字符开始打印。

将该行替换为

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

您还需要使用打印"next year you will be "的代码来完成此操作。

相关问题