954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Arrays (Two easy questions)

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:

<code>#include &lt;iostream&gt;
#include &lt;iomanip&gt;

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&lt;&lt;fixed&lt;&lt;showpoint;
    cout.precision(2);
    
    
    int testscores[numofgrades];
    int labscores[numofgrades];
    int projectscores[numofgrades];
    
    
    testgrades(testscores);
    labgrades(labscores);
    projectgrades(projectscores);
    printresults(testscores,labscores,projectscores);
    
    system (&quot;PAUSE&quot;);
    return 0;
}
    
    void testgrades (int testscores[])
    {
           
           for (int i=1; i&lt;=numofgrades; i++)
           {
               cout&lt;&lt;&quot;Enter score for test #&quot;&lt;&lt;i&lt;&lt;endl;
               cin&gt;&gt;testscores[i]; 
                  
           }
    }
    
    void labgrades (int labscores[])
    {  
           for (int i=1; i&lt;=numofgrades; i++)
           {
               cout&lt;&lt;&quot;Enter score for lab #&quot;&lt;&lt;i&lt;&lt;endl;
               cin&gt;&gt;labscores[i];               
           }
         
    }

    void projectgrades (int projectscores[])
    {
           
           for (int i=1; i&lt;=numofgrades; i++)
           {
               cout&lt;&lt;&quot;Enter score for project #&quot;&lt;&lt;i&lt;&lt;endl;
               cin&gt;&gt;projectscores[i];
           }
           
    }
    
    
    void printresults(int testscores[],int labscores[],int projectscores[])
    {
           
           for (int i=1; i&lt;numofgrades; i++)
           {
           cout&lt;&lt;&quot;Test Scores: &quot;&lt;&lt;testscores[i]&lt;&lt;endl;
           cout&lt;&lt;&quot;Lab Scores: &quot;&lt;&lt;labscores[i]&lt;&lt;endl;
           cout&lt;&lt;&quot;Project Scores: &quot;&lt;&lt;projectscores[i]&lt;&lt;endl;
           }
           
    }</code>
NinjaLink
Posting Pro in Training
419 posts since Mar 2008
Reputation Points: 8
Solved Threads: 0
 

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

kylcrow
Junior Poster
124 posts since Apr 2007
Reputation Points: 11
Solved Threads: 2
 

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?

NinjaLink
Posting Pro in Training
419 posts since Mar 2008
Reputation Points: 8
Solved Threads: 0
 
#include void testgrades (int testscores[]) { for (int i=1; i<=numofgrades; i++) { cout<<"Enter score for test #"<>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.

kylcrow
Junior Poster
124 posts since Apr 2007
Reputation Points: 11
Solved Threads: 2
 

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.

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You