Hello, I need some help with some code I have. The compiler is giving me weird errors and I am not sure how to fix it, especially because I dont see anything wrong with the way I wrote my code. It has to do with array of structs and declaring the variable and whatnot. Here is the code:

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

using namespace std;

struct MedalStruct
{
   string country;
   int gold, silver, bronze;
};

void initializeArray(MedalStruct []);

int main()
{
   MedalStruct medalArray[30];
   initializeArray(medalArray);
   for (int i = 0; i < 30; i++)
   {
      cout << medalArray.country[i] << "  " << medalArray.gold[i] << "  "
           << medalArray.silver[i] << "  " << medalArray.bronze[i] << endl;
   }
   return 0;
}

void initializeArray(MedalStruct medalArray[])
{
   int year;
   string event, firstName, lastName, medal, country;
   float time;
   
   ifstream inFile;
   inFile.open("ass1.data");
   
   if (!inFile)
   {
      cout << "File did open properly!";
      exit(0);
   }
   
   int count = 0;
   
   inFile >> year >> event >> firstName >> lastName >> medal
             >> country >> time;
   
   while (!inFile.eof())
   {	     
      for (int x = 0; x < count; x++)
      {
         if (country == medalArray.country[x])
	 {
	    if (medal == "BRONZE")
	    {
	       medalArray.bronze[x]++;
	    }
	    else if (medal == "SILVER")
	    {
	       medalArray.silver[x]++;
	    }
	    else if (medal =="GOLD")
	    {
	       medalArray.gold[x]++;
	    }
	    else
	    {
	       cout << "Not a proper medal type!";
	       exit(0);
	    }
	 }
	 else
	 {
	    medalArray.country[count] = country;
	    
	    if (medal == "BRONZE")
	    {
	       medalArray.bronze[x]++;
	    }
	    else if (medal == "SILVER")
	    {
	       medalArray.silver[x]++;
	    }
	    else if (medal =="GOLD")
	    {
	       medalArray.gold[x]++;
	    }
	    else
	    {
	       cout << "Not a proper medal type!";
	       exit(0);
	    }
	    
	    count++;
	 }	 	
      } 
      
      inFile >> year >> event >> firstName >> lastName >> medal
             >> country >> time;
   }
   
   inFile.close();
}

And here are my errors....

bash-2.04$ CC -o proj1.out proj1.cpp
"proj1.cpp", line 22: Error: MedalStruct* is not a structure type.
"proj1.cpp", line 22: Error: MedalStruct* is not a structure type.
"proj1.cpp", line 23: Error: MedalStruct* is not a structure type.
"proj1.cpp", line 23: Error: MedalStruct* is not a structure type.
"proj1.cpp", line 52: Error: Variable medalArray is not a structure.
"proj1.cpp", line 56: Error: Variable medalArray is not a structure.
"proj1.cpp", line 60: Error: Variable medalArray is not a structure.
"proj1.cpp", line 64: Error: Variable medalArray is not a structure.
"proj1.cpp", line 74: Error: Variable medalArray is not a structure.
"proj1.cpp", line 78: Error: Variable medalArray is not a structure.
"proj1.cpp", line 82: Error: Variable medalArray is not a structure.
"proj1.cpp", line 86: Error: Variable medalArray is not a structure.
12 Error(s) detected.

Any help would be greatly appreciated!

>>medalArray.country
That's the wrong way to access the array medalArray[i].country BTW: you should rename that array to something else to prevent confusion (you and your prof) between the structure name and the array name.

I think the error for MedalStruct can be avaoided using "Struct MedalStruct" or you can typedef the "struct MedalStruct" to a single name and use it. Also there seems to be a logical error in your code. Please initialize the count with 1 otherwise the for loop will not work

This article has been dead for over six months. Start a new discussion instead.