Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

dynamic struct in c++ question?

ok so im taking a c++ class and im supposed create a dynamic structure but i cant seem to figure out what wrong with it. heres the coding.

#include <iostream>

#include <iomanip>

#include <string>

#include "Functions2.h"

using namespace std;

//Prototypes

void setTestScores(Students[] ,int);

void setSorting(Students[] , int);

double getAverage(Students[] , int);

void showArray(Students[] , int );

struct Students

{

string name;

double testScore;

};

int main()

{

int SIZE;

double Average;

Students student;

cout << "How many students will you be inputing test scores for? ";

cin >> SIZE;

Students* student = new Students[SIZE];

setTestScores(student, SIZE);

setSorting(student, SIZE);

showArray(student, SIZE);

getAverage(student, SIZE);

}

THIS IS THE Function2.h file

#include <iostream>

using namespace std;

void setTestScores(Students student[],int SIZE)

{

for (int count = 0; count < SIZE; count++)

{

cout << "What is the name of the student? ";

cin >> student[count].name;

cout << "What is the score for test number " << count + 1 << "?" << endl;

cin >> student[count].testScore;

}

}

void setSorting(Students testScores[],int SIZE)

{

//Declarations

double temp;

bool swap;

//Loop to organize and sort scores

do

{ swap = false;

for(int count = 0; count < SIZE; count++)

{

if (testScores[count] > testScores[count + 1])

{

temp = testScores[count];

testScores[count] = testScores[count +1];

testScores[count + 1]=temp;

swap=true;

}

}

} while (swap);

}

double getAverage(Students testScores[], int SIZE)

{

double total=0;

//Loops to add each test score to variable "total"

for (int count=1; count < SIZE+1; count++)

{

total += testScores[count];

}

//Returns average and stores intovariable average in int main()

return total/SIZE;

}

void showArray(Students temp[], int SIZE)

{

//Displays scores on screen

cout << "The Scores Sorted by ascending order are as followed: "<< endl;

for (int count = 1; count <= SIZE; count++)

{

cout << temp[count] << " ";

}

cout << endl;

}

Update:

I guess the question is how to properly pass a structure to a function, however thanks for the etiquette lesson, the book im using didnt really mention those things about the header. Ill change my habit if thats the standard. as for the errors; it give me various errors mostly small errors like "expected ; before }" where the function starts (not where the function is called) which doesnt make sense and if i do put the ; it just gives m another error, the funny thing is that intellisense says nothings wrong but theres like 40 errors in the error list fill with things like i mentioned or some saying i havent declared a variable but if i just rid my program of functions completely it runs well so creating the struct object is not the problem or atleast i dont think it is.

2 Answers

Relevance
  • 9 years ago
    Favorite Answer

    You didn't say what the symptoms were, but there is indeed something very wrong with that header file. It's a C++ program file and should be (a) named with a .cpp suffix and (b) compiled separately.

    What belongs in the header is prototypes for the external functions, and definitions for structs, classes, enums, and constants used when calling those functions. No code, and no data defintions. The header is meant to be included in every C/C++ source file that uses a group of functions. As a minimum it's included in two places: The source file that defines the functions and a source file that uses them. That's enough to cause the linker to choke on two separate definitions of the same external function.

    So, rename Function2.h to Function2.cpp, add it to your project if you're using an IDE like VC++ Express or Code::Blocks, and create a "functions.h" file that has the prototypes and the Students struct. Include that "functions.h" header in both the main cpp source and in Function2.cpp.

    Also, at the end of main(), add

    delete[] student;

    Get in the habit of deleting any objects you create with new.

  • 9 years ago

    You have taken array whose memory is allocated through new. Only that much dynamism is there in your problem.

    Tomorrow some more students arrives, what are you going to do?

    If you think of interms of linked lists then your problem becomes dynamic.

Still have questions? Get your answers by asking now.