So here is another assignment I am working on. I can't get to my schools computer lab due to family, job, etc.
Here is the assignment:
Structs
file name: lab2.cpp
1. (3 points)
Define a struct, named FullName, that contains the following fields:
lastname - a string
firstname - a string
midInitial - a char
2. (4 points)
Define a struct, named Student, that contains the following fields:
name - a FullName (the structure from section a)
tests - an array for storing 3 floats
average - a float
lettergrade - a char
3. (2 points)
In a main() function, declare a variable, named roster, that is an array
(size 24) of your new Student structure type.
4. (2 points)
Write a function with the following prototype:
float DetermineAverage(const float[3]);
The function should accept an array of 3 floats as a parameter and then
should return the average of the three numbers.
5. (2 points)
Write a function with the following prototype:
char DetermineLetterGrade(float);
The function should accept a float(test grade) as a parameter and should
return the corresponding letter grade based on the following grading scale:
90% - 100%: A
80% - 89%: B
70% - 79%: C
60% - 69%: D
Below 60%: F
6. (6 points)
Write a function with the following prototype:
Student GetData();
The function will prompt the user to enter her/his name and three test
scores. In the function, call DetermineAverage() to find the Student’s
average and DetermineLetterGrade() to find the Student’s lettergrade. Store
the Student’s information in a temporary Student structure (declared inside
the function), and then return the temporary Student structure.
7. (3 points)
Write a function with the following prototype:
void PrintStudent(const Student);
The function should display the student’s information as follows:
Student Name: John A. Doe
Test 1: 80
Test 2: 90
Test 3: 84
Average: 84.7
Final Grade: B
8. (3 points)
Finally, using the array variable you declared in part (3), write a call to
your GetData() function in main() that assigns the function’s return value
to the last element in your roster array of Student structures.
Then call PrintStudent() to display the student information.

Here is what I have coded, along with my errors:

//lab2.cpp
//Jan. 13 2015
//Tadd O'Bannion

#include<iostream>
#include<string>

using namespace std;

struct FullName
{
//properties(attributes) (data members).
    string lastname, firstname;
    char midInitial;

    //FullName getNewName()
};

struct Student
{
    FullName name;
    float tests[3];
    float average;
    char lettergrade;

};


    //constructor - function with the same name as struct with no return type
    //The purpose of the constructor is to initialize data when an instance of struct is declared
    Student()
    {
        for(int i =0; i<3; i++)
            {
            tests[i]=0.0;
            }


}

