英文:
Why my array doesn't print different subjects name inputted by a user?
问题
以下是您提供的代码的翻译部分:
#include <iostream>
using namespace std;
int main() {
string fname, lname;
int subjects;
float totalMarks;
cout << "Enter your first name: ";
cin >> fname;
cout << "Enter your last name: ";
cin >> lname;
cout << "Enter total marks: ";
cin >> totalMarks;
cout << "Marks of how many subjects: ";
cin >> subjects;
float subjectMarks[subjects];
string subjectNames[subjects];
for (int i = 0; i < subjects; i++) {
for (int j = 0; j < subjects; j++) {
cout << "Enter name of subject: ";
cin >> subjectNames[j];
break; // I've used 'break' to exit the inner loop and go to the outer loop for
// entering the marks obtained in the subject
};
cout << "Enter marks obtained in the subject: ";
cin >> subjectMarks[i];
};
cout << "Dear, " << fname << " " << lname << "! The subjects and their marks are shown below: \n";
cout << "RESULTS IN SUBJECTS\n\n";
for (int i = 0; i < subjects; i++) {
for (int j = 0; j < subjects; j++) {
cout << subjectNames[j] << "\t\t";
break;
}
cout << subjectMarks[i] << endl;
};
return 0;
}
如果您需要进一步的帮助或解决问题,请提出具体的问题或需求。
英文:
The objective is to take input of subjects and their marks from a user and finally display them as a report card. Everything works as expected except that the compiler prints the name of the last subject entered for each different subject name in the final report.
Here is the C++ code that I tried:
#include <iostream>
using namespace std;
int main() {
string fname, lname;
int subjects;
float totalMarks;
cout << "Enter your first name: ";
cin >> fname;
cout << "Enter your last name: ";
cin >> lname;
cout << "Enter total marks: ";
cin >> totalMarks;
cout << "Marks of how many subjects: ";
cin >> subjects;
float subjectMarks[subjects];
string subjectNames[subjects];
for (int i = 0; i < subjects; i++) {
for (int j = 0; j < subjects; j++) {
cout << "Enter name of subject: ";
cin >> subjectNames[j];
break; // I've used 'break' to exit the inner loop and go to outer loop for
// entering the marks obtained in the subject
};
cout << "Enter marks obtained in the subject: ";
cin >> subjectMarks[i];
};
cout << "Dear, " << fname << " " << lname << "! The subjects and their marks are shown below: \n";
cout << "RESULTS IN SUBJECTS\n\n";
for (int i = 0; i < subjects; i++) {
for (int j = 0; j < subjects; j++) {
cout << subjectNames[j] << "\t\t";
break;
}
cout << subjectMarks[i] << endl;
};
return 0;
}
What I expected?
As you can see in the screenshot, 'Chemistry' is printed twice with different marks. It should have printed first 'Physics' with marks '80' and then 'Chemistry' with marks '70' as inputted by the user. I doubt there is a problem with subjectNames[] array.
But this is what actually happened
I'm stuck on this problem for more than 3 hours. Please help!
答案1
得分: 1
以下是您要求的代码部分的翻译:
// 读取
for (int i = 0; i < subjects; i++) {
std::cout << "输入科目名称:";
std::cin >> subjectNames[i];
std::cout << "输入科目的成绩:";
std::cin >> subjectMarks[i];
};
// 输出
std::cout << "亲爱的," << fname << " " << lname
<< "! 以下是各科目及其成绩:\n"
<< "科目成绩\n\n";
for (int i = 0; i < subjects; i++) {
std::cout << subjectNames[i] << "\t\t"
<< subjectMarks[i] << '\n';
}
另外,请注意,您的 subjectMarks
和 subjectNames
是可变长度数组(Variable Length Arrays,VLAs),标准C++不支持它们。只有一些编译器作为扩展支持它们。我建议您改用std::vector
来代替:
#include <vector>
// ...
std::vector<float> subjectMarks(subjects);
std::vector<std::string> subjectNames(subjects);
英文:
The loop for reading and the loop for writing is wrong. You don't need nested loops. Just have one variable when reading and one when writing:
// reading
for (int i = 0; i < subjects; i++) {
std::cout << "Enter name of subject: ";
std::cin >> subjectNames[i];
std::cout << "Enter marks obtained in the subject: ";
std::cin >> subjectMarks[i];
};
// writing
std::cout << "Dear, " << fname << " " << lname
<< "! The subjects and their marks are shown below:\n"
"RESULTS IN SUBJECTS\n\n";
for (int i = 0; i < subjects; i++) {
std::cout << subjectNames[i] << "\t\t"
<< subjectMarks[i] << '\n';
}
Also note that your subjectMarks
and subjectNames
are Variable Length Arrays (VLAs) and those aren't supported in standard C++. Only some compilers supports them as an extension. I recommend that you use std::vector
s instead:
#include <vector>
// ...
std::vector<float> subjectMarks(subjects);
std::vector<std::string> subjectNames(subjects);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论