c++ 逐个获取多个输入[已关闭]

ss2ws0br  于 2023-03-05  发布在  其他
关注(0)|答案(1)|浏览(108)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题?**添加详细信息并通过editing this post阐明问题。

6天前关闭。
Improve this question
C++

#include <iostream>
using namespace std;

int main() {

    char Name;
    cout<<"Enter name ";
    cin>>Name;

    int Age;
    cout<<"Enter age ";
    cin>>Age;
   

    bool Gender; //0 is female and 1 is male
    cout<<"Enter Gender, {O for female, and any other number for male} ";
    cin>>Gender;
   

    bool Married;
    cout<<"Is he/she married, {0 for unmarried and any other number for married} ";
    cin>>Married;

}

我想打印一个语句,然后继续获取输入。获取输入后打印另一个语句,然后获取另一个输入。当前,它打印第一个语句,获取输入,然后打印其余的语句。

epggiuax

epggiuax1#

Name的类型从char替换为std::string。正如上面所注解的,类型char只能包含单个字符,因此所有其他字符都将传播到下面的cin

#include <iostream>
#include <string>
using namespace std;

int main() {
    std::string Name;
    cout<<"Enter name ";
    cin>>Name;

    int Age;
    cout<<"Enter age ";
    cin>>Age;
   
    bool Gender; //0 is female and 1 is male
    cout<<"Enter Gender, {O for female, and any other number for male} ";
    cin>>Gender;
   
    bool Married;
    cout<<"Is he/she married, {0 for unmarried and any other number for married} ";
    cin>>Married;
}

相关问题