The output is not working in C++. The code is good. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

The output is not working in C++. The code is good.

#include <iostream> using namespace std; int main() { // your account's balance auto balance = 2452.4; // price for each notebook auto price = 259.99; // Task: calculate the number of notebooks you can afford and output it. // Hint: use an integer to store the result. int numberofbooksafford= balance/price; cout<<"Number of notebook you can afford :"<<numberofbooksafford<<endl; // Task: calculate the amount left over on your account after the purchase and output it on a new line. // Hint: calculate the total price of the purchase, then substract it from the balance. double totalPurchasePrice= numberofbooksafford* price; double amountLeftOver=balance-totalPurchasePrice; cout<< "Amount left over after the purchase"<<amountLeftOver<<endl; } I tried that output is still not working.

10th May 2024, 2:26 PM
Div Anand
Div Anand - avatar
6 Answers
+ 3
Div Anand , Where is your attempt??
10th May 2024, 3:14 PM
Riya
Riya - avatar
+ 1
numberofbooksafford has type int while the result of the division result to double type. While you using type 'auto' for balance and price you can use auto for this as well
18th May 2024, 10:21 PM
john ds
john ds - avatar
0
The test is failing.
10th May 2024, 4:16 PM
Div Anand
Div Anand - avatar
0
most likely it's the extra closing brace at the end of the code
10th May 2024, 7:42 PM
Dawid Galuszka
Dawid Galuszka - avatar
0
Div Anand your code doesn't meet required criteria as you didn't followed instructions exactly: TASK 1: Calculate number of notebooks you can afford and output it. Expected output: (numberofbooksafford) Your output: Number of notebook you can afford: (numberofbooksafford) TASK2: Calculate the amount leftover... ... and output it on a new line. Expected output: (amountLeftOver) Your Output: Amount left over after the purchase (amountLeftOver)
11th May 2024, 9:55 AM
Dawid Galuszka
Dawid Galuszka - avatar
0
The issue lies in the fact that you're using an integer to store the result of the division, which will truncate the decimal part. In C++, when you divide an integer by another integer, the result is always an integer. Any fractional part is discarded. To fix this, you need to use floating-point numbers (like double or float) to store the result of the division. Here's the corrected code: #include using namespace std; int main() { // your account's balance auto balance = 2452.4; // price for each notebook auto price = 259.99; ```// Task: calculate the number of notebooks you can afford and output it. // Hint: use a floating-point number to store the result. double numberOfBooksAfford = balance / price; cout << "Number of notebooks you can afford :" << numberOfBooksAfford << endl; // Task: calculate the amount left over on your account after the purchase and output it on a new line. // Hint: calculate the total price of the purchase, then subtract it from the balance. double totalPurchasePrice = number
12th May 2024, 6:00 AM
Precious Chisom
Precious Chisom - avatar