rounding problem C++

For my C++ class we are trying to create a grade projector but the part I am stuck on is the entering of the Assignment score part and project part, which are just adding funtions that then are suppose to display all 12 answers as a point total. (10 are Assignment and 2 are Project)when i add in the decimal to a assignment answer, the decimal then scrambles the code and stops the executable.

// Grade Projector.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include "pch.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

const unsigned char DBL_CROSS = 206; // +
const unsigned char DBL_UR = 187; // +
const unsigned char DBL_UL = 201; // +
const unsigned char DBL_LL = 200; // +
const unsigned char DBL_LR = 188; // +
const unsigned char DBL_TO_DBL_B_JOINT = 202; // -
const unsigned char DBL_TO_DBL_T_JOINT = 203; // -
const unsigned char DBL_TO_DBL_L_JOINT = 204; // ¦
const unsigned char DBL_TO_DBL_R_JOINT = 185; // ¦
const unsigned char DBL_TO_SIN_B_JOINT = 207; // -
const unsigned char DBL_TO_SIN_T_JOINT = 209; // -
const unsigned char DBL_TO_SIN_L_JOINT = 199; // ¦
const unsigned char DBL_HORIZONTAL = 205; // -
const unsigned char DBL_VERTICAL = 186; // ¦

int main() {

int a, b, c; // a for assignment, b for project, and c for final answer

for (int i = 0; i < 10; i++) {

cout << "Please enter the score for 'Assignment #0" << i + 01 << "': ";
cin >> a;
}

for (int i = 0; i < 2; i++) {

cout << "please enter the score for 'Project #0" << i + 01 << "': ";
cin >> b;
}

c = a + b;

cout << "\n" << "TOTAL POINTS SO FAR: " << c << "\n";

std::cout << DBL_HORIZONTAL;
std::cout.width(20);

}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

int main()
{
    const int NUM_ASSIGNMENTS = 10 ;
    const int NUM_PROJECTS = 2 ;

    double total_score = 0 ;

    for( int i = 0 ; i < NUM_ASSIGNMENTS ; ++i )
    {
        std::cout << "Please enter the score for 'Assignment #0" << i+1 << "': " ;
        int assignment_score ;
        std::cin >> assignment_score ; // we assume that the user enters a valid score

        total_score += assignment_score ;
    }

    for( int i = 0 ; i < NUM_PROJECTS ; ++i )
    {
        std::cout << "Please enter the score for 'Project #0" << i+1 << "': " ;
        int project_score ;
        std::cin >> project_score ; // we assume that the user enters a valid score

        total_score += project_score ;
    }

    std::cout << "total score: " << total_score << '\n' ;
}
There is nothing wrong with your loops. The only issue here is that everytime you do cin>> a or cin>>b you overwrite the last input. The way you are doing it will have sum of last a and last b in c.
You need to keep adding the input everytime like JLBorges is doing
total_score += assignment_score
.

Just initialize c=0 than do c=c+ a or c= c+b after every input and remove c= a+b. That will solve the issue
Topic archived. No new replies allowed.