0

i am making a program which is supposed to collect input from the user by asking for id, name, units attempted, units earned and the gpa of a student and store that into a struct. Currently, when i input more than one student into the struct, it only outputs the last student in the array. What can i do to fix this?

Here is my code

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

struct Student {
string ID;      //an 8-digit student ID
string name;        //name of the student
unsigned unitsAttempted;//number of units attempted by the student
unsigned unitsEarned;   //number of units earned by the student
double gpa;     //the grade point average for the student
};


int main()
{
  Student students[10];//array for 10 Student Structs
  
  char answer = 'y';
  int number = 0;

  while ((answer == 'y') || (answer == 'Y')) {
   //prompt the user to enter the data
  {
    answer = ' ';
    int i = 0;

    cout << "Enter the students ID: \n" ;
    getline(cin, students[i].ID);

    cout << "\nEnter the name:\n";
    getline(cin, students[i].name);

    cout << "\nEnter the units attempted:\n";
    cin >> students[i].unitsAttempted;
    cin.ignore();

    cout << "\nEnter the units earned:\n";
    cin >> students[i].unitsEarned;
    cin.ignore();

    cout << "\nEnter the GPA: \n";
    cin >> students[i].gpa;
    cin.ignore();

    cout << "\nWould you like to add another student? y or n\n";  
    cin >> answer;
    cin.ignore();

    i++;
    number++;
  }
  }
  

  cout << left << setw(10) << "ID " << setw(20) <<"Name" << setw(20) <<"Units Attempted" << setw(20) <<"Units Earned" << setw(10) <<"GPA";
  cout << "\n===============================================================\n";

    for (int i = 0; i < number; i++)//display the students
    {
      cout << left << setw(10) << students[i].ID << setw(20) << students[i].name << setw(20) <<students[i].unitsAttempted << setw(20) <<students[i].unitsEarned << setw(10) <<students[i].gpa << endl << endl;

    }

  return 0;
}

Im still learning so please go easy on me, thanks.

Piklez
  • 13
  • 5
  • Glad to hear that you're still learning, so the next thing for you to learn is how to use your debugger to run your program one line at a time, inspect the values of all variables, and see exactly what your program is doing. Knowing how to effectively use a debugger is a required skill for every C++ developer, no exceptions. With your debugger's help you can quickly figure out the bugs in your code without asking anyone else for help, good luck! – Sam Varshavchik Sep 18 '20 at 02:20
  • do you know how to access the debugger in repl.it – Piklez Sep 18 '20 at 02:21
  • Never heard of it. Seem to be some web-based development tool. You can't effectively learn C++ like this. You need both [a good C++ textbook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), as well as a modern C++ compiler with a debugger. – Sam Varshavchik Sep 18 '20 at 02:22
  • what modern c++ compiler would you recommend – Piklez Sep 18 '20 at 02:23
  • gcc on Linux, of course. gdb as a debugger. Pretty hard debugger to learn, but once you do, it's invaluable. – Sam Varshavchik Sep 18 '20 at 02:27
  • visual studio on windows. It is very easy to debug with it. – bolov Sep 18 '20 at 03:10

1 Answers1

2

Currently, when i input more than one student into the struct, it only outputs the last student in the array.

It looks most likely that the scope of your i is incorrect:

    int i = 0;   //<-- this should be declared outside the `while` loop

As it stands, your i is scoped inside while loop, so it will be reset every time the while loop is entered. In other word, that i never increased.

artm
  • 17,291
  • 6
  • 38
  • 54