So my code compiles and works almost like I want it to. This is basically a blackboard, the user enters the number of assignments and the weight per assignment in percentages. Then you can choose if you want to calculate the average grade for one or multiple students.
My problem here is that I wanted to do some error checking, and so I've been trying to get the total number of the sum of the weights, but when I print it it gives me the last weight instead of the sum... I'm trying to get a 100. Can someone tell me where did I messed up??
Please forgive the dumb question... This is my first time writing a program in c++ so I don't really understand much about what this --->[i] does.
#include <iostream>
using namespace std ;
int main(int argc, char **argv)
{
const int maxSize = 100 ;
float weight [ maxSize ] ;
float grades [ maxSize ] ;
int numAssignments ;
cout << " WELCOME TO BLACKBOARD ! " << endl ;
cout << " (This program was created for the calculation grades) " << endl ;
cout << " " << endl ;
cout << " To start, enter number of assignments: " ;
cin >> numAssignments ;
cout << " thanks! " ;
cout << " Now, enter weight of assignments " << endl ;
out << " (total sum of the weights should be 100) " << endl ;
float sum = 0.0 ;
int i = 0 ;
for ( int i = 0; i < numAssignments; i++ )
{
cout << " Assignment " << i + 1 << " : " ;
cin >> weight [i] ;
weight[i] /= 100 ;
sum += weight[i] * 100 ;
}
cout<<"sum is: "<< sum<<endl;
EDIT entry:
I figured out what I was doing wrong to get the total average. In this block:
float sum = 0.0 ;
int i = 0 ;
for ( int i = 0; i < numAssignments; i++ )
{
cout << " Assignment " << i + 1 << " : " ;
cin >> weight [i] ;
weight[i] /= 100 ;
sum += weight[i] * 100 ;
}
cout<<"sum is: "<< sum<<endl;
I added another variable called perc:
float sum = 0.0 ;
int i = 0 ;
for ( int i = 0; i < numAssignments; i++ )
{
cout << " Assignment " << i + 1 << " : " ;
cin >> weight [i] ;
float perc ;
perc = weight[i] /= 100 ;
sum += perc * 100 ;
}
cout<<"sum is: "<< sum<<endl;
it's kind of a cheap way to get the result I wanted but the suggestions with vectors was actually very useful for understanding how to work around it too.