我有以下代码提示用户输入他们的名字和状态:
#include <iostream>
#include <string>
int main()
{
std::string name;
std::string state;
if (std::cin >> name && std::getline(std::cin, state))
{
std::cout << "Your name is " << name << " and you live in " << state;
}
}
Run Code Online (Sandbox Code Playgroud)
我发现该名称已被成功提取,但不是州.这是输入和结果输出:
Run Code Online (Sandbox Code Playgroud)Input: "John" "New Hampshire" Output: "Your name is John and you live in "
为什么输出中省略了状态名称?我给出了正确的输入,但代码忽略了它.为什么会这样?
我正在尝试将cout命令转换为c ++中的getline命令.
这是我试图改变的代码....
for (int count=0; count < numberOfEmployees; count++)
{
cout << "Name: ";
cin >> employees[count].name;
cout << "Title: ";
cin >> employees[count].title;
cout << "SSNum: ";
cin >> employees[count].SSNum;
cout << "Salary: ";
cin >> employees[count].Salary;
cout << "Withholding Exemptions: ";
cin >> employees[count].Withholding_Exemptions;
}
Run Code Online (Sandbox Code Playgroud)
我正试图改变这一行:cin >> employees[count].name;这一行:cin >> employees[count].title;进入getlines.有人可以帮忙吗?
谢谢
我正在制作一个名叫吉尔伯特的机器人,他充满了愚蠢.到目前为止.
http://pastecode.org/index.php/view/36572371
让我们说他回答了一个问题,然后就会说
"问别人?(是/否)"
如果我说Y,它会说
"问别人?(是/否)"
再次.说Y再问你一次.我该如何解决?
// Created by Brad Gainsburg on 4/23/13.
// Copyright (c) 2013 Ostrich. All rights reserved.
#include <iostream>
#include <string>
using namespace std;
int main(int argc, const char* argv[]) {
cout << " _____ _ _ _ _ " << endl;
cout << " / ____(_) | | | | " << endl;
cout << "| | __ _| | |__ ___ _ __| |_ " << endl;
cout << "| | |_ | …Run Code Online (Sandbox Code Playgroud) 我正在编写一个程序,需要用户输入。我需要输入在单词之间包含空格。我在寻找解决方案时遇到了麻烦。在您提出问题之前,我已经在stackoverflow上尝试了其他多个问题,其中同一问题。这些是我尝试过的一些。 如何在C ++中cin空间?
我的代码的问题在于,一旦调用我的setBusinessName方法,它便会自动完成。它输出然后返回自身,而无需等待我输入数据。需要帮助...
string setBusinessName()
{
string name = "";
cout << "The name you desire for your business:";
getline(cin, name, '\n');
cout << name;
return name;
}
Run Code Online (Sandbox Code Playgroud)