小编Alb*_*rto的帖子

使用cin >>的C++中变量的默认值

我在CodeBlocks IDE中用C++编写了这段代码,但是当我运行它时,如果它没有读取数字,它就不会给我-1,它给了我0.代码是否有问题?

#include "iostream"

using namespace std;

int main()
{
    cout<<"Please enter your first name and age:\n";
    string first_name="???"; //string variable
                            //("???" means "don't know the name")
    int age=-1; //integer variable (-1 means "don't know the age")
    cin>>first_name>>age; //read a string followed by an integer
    cout<<"Hello, " <<first_name<<" (age "<<age<<")\n";

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ c++11

7
推荐指数
1
解决办法
1476
查看次数

标签 统计

c++ ×1

c++11 ×1