0

I need to create an array of struct with the struct being a student (with data types string FirstName, string LastName, int testScore, and char Grade). I have the logic figured out for the function prototypes, and I have learned a little bit of basic file i/o. I want 20 students in the array of structs, and the info is to be read in from .txt files. This is where I am having trouble. Here is my basic code.

#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std; 

struct studentType {
   string firstName;
   string lastName; 
   int testScore; 
   char Grade; 
};

int main()
{
   studentType students[20];

   int i;

   ifstream inputFile; 
   inputFile.open("testScores.txt"); 

   for (i = 0; i < 20; i++)
      inputFile >> students->testScore; 

   cout << "The test scores entered are: "; 

   for (i = 0; i < 20; i++)
      cout << " " << students->testScore; 

   return 0;
}
3
  • I also added a reference file "testScore.txt." When I output I get the first value from the text file 20 times. Is this taking the first value of testScore.txt and storing into all the testScore members of studentType? Commented Feb 9, 2018 at 21:44
  • 2
    Have you stepped through this in your debugger? What's the actual issue here? On what line does it behave unexpectedly? Commented Feb 9, 2018 at 21:45
  • to access a single array element use square brackets [] Commented Feb 9, 2018 at 21:45

1 Answer 1

2

You forget to index the element from the array when you access the array. Change:

students->testScore

To:

students[i].testScore

In both loops. The first version only does the changes to the first element (Since it can be accessed with *students), while the second adds the index to the pointer.

This is just another good reason to use std::vector or std::array since if you tried to dereference them as you did with the array here, you would get an obvious error.

On a side note, in C++, you should declare your loop variables inside your loops. Declaring them outside is something that used to be necessary before C99, but not C++.

Sign up to request clarification or add additional context in comments.

3 Comments

Oh, so I just added the value of the first line of .txt to the first element of the array member then printed it 20 times... :P lol thank you though it worked.On the side note, c++ doesn't use curly braces for loops? I'm guessing I should just declare the loop variable under the if statement?
@DrizztDo'Urden Yes, you are correct in what happened with your code. With the loop, I meant you should replace int i; for (i = 0; i < x; ++i) {} with for(int i = 0; i < x; ++i) {}. The latter limits the scope of the variable to a small area. Also, if you found this answer helpful, please consider accepting it by clicking the checkmark below the downvote button.
Ah so put the variable declaration in the loop parameters. Got it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.