Adding Decimal problem C++

With my code at the moment it is suppose to repeat Assignment # each 10 times and Project # each twice, then to take all 12 answers and add them up in one giant point total. but the problem is that when I enter a decimal as a answer, my code then begins to scramble and exits out of the executable.

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

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';
}
Last edited on
> that when I enter a decimal as a answer, my code then begins to scramble

Make the type of the variable double. For example:

1
2
3
4
5
6
7
std::cout << "Please enter the score for 'Assignment #0" << i + 1 << "': ";

// int assignment_score;
double assignment_score ;

std::cin >> assignment_score; // we assume that the user enters a valid score
// ... 
Topic archived. No new replies allowed.