0
点赞
收藏
分享

微信扫一扫

C++结构体简单使用

小贴贴纸happy 2022-01-31 阅读 44
#include <iostream>
#include <string>
using namespace std;

struct student {
    // write your code here......
    string name;
    int age;
    double height;
    
};

int main() {
 
    student s;
    cin >> s.name >> s.age >> s.height;
    
    cout << s.name << " " << s.age << " " << s.height << endl;
     
    return 0;
}

太low的写法:

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

struct student {
    // write your code here......
    string name;
    int age;
    double height;
    
};

int main() {

    string name;
    int age;
    double height;
    
    getline(cin, name); // 当输入的字符串有空格时用getline ,例如:张 三
    cin >> age;  // cin 时,会自动回车。
    cin >> height;
    //cin >> name>> ege >> height; // 这三行可缩为一行
    
    student s;
    s.name = name;
    s.age = age;
    s.height = height;
    
    cout << s.name << " " << s.age << " " << s.height << endl;
     

    return 0;
}
举报

相关推荐

C++结构体

C++ 结构体

C++: 结构体

C++ 结构体 struct

c++(08-结构体)

C++从结构体到类的使用

0 条评论