每当我在C ++中输入cin的空格时,我的代码都会跳过行 [英] My code skips lines whenever I type in a space into the cin in C++

查看:156
本文介绍了每当我在C ++中输入cin的空格时,我的代码都会跳过行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我键入一个单词输入或仅输入数字时,代码似乎工作正常但是只要我输入空格键它就会跳过代码中的行。这只是我们的第一个家庭作业,所以我知道它不应该是非常复杂的。它可能只是我的软件吗?有人愿意帮助我吗?



Code seems to work fine when I type in one word inputs, or only numbers but as soon as I type in a spacebar it just skips lines in the code. This is only the first homework that we have and so I know it's not supposed to be very complicated. Could it just be my software? Anyone care to help me?

// This program prompts the user a few general questions about themselves, the date, which IDE they are using, and which machine they have downloaded the IDE on to.
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string userName, currentDate, softwareName, machineName;

    //Ask the user his name.
    cout << "What is your name? ";
    cin >> userName;

    //Ask the user the date.
    cout << "What is the date? ";
    cin >> currentDate;

    //Asks the user which software he downloaded.
    cout << "What is the name the IDE software you downloaded? ";
    cin >> softwareName;

    //Asks the user which machine he downloaded his IDE on.
    cout << "Which machine did you download the IDE on? (ie. home computer, laptop, etc.) ";
    cin >> machineName;

    //Prints out the users inputs going downwards.
    cout << "" <<  userName << endl;
    cout << "" <<  currentDate << endl;
    cout << "" <<  softwareName << endl;
    cout << "" <<  machineName << endl;
    return 0;
}





我的尝试:



我尝试重写代码,学会了我必须添加字符串,并且只是尝试将所有内容格式化,但似乎得到相同的输出。



What I have tried:

I've tried rewriting the code, learned I had to add string, and just tried to format everything a little neater but seem to get the same output.

推荐答案

cin 总是在分隔符上停止,包括空格。

要读取整行直到ENTER,请使用 std :: getline - cppreference.com [ ^ ]。
cin always stops on a separator, which includes space.
To read a whole line up to the ENTER, use std::getline - cppreference.com[^] instead.


这篇关于每当我在C ++中输入cin的空格时,我的代码都会跳过行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