C++ C++;字符数组不工作
我试图让这个程序工作,但得到奇数输出。我知道字符缓冲区在这里并不理想,但它是用于分配的,不是我自己设计的。这是我的密码:C++ C++;字符数组不工作,c++,C++,我试图让这个程序工作,但得到奇数输出。我知道字符缓冲区在这里并不理想,但它是用于分配的,不是我自己设计的。这是我的密码: #include <iostream> #include <string> #include <cstring> using namespace std; //Place your class definition here class student { public: void inputData(int a, c
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
//Place your class definition here
class student {
public:
void inputData(int a, char s[20], float e, float m, float sci);
void printData();
private:
int admno;
char sname[20];
float eng;
float math;
float science;
float total;
float ctotal();
};
int main () //This is your main driver. Do NOT make any changes to it
{
student obj ;
int a;
char s[20];
float e, m, sci;
cin >> a;
cin.getline(s,20);
cin.clear();
cin.ignore(10,'\n');
cin >> e;
cin >> m;
cin >> sci;
obj.inputData(a, s, e, m, sci);
obj.printData();
return 0;
}
//Place your class member function definitions here
void student::inputData(int a, char s[], float e, float m, float sci) {
admno = a;
*sname = *s;
eng = e;
math = m;
science = sci;
total = ctotal();
}
void student::printData() {
cout << "Admission number: " << admno << endl << "Student name: ";
for (int i = 0; i < 20; i++)
cout << sname[i];
cout << endl << "English: " << eng << endl << "Math: " << science << endl << "Science: " << science << endl << "Total: " << total;
}
float student::ctotal() {
return (eng + math + science);
}
以下是预期输出:
Admission number: 98745
Student name: Kyle Smith
English: 98
Math: 78
Science: 62
Total: 238
以下是实际输出:
Admission number: 98745
Student name: m ╩`uÄM■Å■ ║k`u
English: 98
Math: 62
Science: 62
Total: 238
请给出如何修复的建议。我必须坚持使用那个字符缓冲区,但不知道为什么会出现这种损坏。
谢谢
这将复制一个字符,而不是整个字符串。如果要复制整个字符串,需要使用
还是一个循环
char* src = s;
char* dst = sname;
while (src) {
*dst = *src;
++src;
++dst;
}
当然,您可以取消所有这些手动字符串处理,改为使用:
发生的情况如下:
cin >> a;
cin.getline(s,20);
cin.clear(); // never mind this, doesn't do anything in this context
cin.ignore(10,'\n');
int
被读取并存储在a
cin
现在包含按下Enter后剩余的\n
getline
立即尝试将其读入s
但将其丢弃,s
现在为空,但getline
操作完成cin.ignore
正在等待输入\n
或至少输入10个char
s,但流缓冲区现在为空李>
ignore
至少会获取其\n
现在已完成,但输入的字符串未存储在任何位置。它只是被忽略了。如果不忽略它,您将在输出中获得该字符串的第一个char
,但您没有长话短说,您的输入已中断。在
getline
之前输入第一个数字后,应清除该\n
。只有这样,在将您的名称存储在s
中后,才可以尝试将其传递给函数,将指针传递给空数组并期望它工作是没有意义的 使用调试器逐步调试,观察程序与预期的不同之处。简化您的生活,对文本使用std::string
,而不是字符数组。字符数组可能会出现溢出。此外,如果数组没有nul终止符,许多算法会一直进入内存的其他部分,直到找到nul。这复制了一个字符
,那么输出中至少会显示第一个字符,但事实并非如此
std::strcpy(sname, s);
char* src = s;
char* dst = sname;
while (src) {
*dst = *src;
++src;
++dst;
}
//Place your class definition here
class student {
public:
void inputData(int a, std::string s, float e, float m, float sci);
void printData();
private:
int admno;
std::string sname;
float eng;
float math;
float science;
float total;
float ctotal();
};
void student::inputData(int a, std::string s, float e, float m, float sci) {
admno = a;
sname = std::move(s);
eng = e;
math = m;
science = sci;
total = ctotal();
}
cin >> a;
cin.getline(s,20);
cin.clear(); // never mind this, doesn't do anything in this context
cin.ignore(10,'\n');