i dont understand why this doesn't work | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

i dont understand why this doesn't work

#include <iostream> using namespace std; int main() { // your account's balance auto balance = 2452.4; // price for each notebook auto price = 259.99; auto notebooks = balance / price; auto rimasto = balance - (macbooks * price); cout << notebooks; cout << rimasto; // Task: calculate the number of notebooks you can afford and output it. // Hint: use an integer to store the result. // 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. }

1st Feb 2023, 8:16 PM
comodinooarmadioo
11 Answers
+ 7
… have you changed the line suggested? #include <iostream> using namespace std; int main() { auto balance = 2452.4; auto price = 259.99; auto notebooks = balance / price; auto rimasto = balance - (notebooks * price); cout << notebooks; cout << rimasto; } This is your code (comments removed). Where you had macbooks, changed to notebooks works fine for me. Outputs 9.432670
1st Feb 2023, 8:30 PM
DavX
DavX - avatar
+ 4
I had the same answer with different variables as DavX but it still failed on me. I had to do it the exact way the "See Solution" had it or it didn't work. What I was entering was: int main() { auto balance = 2452.4; auto price = 259.99 auto afford = balance /price; auto left = balance - (afford *price); cout << afford << endl; cout << left; } What gave me the green light for good code was: int main() { auto balance = 2452.4; auto price = 259.99; int total = balance / price; cout << total << endl; cout << balance - (total *price); }
25th Jun 2023, 7:43 PM
justin cross
+ 3
You haven’t declared or initialized the variable ‘macbooks’… You cannot multiply an unknown 👍
1st Feb 2023, 8:20 PM
DavX
DavX - avatar
+ 2
thanks
1st Feb 2023, 8:32 PM
comodinooarmadioo
+ 2
add endline in notebooks output #include <iostream> using namespace std; int main() { // your account's balance auto balance = 2452.4; // price for each notebook auto price = 259.99; int notebooks = balance/price; auto price2= balance-(notebooks*price); cout << notebooks<<endl; cout << price2; }
19th Sep 2023, 1:31 AM
Syahidah Humairoh
Syahidah Humairoh - avatar
+ 1
it still doesn't work, i checked the comments and it seems that im not the only one with this problem
1st Feb 2023, 8:26 PM
comodinooarmadioo
+ 1
Try to end this code with return 0;
2nd Feb 2023, 2:07 AM
Mayada Abdelmonem
Mayada Abdelmonem - avatar
+ 1
The task says you should output the last one on a new line... use <<endl;
7th Jun 2023, 10:17 AM
Ethan Kisang
Ethan Kisang - avatar
0
thnx justin cross. i've tried is some many times but they want you to do the math in the print.thnx!
5th Sep 2023, 8:46 AM
Thirza Swijnenburg
Thirza Swijnenburg - avatar
0
#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. cout << "You can afford " << int(balance / price) << " notebooks!" << 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. cout << "The Amount you have left over in your account is
quot; << balance - ((int(balance/price)) * price) << " Dollars" << endl; return 0; } //Why does this not work I have ran it through a compiler and everything works well! I tried assigning variables prior but nothing like that worked
5th Sep 2023, 11:49 PM
Michael Thompson
Michael Thompson - avatar
0
// this is right answer #include <iostream> using namespace std; int main() { auto balance = 2452.4; auto price = 259.99; int total = balance / price; cout << total << endl; cout << balance - (total *price); } // this is wrong answer 💀🙏🏼 #include <iostream> using namespace std; int main() { // your account's balance auto balance = 2452.4; // price for each notebook auto price = 259.99; // affordable number of notebooks int notebooks = balance / price; // purchased amount int purchased = price * notebooks; // amount left in my balance after purchase int amount = balance - purchased; // Amount output cout << notebooks << endl; cout << amount; // Task: calculate the number of notebooks you can afford and output it. // 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. // Hint: use an integer to store the result. } // no hate to this app cuz its free 💀🙏🏼 and teaching me
13th Jan 2024, 1:18 PM
Razor Xhibit
Razor Xhibit - avatar