Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C和x2B之间的差异+;声明为变量并在SCSTRUCT中读取的字符串 最近,在重新引入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; } #包括 #包括 #包括 使用名称空间std; 结构学生{ 字符串名; 字符串地址; 字符串电话号码; 浮球一级; 浮球二级; }; 串情况; int main(){ 学生; 情境=“失败”; 浮动介质; scanf(“%s”、&student.name); scanf(“%s”和学生地址); scanf(“%s”和学生电话号码); scanf(“%f”和1年级学生); scanf(“%f”和学生2年级); 平均=(学生1年级+学生2年级)/2; 如果(平均值>=7.0){ 情境=“通过”; } printf(“学生%s的平均值为%4.1f\n”,&student.name,average); printf(“他/她的情况是%s\n”,situation.c_str()); 系统(“暂停”); 返回0; }_C++_String_Struct_Types_Printf - Fatal编程技术网

C和x2B之间的差异+;声明为变量并在SCSTRUCT中读取的字符串 最近,在重新引入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; } #包括 #包括 #包括 使用名称空间std; 结构学生{ 字符串名; 字符串地址; 字符串电话号码; 浮球一级; 浮球二级; }; 串情况; int main(){ 学生; 情境=“失败”; 浮动介质; scanf(“%s”、&student.name); scanf(“%s”和学生地址); scanf(“%s”和学生电话号码); scanf(“%f”和1年级学生); scanf(“%f”和学生2年级); 平均=(学生1年级+学生2年级)/2; 如果(平均值>=7.0){ 情境=“通过”; } printf(“学生%s的平均值为%4.1f\n”,&student.name,average); printf(“他/她的情况是%s\n”,situation.c_str()); 系统(“暂停”); 返回0; }

C和x2B之间的差异+;声明为变量并在SCSTRUCT中读取的字符串 最近,在重新引入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; } #包括 #包括 #包括 使用名称空间std; 结构学生{ 字符串名; 字符串地址; 字符串电话号码; 浮球一级; 浮球二级; }; 串情况; int main(){ 学生; 情境=“失败”; 浮动介质; scanf(“%s”、&student.name); scanf(“%s”和学生地址); scanf(“%s”和学生电话号码); scanf(“%f”和1年级学生); scanf(“%f”和学生2年级); 平均=(学生1年级+学生2年级)/2; 如果(平均值>=7.0){ 情境=“通过”; } printf(“学生%s的平均值为%4.1f\n”,&student.name,average); printf(“他/她的情况是%s\n”,situation.c_str()); 系统(“暂停”); 返回0; },c++,string,struct,types,printf,C++,String,Struct,Types,Printf,有人知道为什么要用printf打印struct属性student.name吗?我不需要使用.c_str()来打印情境值,如果它们都声明为字符串,一个在struct内部,另一个作为变量。scanf函数是基于c的函数。它不知道C++ STD::String 是什么,因此程序显示了未定义的行为。为什么不使用std::cin?C++ C++书籍或学习材料在C++输入例程中显示了 SCANF的用法?使用printf也会出现类似错误。为什么不使用std::cout?如果使用printf()您不需要使用st

有人知道为什么要用printf打印struct属性student.name吗?我不需要使用.c_str()来打印情境值,如果它们都声明为字符串,一个在struct内部,另一个作为变量。

scanf函数是基于
c
的函数。它不知道C++ <代码> STD::String 是什么,因此程序显示了未定义的行为。为什么不使用
std::cin
?C++ C++书籍或学习材料在C++输入例程中显示了 SCANF的用法?使用
printf
也会出现类似错误。为什么不使用
std::cout
?如果使用
printf()
您不需要使用
std::string:::c_str()
,那是偶然的。如果没有它,
printf()
语句的行为是未定义的-未定义行为的一个问题是,有时,结果似乎完全有效,即使它实际上不是。您的问题与
struct
的成员和命名变量之间的差异无关。这一切都与这样一个事实有关,即未定义的行为意味着任何事情都可能发生——包括在两种情况下(直觉上)工作方式不同,这两种情况应该是相同的。谢谢你,@Peter这是一个完美的解释。