Ok, here's the deal. I have this homework project I'm working on and while I realize no one is here to do my homework I am a little confused right now with how to get a function to read into an array. I put in the entire code but I want to focus specifically on:

int bubbleSort()
{
    strcpy(stemp, name1);
    strcpy(name1, name2);
    strcpy(name2, stemp);
    strcpy(stemp, name3);
    strcpy(name3, name4);
    strcpy(name4, stemp);
    strcpy(stemp, name5);
    strcpy(name5 name6);
    strcpy(name6, stemp);

So in my code below how can I get this to read into an array such as (needs to be even as well do I need setw?):

Last Name First Name Id Test 1 Test 2 Test 3 Grade
Ant Adam 56789 65 72 68 D
Doe Jane 67890 54 64 52 F
Mix Tom 45678 98 83 74 B
Nerd Bobby 12345 95 96 99 A
Sunday Billy 34567 75 65 82 C
Thumb Tom 23456 87 93 77 B

Next, I need to know how to get the letter grades to work into the array? Below is the code.

int dataGrade() //function that shows table and grade
{
    
 
    if (average <= 100 && average >=90) //am I doing this right?
         grade = 'A';                   //I feel like something is missing?
    else if (average < 90 && average >= 80)
         grade = 'B';
    else if (average < 80 && average >= 70)
         grade = 'C';
    else if (average < 70 && average >= 60)
         grade = 'D';    
    else if (average < 60 && average >= 0)
         grade = 'F';
#include <iostream>
#include <string>
using namespace std;

int studentData()
{
    int i = 0;
    string studentInfo[7][7];
    char inputDone = 'y';

    for (i=0; inputDone == 'y'; i++)//notice the change here
    {
        cout << "Please Input student's first name : ";
        cin >> studentInfo[i][0];

        cout << "Please Input student's last name : ";
        cin >> studentInfo[i][1];

        cout << "Please Input student's  ID Number : ";
        cin >> studentInfo[i][2];

        cout << "Please Input student's Test 1 Grade : ";
        cin >> studentInfo[i][3];

        cout << "Please Input student's Test 2 Grade : ";
        cin >> studentInfo[i][4];

        cout << "Please Input student's Test 3 Grade : ";
        cin >> studentInfo[i][5];

        cout << "Are there anymore student grades to enter? (y/n)";
        cin >> inputDone;
    }//end of loop
}
int bubbleSort()
{
    strcpy(stemp, name1);
    strcpy(name1, name2);
    strcpy(name2, stemp);
    strcpy(stemp, name3);
    strcpy(name3, name4);
    strcpy(name4, stemp);
    strcpy(stemp, name5);
    strcpy(name5 name6);
    strcpy(name6, stemp);
    
}
int averageGrade() //function to determine the student's letter grade
{
    dataGrade()
    studentData()
    }

int dataGrade() //function that shows table and grade
{
    
 
    if (average <= 100 && average >=90) //am I doing this right?
         grade = 'A';                   //I feel like something is missing?
    else if (average < 90 && average >= 80)
         grade = 'B';
    else if (average < 80 && average >= 70)
         grade = 'C';
    else if (average < 70 && average >= 60)
         grade = 'D';    
    else if (average < 60 && average >= 0)
         grade = 'F';

             
}

int gradeFrequency() //function that will show table with letter grade 
                     //frequency per test
{
    
}
                     
int main()
{
    cout << "Please input the student's information " << endl;
    studentData();
    cout << "Below is a chart with the student's performance"
    averageGrade()
    << "and letter grade " << endl;
    system ("pause");
    return 0;
}

Build a grading program as follows:
1. Reads in the following data items:
a. Last Name
b. First Name
c. ID #
d. 3 Test Scores
2. Stores data in arrays and sorts the students alphabetically
3. Do not use any global variables, this means that you will pass information between functions using their argument and return value.
4. Calculates average and grade (tests are weighted equally and standard letter grades apply)
5. Displays
a. Student list including all items in 1 above and grade
b. Table showing number of As, Bs, Cs, Ds, and Fs by test
c. Both displays should include header lines
6. Should have functions for 1, 2, 4, 5a, 5b and main
7. Any additional features (such as displaying 5b as a bar chart) get creativity points
8. Use proper indenting, and supply comments (failure to do either can cost up to 10 points each)
9. If the submitted project fails to compile, there will be a 20 point deduction.
Example Output
Last Name First Name Id Test 1 Test 2 Test 3 Grade
Ant Adam 56789 65 72 68 D
Doe Jane 67890 54 64 52 F
Mix Tom 45678 98 83 74 B
Nerd Bobby 12345 95 96 99 A
Sunday Billy 34567 75 65 82 C
Thumb Tom 23456 87 93 77 B

Frequency
Test A B C D F
1 2 1 1 1 1
2 2 1 1 2 0
3 1 1 2 1 1

Above is what I'm supposed to finish up. I'm not trying to get someone to do my homework, I just need help understanding specifically the bubble sort and how to get the letter grades to work into that array. Any help will be greatly appreciated :)

Also, I know I have probably messed up a lot of this so any constructive criticism is appreciated. I am new to C++ and I have no idea about what most of this is or how exactly it works. If you decide to reply please go easy on me :) I'm clueless :)

Recommended Answers

All 3 Replies

Reading the information from the data file is very much similar to how you have it coded now to get it from the keyboard. The only difference is that you have to open an ifstream object.

ifstream in("infile.txt");
int i = 0;
while( in >> studentInfo[i][0] )
{
    in >> studentInfo[i][1];
    // blabla for all the other items

}

But why didn't you just use a structure to simplify things:

struct person
{
    std::string lastName;.
    std::string firstName;
    // blabla for all the other fields
};

Then you could have done this, which is a lot more meaningful than what you have done with simple arrays of strings.

struct Student studentInfo[20]; 
int i = 0;
ifstream in("infile.txt");
while( in >> studentInfo[i].lastName)
{
    in >> studentInfo[i].firstName;
    // blabla
}

And then, in C++ where are even easier ways to accomplish that, but you may not have studied c++ classes and vectors yet, so I'll not mention them any more.

Looking at the grade evaluation problem

int dataGrade() //function that shows table and grade
{
    if (average <= 100 && average >=90) //am I doing this right?
         grade = 'A';                   //I feel like something is missing?
    else if (average < 90 && average >= 80)
         grade = 'B';
    else if (average < 80 && average >= 70)
         grade = 'C';
    else if (average < 70 && average >= 60)
         grade = 'D';    
    else if (average < 60 && average >= 0)
         grade = 'F';

How is average transmitted to the function? You need an input parameter.

What datatype do you intend grade to be? You're assigning a character, but your function's return type is int. And no variable named grade has been declared.

You don't need the less than tests in the if...else if block. In order to get to any of the tests past the first, it must be true that the average is less than the limit value of the previous test.

average might be a data member .

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.