-1

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.

  • 1
    If you don't know about arrays and how to use them, then I suggest you [find a good beginners book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to read. – Some programmer dude Jan 19 '17 at 09:41
  • http://www.cplusplus.com/doc/tutorial/arrays/ – jamek Jan 19 '17 at 09:41
  • 1
    Fixing the compilation error, [I can't reproduce that](http://ideone.com/w9tRaF). (If you retyped the code rather than copy and paste it, I suspect that your real code has `=`, or even `=+`, where this code has `+=`.) – molbdnilo Jan 19 '17 at 09:48
  • You're code works kinda ok for me: http://coliru.stacked-crooked.com/a/6d5d3af5add60bc2 (output: "sum is: 100") – YSC Jan 19 '17 at 09:48
  • If numAssignments>maxSize you might run into a segmentation fault – FloHe Jan 19 '17 at 09:51
  • Looks like this is a simple typo - voting to close as such. – Martin Bonner supports Monica Jan 19 '17 at 10:02
  • 1
    Couple of comments on the code: Change weight to `std::vector weight;`, then make loop be `for ( int i = 0; i < numAssignments; i++ ) { cout << " Assignment " << i + 1 << " : " ; float w; cin >> w; sum += w; weight.push_back(w/100); } std::vector grades(numAssignments);` This will mean you don't need to hard code a limit (limits are bad - you always end up exceeding them at an inconvenient time). Swapping the summation and the divide by 100 avoids having to multiply by 100 again. – Martin Bonner supports Monica Jan 19 '17 at 10:03
  • You have a useless variable, btw. You declare int i = 0 and then re-declare it in the for-loop initializer. I guess that you had some C-styled tutorials, declaring the iteration variable outside of the loop is pretty much C. Another thing, you might want to replace "using namespace std;" with more particular usages, like "using std::cout;". Thing is, std is quite big and uses some common names, like "sort". This can lead to name collisions. Also, it makes it obvious where something comes from. Everybody knows where cout comes from, but in other cases... – Aziuth Jan 19 '17 at 11:03
  • thanks!! this was a very informative response. I'm taking an "advance" class in programming so I can get better understanding of 3D softwares such as Houdini. It's more like a fast paced learning from the beginning. Anyways, the people with more experience in code and the professor were debating whether is easier to code with arrays or vectors. I thought arrays was going to be easier since it was the first thing we learned, but the use of vectors seem much more straightforward. – Alexandra López Jan 20 '17 at 14:05

1 Answers1

0

here's an update of my fixed code with vectors. it compiles and does mostly what I want... except for the fact that I suck at math can can't get a simple average done down at the bottom for the total weight per student's grade...

    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std ;

    int main(int argc, char **argv)
    {
        const int maxSize = 100 ;

        vector <float> weight (maxSize) ;


        cout << "              WELCOME TO BLACKBOARD ! " << endl ;
        cout << " " << endl ;
        cout << " (This program was created to calculate grades) " << endl ;
        cout << " " << endl ;

        // Here is where the user inputs the number of assignments  
        cout << " To start, enter number of assignments: " ;
        int numAssignments ;
        cin >> numAssignments ;

        cout << " " << endl ;
        cout << " Now, enter weight ( in percent ) of assignments: " << endl ; 
        cout << " " << endl ; 

        // Loop to input the weighted value per assignment
        float sum = 0.0 ;
        float num ;
        for ( int i = 0; i < numAssignments; i++ )
        {
            cout << " Assignment " << i + 1 << " : " ;

            cin >> num ;
            weight.push_back( num / 100 ) ;

            sum += num ;



        }
        cout << " Total Weight is: "<< sum << endl ;

        vector <float> grades (maxSize) ;

        // Input number of students

        cout << endl ;
        cout << " Please, enter number of students graded : " ;
        int numStud ;
        cin >> numStud ;

        // Loop for the calculation of average grade per student


        for ( int j = 0 ; j < numStud ; j++ )
        {
            cout << endl ;
            cout << " Enter grades for student " << j + 1 << ": " << endl ;
            float numGrade ;   
         for ( int k = 0 ; k < numAssignments ; k++ )
            {
                cout << " Grade for assignment " << k + 1 << ": " ;

                cin >> numGrade ;

                grades.at( numGrade ) ;
            }

            float totalWeight = 0.0 ;
            totalWeight += num * numGrade ; 

            char letterGrade ;      
            if ( totalWeight >= 90 )
            letterGrade = 'A' ;
            else if ( totalWeight >= 80 )
            letterGrade = 'B' ;
            else if ( totalWeight >= 70 )
            letterGrade = 'C' ;
            else if ( totalWeight >= 60 )
            letterGrade = 'D' ;
            else
            letterGrade = 'F' ;

            cout << " weight average is : " << totalWeight << endl ;    
            cout << " Letter grade is : " << letterGrade << endl ;
        }

        return 0;
    }