关于STRING 和CHAR

来源:百度知道 编辑:UC知道 时间:2024/09/23 17:16:12
// Note:Your choice is C++ IDE
#include <iostream>
#include <string>
using namespace std;
struct Student
{
int num;
char name[20];
float score[3];
};

int main()
{
void print(Student);
Student stu;
stu.num=12345;
stu.name="li Fung";
stu.score[0]=67.5;
stu.score[1]=89;
stu.score[2]=78.5;
print(stu);

return 0;
}
void print(Student stu)
{
cout<<stu.num<<" "<<stu.name<<" "<<stu.score[0]<<" "<<stu.score[1]<<" "stu.score[2]<<endl;
}
请帮我看下CHAR有什么毛病 ·· ·这个程序在2009C++上运行不了。如何改呢。还有用STRING怎么换呢· ·谢谢大虾们了
我 知道哪错了· ·· 粗心了呵呵· ··那要用char 代替string 呢·· 谢谢

当然要用C++标准库的STRING了,简单方便
顺便帮你把写错的地方也改了

#include <iostream>
#include <string>
using namespace std;
struct Student
{
int num;
string name;
float score[3];
};

int main()
{
void print(Student);
Student stu;
stu.num=12345;
stu.name="li Fung";
stu.score[0]=67.5;
stu.score[1]=89;
stu.score[2]=78.5;
print(stu);

return 0;
}
void print(Student stu)
{
cout<< stu.num << " " << stu.name <<" "<<stu.score[0]<<" "<<stu.score[1]<<" "<<stu.score[2]<<endl;
}

如果要用char的话当然是用指针了:char *name; 就好了

#include <iostream>
#include <string>
using namespace std;
struct Student
{
int num;
char name[20];
float score[3];
};

int main()