//This function prototype accepts an array of 3 floats as parameters and returns the average of the three numbers.
float DetermineAverage(const float[3]);
    for (int i=0; i<3;i++)
        {
        cout<< " Please enter a grade. " <<endl;
        cin>>tests;
//This function prototype accepts the test scores and assigns a letter grade.
char DetermineLetterGrade(float);

//Need to define this function through structs...nevermind, it was defined already.
Student GetData();

void PrintStudent(const Student);


int main()
{

int roster[24];

DetermineAverage;

DetermineLetterGrade;

PrintStudent;



return 0;
}

//Function Definitions.
float DetermineAverage (const float temp[3])
    {
    float total = 0.0;
    for (int i =0; i<3; i++)
    {
    total += tests[i];
    }
    average = total / 3;
    return average;
    }
char DetermineLetterGrade (float grade)
    {
    if (grade >= 90)
    return 'A';
    else if (grade >=80 && <= 89)
    return 'B';
    else if (grade >=70 && <= 79)
    return 'C';
    else if (grade >=60 && <= 69)
    return 'D';
    else if (grade < 60)
    return 'F';
    }

void PrintStudent (const Student)
{
cout<< " Please_enter_your_first_name: " <<endl;
cin>>firstname;
cout<< " Please_enter_your_last_name: " <<endl;
cin>>lastname;
cout<< " Please_enter_your_middle_initial: " <<endl;
cin>>midInitial;
cout<< " Student Name:    "<<firstname<<midInitial<<lastname;<<endl;
cout<< " Test 1:          "<<tests[0];
cout<< " Test 2:          "<<tests[1];
cout<< " Test 3:          "<<tests[2];
cout<< " Average:         "<<average;
cout<< " Final Grade:     "<<grade;
}

When I try to compile I get the errors:
[centos@localhost lab2]$ g++ lab22.cpp
lab22.cpp:19: error: declaration does not declare anything
lab22.cpp:28: error: ISO C++ forbids declaration of ‘Student’ with no type
lab22.cpp:40: error: expected unqualified-id before ‘for’
lab22.cpp:40: error: expected constructor, destructor, or type conversion before ‘<’ token
lab22.cpp:40: error: expected constructor, destructor, or type conversion before ‘++’ token

//I should add, I'm running CentOS on virtualbox.
Thanks in advance for the advice.

Recommended Answers

All 4 Replies

The Student function definition in line 31 is in conflict with the Student structure definition. Change the name of one or the other.

Lines 43-46 don't compute, are missing a closing brace, and since they aren't part of a function definition and are outside of main(), this is a short trip to compiler hell.

I'm not sure why the error on line 19 except line 13 string lastname, firstname; may be the cause. Try this instead: string lastname; string firstname;

Since the errors are mostly for lines other than the problematical ones, either your editor has a problem, or you compiled a different version of the code to get these errors.

And finally, if you are taking this class, then you need to find time to do the work - family, job, etc. notwithstanding.

Thank you for your input, and while I understand your final statement, as a lot of people come on here to try to get the answers, I find all the time that I can. I am asking for advice and direction, not homework solutions.
I have researched the web enough to know how annoyed forum members get when asked to do homework for people. I will look into this more closely and take your advice, and I apologize if the perception is that I was complaining about my life situation.

So after a bit in the lab, with some one on one guidance, here is the solution:

//lab2
//Tadd O'bannion    
//Jan 13, 2015

#include<iostream>
#include<string>

using namespace std;

struct FullName
{
    //properties(attributes) (data members).
    string lastname,firstname;
    char midInitial;

    //constructor for FullName
    FullName()
    {
        lastname = "Your_last_Name";
        firstname = "Your_First_Name";
        midInitial = 'I';
    }
};


struct Student
    {
    //properties(attributes) (data members).
    FullName name;
    float tests[3];
    float average;
    char letterGrade;


    //constructor for Student
    Student()
        {
        for(int i =0; i<3; i++)
            {
            tests[i]=0.0;
            }


        }
    };
//function prototypes
float DetermineAverage(const float[3]);

char DetermineLetterGrade(float); 

Student GetData();

void PrintStudent(const Student);


int main()
{
Student roster[24];



roster[23] =GetData();

PrintStudent(roster[23]);


return 0;
}


float DetermineAverage(const float tempTests[3])    
{

    float average=0.0;
    float total=0.0;
    for (int i =0; i<3; i++)
    {
    total += tempTests[i];
    }
    average = total / 3;
    return average;
}

char DetermineLetterGrade(float tempScore)
{

    char letterGrade;
    if (tempScore>=90)
    letterGrade= 'A';
    else if (tempScore >=80 && tempScore <= 89)
    letterGrade= 'B';
    else if (tempScore >=70 && tempScore <= 79)
    letterGrade=  'C';
    else if (tempScore >=60 && tempScore <= 69)
    letterGrade=  'D';
    else if (tempScore < 60)
    letterGrade=  'F';
    return letterGrade;
}

Student GetData()
{
Student tempStudent;



    cout<< " Please_enter_your_first_name: "<<endl;
    cin>> tempStudent.name.firstname;
    cout<< " Please_enter_your_last_name: "<<endl;
    cin>> tempStudent.name.lastname;
    cout<< " Please_enter_your_middle_initial: "<<endl;
    cin>> tempStudent.name.midInitial;
    cout<< " Please_enter_the_first_test_score: "<<endl;
    cin>>tempStudent.tests[0];
    cout<< " Please_enter_the_second_test_score: "<<endl;
    cin>>tempStudent.tests[1];
    cout<< " Please_enter_the_third_test_score: "<<endl;
    cin>>tempStudent.tests[2];
    tempStudent.average= DetermineAverage(tempStudent.tests);

    tempStudent.letterGrade= DetermineLetterGrade(tempStudent.average);
    return tempStudent;
}

void PrintStudent(const Student tempStudent)
{
cout<< " Student Name:    "<<tempStudent.name.firstname<<tempStudent.name.midInitial<<tempStudent.name.lastname<<endl;
cout<< " Test 1:          "<<tempStudent.tests[0]<<endl;
cout<< " Test 2:          "<<tempStudent.tests[1]<<endl;
cout<< " Test 3:          "<<tempStudent.tests[2]<<endl;
cout<< " Average:         "<<tempStudent.average<<endl;
cout<< " Final Grade:     "<<tempStudent.letterGrade<<endl;   

}       

Just thought I would post it for reference. Thanks Rubberman for your help.

NP. And congratulations! I know how much sacrifice people make trying to juggle family, job, school. Been there, done that! Anyway, keep posting here when you need help, and I at least will help you as much as reasonable. "Don't just give them a fish. Teach them to fish." :-) Yeah, not the original quote, but it works for me!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.