Using non-integer values with cin?

I am brand new to C++, and trying to create a simple program to calculate GPA as is done at my high school, for the sake of practice. I can't figure out how to make it yield a non-integer answer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "stdafx.h"
#include <iostream>
#include <iomanip>


void main()
{
	std::cout << "Enter the number of AP classes taken in high school.";
	int x;
	std::cin >> x;
	std::cout << "Enter the number of zero hour classes taken in high school.";
	int y;
	std::cin >> y;
	std::cout << "Enter the number of free hours taken in high school, or other classes entirely exempt from GPA calculation.";
	int z;
	std::cin >> z;
	int a = 24 + y - z;
	float b = float (x / a);
	float g = float (4 + b);
	std::cout << "Your cumulative weighted GPA will be:" << g << std::endl;
}
/*void*/ int main() { /* ... */ } - main() must return int.
http://www.stroustrup.com/bs_faq2.html#void-main

float (x / a); performs integer division and then converts the integer result to float.

This is what you intended:
1
2
	float b = float(x) / a ;
	float g = 4 + b ;


In general, favour double over float; and use const proactively.
1
2
	const double b = double(x) / a ;
	const double g = 4 + b ;


You may want to verify that a is not equal to zero before attempting the division.
Thank you so much!
Topic archived. No new replies allowed.