+ 3

i cant get the sum of this C++

#include<iostream> #include<stdio.h> using namespace std; int main() { int student ; int workbook = 3; int pencil = 1.5; int eraser = 0.5; double sum ; cout << "enter the number of students: "; cin >> student ; workbook = student * 5 ; cout << "total workbooks: " <<workbook<<""<<endl; pencil = student * 4 ; cout << "total pencils: " <<pencil<<""<<endl; eraser = student * 3 ; cout << "total erasers: " <<eraser<<""<<endl; sum = workbook + pencil + eraser ; cout << "the total cost will be: " <<sum<<""<<endl; return 0 ; } so in the following code as you can see in the int = workbooks = 3 ; < the 3 is supposed to be the euros , as in the pencils = 1.5 and erasers 0.5... in the sum it wont calculate the values and justs prints out the sum of all the equipment and not the price of each one as well ... any thoughts on this ? thank you, Spireta

8th Jun 2018, 10:58 PM
Dichonia
Dichonia - avatar
4 Respostas
+ 2
EDIT : i want to get sum = all the equipment + the value of each one in euros calculated
8th Jun 2018, 10:59 PM
Dichonia
Dichonia - avatar
+ 2
it worked just fine guys , thank you all for your help. i should really keep an eye out for these little mistakes ! Cheers, Spireta.
9th Jun 2018, 9:07 PM
Dichonia
Dichonia - avatar
+ 1
First, you can't have a floating point literal number declared as an int: int pencil = 1.5; //needs to be float or double You don't need the empty string literal here: cout << "total workbooks: " << workbook << "" << endl; //use ... << workbook << endl; Whenever you perform an operation on different types, like int and double ( pencil = student * 4), you have to cast into the type you want. Like this: pencil = (double)student * 4; ... sum = (double)workbook + pencil + eraser; Try these things first and then let me know how it goes, Filios Christou
9th Jun 2018, 2:31 AM
Zeke Williams
Zeke Williams - avatar
0
change every variable to double or sum = (double) workbook + pencil + eraser
9th Jun 2018, 7:06 AM
Aveek Bhattacharyya
Aveek Bhattacharyya - avatar