-1

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

enter image description here

I'm stuck on this problem for more than 3 hours. Please help!

  • The `break` s are misplaced. It'll restart the inner loop for every iteration of the outer loops. You only need one loop to read and one to print. – Ted Lyngmo Feb 13 '23 at 23:09
  • Take a piece of pen of paper or launch a debugger and note down what will be values of `i` and `j` in your loops at every iteration. Then think why are there two nested loops at all. – Yksisarvinen Feb 13 '23 at 23:10
  • 3
    c++ does not support defining arrays with runtime values as their dimensions - use a vector. and perhaps you would not be stuck if you had read a decent c++ textbook – Neil Butterworth Feb 13 '23 at 23:11
  • 2
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Feb 13 '23 at 23:15
  • 1
    `int subjects;...float subjectMarks[subjects]; string subjectNames[subjects];` -- [This is not valid C++ code.](https://godbolt.org/z/5ndGxTGGT) – PaulMcKenzie Feb 13 '23 at 23:26

1 Answers1

1

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

#include <vector>

// ...

std::vector<float> subjectMarks(subjects);
std::vector<std::string> subjectNames(subjects);
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108