我的数组为什么不打印用户输入的不同主题名称?

huangapple go评论60阅读模式
英文:

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 &lt;iostream&gt;
using namespace std;

int main() {
    string fname, lname;
    int subjects;
    float totalMarks;
    cout &lt;&lt; &quot;Enter your first name: &quot;;
    cin &gt;&gt; fname;
    cout &lt;&lt; &quot;Enter your last name: &quot;;
    cin &gt;&gt; lname;
    cout &lt;&lt; &quot;Enter total marks: &quot;;
    cin &gt;&gt; totalMarks;
    cout &lt;&lt; &quot;Marks of how many subjects: &quot;;
    cin &gt;&gt; subjects;
    float subjectMarks[subjects];
    string subjectNames[subjects];
    for (int i = 0; i &lt; subjects; i++) {
            for (int j = 0; j &lt; subjects; j++) {
                cout &lt;&lt; &quot;Enter name of subject: &quot;;
                cin &gt;&gt; subjectNames[j];
                break; // I&#39;ve used &#39;break&#39; to exit the inner loop and go to outer loop for
                       // entering the marks obtained in the subject
                };
            cout &lt;&lt; &quot;Enter marks obtained in the subject: &quot;;
            cin &gt;&gt; subjectMarks[i];
    };

    cout &lt;&lt; &quot;Dear, &quot; &lt;&lt; fname &lt;&lt; &quot; &quot; &lt;&lt; lname &lt;&lt; &quot;! The subjects and their marks are shown below: \n&quot;;
    cout &lt;&lt; &quot;RESULTS IN SUBJECTS\n\n&quot;;
    for (int i = 0; i &lt; subjects; i++) {
        for (int j = 0; j &lt; subjects; j++) {
            cout &lt;&lt; subjectNames[j] &lt;&lt; &quot;\t\t&quot;;
            break;
        }
        cout &lt;&lt; subjectMarks[i] &lt;&lt; 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

enter image description here

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';
}

另外,请注意,您的 subjectMarkssubjectNames 是可变长度数组(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 &lt; subjects; i++) {
    std::cout &lt;&lt; &quot;Enter name of subject: &quot;;
    std::cin &gt;&gt; subjectNames[i];
    std::cout &lt;&lt; &quot;Enter marks obtained in the subject: &quot;;
    std::cin &gt;&gt; subjectMarks[i];
};
// writing
std::cout &lt;&lt; &quot;Dear, &quot; &lt;&lt; fname &lt;&lt; &quot; &quot; &lt;&lt; lname
          &lt;&lt; &quot;! The subjects and their marks are shown below:\n&quot;
             &quot;RESULTS IN SUBJECTS\n\n&quot;;
for (int i = 0; i &lt; subjects; i++) {
    std::cout &lt;&lt; subjectNames[i] &lt;&lt; &quot;\t\t&quot;
              &lt;&lt; subjectMarks[i] &lt;&lt; &#39;\n&#39;;
}

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::vectors instead:

#include &lt;vector&gt;

// ...

std::vector&lt;float&gt; subjectMarks(subjects);
std::vector&lt;std::string&gt; subjectNames(subjects);

huangapple
  • 本文由 发表于 2023年2月14日 07:03:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441994.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定