Can anyone points out what is wrong with my code? Determine the total price of the products purchase and possible change | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can anyone points out what is wrong with my code? Determine the total price of the products purchase and possible change

So I far, I’ve gotten a lot of issues with this program I need the possible output to be correct: || Enter product1 price: Enter product2 price: Enter the amount to pay: Total price: Change if there is: || What I have tried, yet #include <iostream> using namespace std; int main(){ int product1; int product2; int amount_to_pay; int total_price; int change; cout<<"Product1: "<<endl; cin>>product1; cout<<"Product2: "<<endl; cin>>product2; cout<<"Enter the amount to pay: "<<amount_to_pay; cin>>amount_to_pay; total_price=product1+product2; cout<<"Total price: "<<total_price<<endl; change=amount_to_pay-total_price; cout<<"Your change: "<<change<<endl; return 0; } Which brought alot of errors like the integer point when calling the output text “amount_to_pay”

5th Apr 2022, 4:29 PM
Sureta Darren
Sureta Darren - avatar
12 Answers
+ 1
Something like this ... // prompt and read value for <product1> and <product2> total_price = product1 + product2; cout << "Total price: " << total_price << endl; do { cout << "Enter the amount to pay: " << endl; cin >> amount_to_pay; if( amount_to_pay < total_price ) { cout << "Not enough money." << endl; } } while( amount_to_pay < total_price ); change = amount_to_pay - total_price; if( change > 0 ) { cout << "Your change: " << change << endl; }
5th Apr 2022, 9:12 PM
Ipang
+ 2
cout << "Enter the amount to pay:" << amount_to_pay; Here you are printing <amount_to_pay> when in fact you haven't read its value. Remove <amount_to_pay> and replace it with `endl` the way you wrote other input prompt.
5th Apr 2022, 4:48 PM
Ipang
+ 1
But you don't have to print it when prompting user. Look at other input prompt, none showed their respective variable ...
5th Apr 2022, 5:02 PM
Ipang
0
That’s the main thing, I want to print the amount to pay to prompt the user their amount of payment values
5th Apr 2022, 4:54 PM
Sureta Darren
Sureta Darren - avatar
0
Ok, your suggestion work but how can I make the program only output and input positive values everywhere? Since negative “change” is not a real thing in the real world if I input product1=20 and product2=30 then I get a total of price=50 but now I get a negative change of -20
5th Apr 2022, 5:03 PM
Sureta Darren
Sureta Darren - avatar
0
And the “amount to pay value” should always be greater than prod1 and prod2 otherwise it wouldn’t make any sense
5th Apr 2022, 5:06 PM
Sureta Darren
Sureta Darren - avatar
0
I’m thinking of a different track instead, should I use switch case or loop?
5th Apr 2022, 5:15 PM
Sureta Darren
Sureta Darren - avatar
0
switch case is conditional (decision making) loop is repetition They are no comparison ...
5th Apr 2022, 5:16 PM
Ipang
0
Please can you show me how this work?
5th Apr 2022, 5:28 PM
Sureta Darren
Sureta Darren - avatar
0
Show you what?
5th Apr 2022, 6:12 PM
Ipang
0
How can I use the conditional and the loop for this to work
5th Apr 2022, 6:44 PM
Sureta Darren
Sureta Darren - avatar
- 1
That's where conditionals come handy. You check whether <amount_to_pay> was greater or equal to <total_price>. If the check fails, you know the money wasn't even enough, and no change was in order ... You can even use a loop to continue prompting and reading <amount_to_pay> while the given value wasn't enough to complete the transaction.
5th Apr 2022, 5:11 PM
Ipang