我有以下代码提示用户输入他们的名字和状态:
#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 "
为什么输出中省略了状态名称?我给出了正确的输入,但代码忽略了它.为什么会这样?
我是编码的新手,我正在尝试do while使用嵌套if语句进行长循环,但是我遇到了让循环实际循环的问题.
我没有直接获得有关代码很长的项目的帮助,而是制作了一个简单的版本.它也不循环.它将结束并询问用户是否要再次尝试但是当输入"y"时它忽略if语句.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string sodaChoice;
char answer = 'n';
do
{
cout << "Please choose your favorite soda from the following: Rootbeer, Diet Coke, Coke, Sprite" << "\n";
getline(cin, sodaChoice);
if (sodaChoice == "Rootbeer")
{
cout << "Me too!" << "\n";
}
else if (sodaChoice == "Diet")
{
cout << "Not me :(" << "\n";
}
else if (sodaChoice == "Coke")
{
cout << "It's alright" …Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的程序做案例菜单。我总是收到交叉初始化错误,我以前从未见过这个错误。也许有人可以向我解释我的代码出了什么问题。
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <fstream>
using namespace std;
#define N_CARS 1
struct car{
string model;
int year;
double price;
bool available;
}cars [N_CARS];
void menu();
void mainMenu();
void writeToFile(ofstream &outputFile , const car& p)
{
outputFile << p.model << " "
<< p.year << " "
<< p.price << " "
<< p.available<<"\n";
}
int choice1 = 0;
int main(int argc, char** argv) {
menu();
return 0;
}
void menu() {
do …Run Code Online (Sandbox Code Playgroud) 我的问题/问题:
我现在的程序是使用cin >>以获取输入和除了我需要使用多数民众赞成在工作的罚款getline()在一个点上,我知道我不应该像混为一谈getline(),并cin.get()用cin >>.如何在不使用的情况下输入整数>>?
研究:
我试图对此进行研究,但我发现的每一个结果都说使用cin >> 这是我发现的最接近的结果.
我会很高兴找到避免使用的方法getline().
我正在使用Microsoft Visual Studio 2013
我目前有这个switch语句:
switch (option)
{
case 1:
getline(cin, newname);
cout << "What would you like your new username to be?\nName: ";
getline(cin, newname);
name = newname;
cout << "\nYour username is now '" << name << "' with your balance being $" << balance << "\n";
options();
break;
case 2:
cout << "\nOpenning cheat menu\n";
cheats();
break;
default:
cout << "\nExitting to main title...\n";
title();
break;
}
Run Code Online (Sandbox Code Playgroud)
你可以忽略案例2和默认情况,只是显示所以你知道这些案例是有效的.案例2和默认工作完全正常,但案例1表现得很奇怪.正如你在案例1中看到的,我有两个getline(cin,newname); 您可能认为我应该只有一个,但由于某种原因,我需要两个才能使此代码正常运行.
如果我只有其中一个代码跳过用户应该给出的输入.如果我有两个它正常工作.
我问过这个人他们也不明白,因此:为什么这样做.
我正在做 C++ 入门练习,并尝试编写一个接收单词和一行作为输入的程序。如果当我询问一个单词(使用 cin)时,我按 Enter 键,那么程序只会跳过下一行,并且不会询问一行(使用 getline)...如果我在 cin 中写下整个短语(例如“ hello beautifull world") 然后第一个单词 ("hello") 由 cin 捕获,其他两个单词 ("beautifull world") 由 getline 捕获。
我知道在 cin 中,当我输入空格时,它会切断输入。我不明白的是两件事:
1.- 为什么如果我用 Enter 结束输入(在 cin 中),它会跳过所有其余代码?(有解决办法吗?)
2.- 为什么如果我在 cin 中写下整个短语,它会在执行 cout << "enter a line" << endl; 之前将另外两个单词分配给 getline; ?
谢谢!对不起我的英语 C:
#include <iostream>
#include <string>
using namespace std;
int main() {
string word, line;
cout << "enter a word" << endl;
cin >> word;
cout << "enter a line" << endl;
getline(cin, line);
cout …Run Code Online (Sandbox Code Playgroud)