我正在为我的学校项目使用 Turbo C++。我知道它很旧,我应该使用代码块或 MVC++,但我只是将它用于学校的简单程序。好的,我的问题是我无法打印出 char 数组。这是我的简单代码
#include <iostream.h>
#include <conio.h>
class abc
{
private:
int rno,m1,m2,tot;
char sname[10],grade;
public:
void getinput()
{
cout<<"Enter roll no:"<<endl;
cin>>rno;
cout<<"\nEnter mark 1:"<<endl;
cin>>m1;
cout<<"\nEnter mark 2:"<<endl;
cin>>m2;
cout<<"\nEnter student name:"<<endl;
cin>>sname;
cout<<endl;
// Getting the returns.
//tot = gettotal();
//grade = getgrade();
}
void gettotal()
{
tot = m1+m2;
// Returning the total to getinput's "tot" part.
//return (tot);
}
void getgrade()
{
if(tot >= 150)
{
grade = 'A';
}
else if(tot >= 100)
{
grade = 'B';
}
else
{
grade = 'C';
}
// Returning the total to getinput's "grade" part.
//return (grade);
}
void display()
{
cout<<sname<<"'s total grade ranks: "<<grade;
}
};
void main()
{
abc a;
a.getinput();
a.gettotal();
a.getgrade();
a.display();
}
如您所见,它要求标记 1 和标记 2,然后对其进行计算并根据标记打印“A”、“B”和“C”。
好吧,我想要的是打印“First”、“Second”和“Third”,而不是 A、B、C。
有人可以帮我解决这个问题吗
谢谢。
最佳答案
试试这个方法:
enum Grade{
A,
B,
C,
};
const char * GetGradeStr(Grade grade){
switch(grade){
case A:return "First";
case B:return "Second";
case C:return "Third";
}
}
char sname[10];
Grade grade;
...
grade = A;
...
cout<<sname<<"'s total grade ranks: "<<GetGradeStr(grade);
这使得 grade 成为一个枚举,这意味着只有 3 个值是有效值,然后将正确的字符串作为“String Literal”返回
关于c++ - 无法打印字符数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14316288/