I have a couple of easy questions, but I'm not that good at C++ yet, so I need your assistance...

I'm using an array to store for:

1) Test grades
2) Project grades
3) Lab grades

I want it to print out like this:

Test Scores: 92 73 81

My program runs, but this is what I'm getting for my results:

Example:

Test Scores: 92
Lab Scores: 58
Project Scores: 32
Test Scores: 77
Lab Scores: 65
Project Scores: 25

It doesn't print out the last scores that I input for (Test, Project, and Labs)...It only print out 2 of each...but not 3 of each (numofgrades = 3)

1) How do I make it finish printing out 3 of each and not just 2 of each
2) How do I make it into the format Test: 92 99 63 etc.

Thanks..

Here is my C++ code:

#include <iostream>
#include <iomanip>

using namespace std;

const int numofgrades = 3;

void testgrades(int testscores[]);
void labgrades(int labscores[]);
void projectgrades(int projectscores[]);
void printresults(int testscores[],int labscores[],int projectscores[]);

int main ()
{

    cout<<fixed<<showpoint;
    cout.precision(2);


    int testscores[numofgrades];
    int labscores[numofgrades];
    int projectscores[numofgrades];


    testgrades(testscores);
    labgrades(labscores);
    projectgrades(projectscores);
    printresults(testscores,labscores,projectscores);

    system ("PAUSE");
    return 0;
}

    void testgrades (int testscores[])
    {

           for (int i=1; i<=numofgrades; i++)
           {
               cout<<"Enter score for test #"<<i<<endl;
               cin>>testscores[i]; 

           }
    }

    void labgrades (int labscores[])
    {  
           for (int i=1; i<=numofgrades; i++)
           {
               cout<<"Enter score for lab #"<<i<<endl;
               cin>>labscores[i];               
           }

    }

    void projectgrades (int projectscores[])
    {

           for (int i=1; i<=numofgrades; i++)
           {
               cout<<"Enter score for project #"<<i<<endl;
               cin>>projectscores[i];
           }

    }


    void printresults(int testscores[],int labscores[],int projectscores[])
    {

           for (int i=1; i<numofgrades; i++)
           {
           cout<<"Test Scores: "<<testscores[i]<<endl;
           cout<<"Lab Scores: "<<labscores[i]<<endl;
           cout<<"Project Scores: "<<projectscores[i]<<endl;
           }

    }

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Firstly, to get the Test Scores:, Lab Scores:, and Project Scores: to stop printing a duplicate amount of time, you ill have to take them Out of your for loop. The reason you are only getting 2 resuts of scored for each is becuase of the wy you set your for loop up. Arrays start at 0, not 1. So when you say (int i=1; i<numofgrades; i++) you are going to process the loop twice. You would need to change it to: (int i=0; i<numofgrades; i++). Or you could also change it to: (int i=1; i<=numofgrades; i++). But I believe the previous is more common in this kind of situation since arrays start at index zero.

I tried what you told me about taking my cout statement out of the for-loop, but it gave me an error because of "i" in the cin statement.

I changed the for-loop around, but I still got the same thing. I'm originally using the 3rd suggestion you told me.


Anyone know what is wrong?

#include <iostream>
    void testgrades (int testscores[])
    {

           for (int i=1; i<=numofgrades; i++)
           {
               cout<<"Enter score for test #"<<i<<endl;
               cin>>testscores[i]; 

           }
    }
}

You have three arrays of integers. When you are reading them in, you are reading in 3 numbers into an array. The error you are getting is because on your cin, you are starting at 1. you are trying to read in testscores[1], testscores[2], and testscores[3]. This is wrong because testscores[3] is actually the fourth number in the array. You need to be storing the scores in the indexes: testscores[0], testscores[1], and testscores[2].

Since you are using the integer i to cout the test number, you might want to try this: cin >> testscrores[i-1];

Hope this helps.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Life is easier if you do array access and the loop accessing its elements starting at element 0. If you want to show the user numbering starting at 1, do so in the display.

int i;
int arr[3] = { 0 };

for( i = 0; i < 3; i++ )
{
     cout << "enter item #" << i+1 << " :";
     cin >> arr[i];
}

To display all the data entered, on one line, with a label as you're trying to do, take the label out of the loop, just do the data output inside the loop.

cout << "Data values: ";
for( i = 0; i < 3; i++ )
    cout << arr[i] << "  ";

In your printresults( ) function, you'll need separate loops for each of the three score types.

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