尝试将用户输入传递给字符串(C ++)

时间:2019-11-19 19:12:53

标签: c++ string pass-by-reference

我目前正在研究字符串,并且正在研究这个简单的示例。我试图在 logSuccess 字符串中传递“生日” 用户输入。做了很多谷歌搜索,但还没有找到解决方案。有提示吗?

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

using namespace std;

std::string birthdate;

void printString(const std::string& string)
{
   std::cout << string << std::endl;
}

void getBirthdate()
{
   std::cout<<"When is your birthday? "<<std::endl;
   cin>>birthdate;
}

int main()
{
std::string name = std::string("Dame") + " hello!";
std::string logSuccess = std::string("Thank you! We will send you a postcard on ") + birthdate;


printString(name);
getBirthdate();
printString(logSuccess);


std::cin.get();
}

2 个答案:

答案 0 :(得分:3)

在创建消息并将其分配给logSuccess变量时,birthdate变量为空。从用户获得输入后,您想用数据填充logSuccess。

答案 1 :(得分:0)

您的错误在这里:

OK|master

应该是:

a = ['master', 'FAULT', None]
b = ['FAULT', None, 'master']
c = [None, 'master', 'FAULT']


def getMsg(state):
    d = []
    for i in state:
        if i == 'master':
            d.append('OK')
        else:
            d.append('CRITICAL')
    return(d)


print(getMsg(a))  # --> ['OK', 'CRITICAL', 'CRITICAL']
print(getMsg(b))  # --> ['CRITICAL', 'CRITICAL', 'OK']
print(getMsg(c))  # --> ['CRITICAL', 'OK', 'CRITICAL']