c++ - 声明为变量的 C++ 字符串与在结构中读取的区别
问题描述
最近,在重新引入 C++ 时,我遇到了一个我编写此代码的练习。
#include<stdio.h>
#include <iostream>
#include<string>
using namespace std;
struct Student {
string name;
string address;
string telephoneNumber;
float grade1;
float grade2;
};
string situation;
int main () {
Student student;
situation = "failed";
float media;
scanf("%s", &student.name);
scanf("%s", &student.address);
scanf("%s", &student.telephoneNumber);
scanf("%f", &student.grade1);
scanf("%f", &student.grade2);
average = (student.grade1 + student.grade2) / 2;
if (average >= 7.0) {
situation = "pass";
}
printf("The average of the student %s is %4.1f\n", &student.name, average);
printf("And his/her situation is %s\n", situation.c_str());
system("PAUSE");
return 0;
}
有谁知道为什么要使用 printf 打印出结构属性 student.name 我不需要使用 .c_str() 并且需要它来打印情况值,如果它们都被声明为字符串,一个在 struct 内部,另一个作为变量。
解决方案
推荐阅读
- visual-studio-code - 使用 Visual Studio 代码段将浮点数转换为范围
- qt - 在 Qt Quick Controls 中隐藏 TextArea 时无法摆脱光标
- c# - 使用 VSTS 的数据工厂 V1 的 CI/CD 管道
- html - 如何使用 XSL 修复 XML 转换中的冒号问题
- angular - @types/googlemaps/index.d.ts' 不是模块
- javascript - 如果对象传播不是可迭代的,它如何工作?
- r - R字符串操作:我该如何优化这个?
- java - RecyclerView in Fragments Error inflating class
- android - 如何禁用recyclerview的内部滚动但保持外部列表滚动?(如材料设计指南中所示)
- julia - 范围之间的条件子集数组