-3

I've been coding for a few days now and decided to take on an exercise. One which calculates the total of five items and calculates the tax. In this particular exercise, I've managed to display the names of the items, and their prices , however, the sales tax and total aren't being displayed. I know this is a very basic problem, I've just been trying to get some hands on and learn, familiarize myself a bit more by trial and error. Can anyone pinpoint what I'm missing as far as the calculation part goes? Thanks advance.

#include <iostream>
using namespace std;

float tax = 0.07;
string item1,item2,item3,item4,item5;
float price1,price2,price3,price4,price5;

int main()
{
cout<<" please enter the name of the first item \n";
cin>> item1;

cout<<" please enter the name of second item \n";
cin>> item2;

cout<<" plrease enter the name of the third item \n";
cin>> item3;

cout<<" please enter the name of the fourth item \n";
cin>> item4;

cout<<" please enter the name of the fifth item \n";
cin>> item5;

cout<<" please enter the price for the first item \n";
cin>> price1;

cout<<" please enter the price for the second item \n";
cin>> price2;

cout<<" please enter the price for the third item \n";
cin>> price3;

cout<<" please enter the price for the fourth item \n";
cin>> price4;

cout<<" please enter the price for the fifth item \n";
cin>> price5;

float subtotal = 0;
float saletax = 0;
float grandtotal = 0;

subtotal = price1 + price2 + price3 + price4 + price5;
saletax = subtotal * tax;
grandtotal = subtotal + saletax;

cout<<"my shopping list \n";
cout<<"================\n";

cout<<item1<<"     "<<"$"<<price1 <<endl;
cout<<item2<<"     "<<"$"<<price2 <<endl;
cout<<item3<<"     "<<"$"<<price3 <<endl;
cout<<item4<<"     "<<"$"<<price4 <<endl;
cout<<item5<<"     "<<"$"<<price5 <<endl;
ReMaKe
  • 37
  • 1
  • 9
  • 1
    What exactly is the problem? Are you getting the wrong output? An error? – Mureinik Sep 14 '16 at 20:35
  • 3
    You did not copy all of your `main()` so its going to be impossible explain the problem with the missing part. – drescherjm Sep 14 '16 at 20:35
  • I'm getting no errors . Just that it's not giving me the total. So when I get the five items and their prices , the prices will appear next to the names but no total , nor sales tax. – ReMaKe Sep 14 '16 at 20:37
  • 2
    If your `main` is ending where it is in your paste, then the problem is simply that you did not print them with `cout` as you did with the item list. –  Sep 14 '16 at 20:37
  • On a side note, what if you had 100 items, or 500 items. Hopefully you wouldn't expect to write 100 or 500 `cin` and `cout` statements (what I'm saying is that there are far better ways of writing the code without repeating the same things over and over again). – PaulMcKenzie Sep 14 '16 at 20:55
  • @PaulMcKenzie Yeah I definitely need to learn different ways , I've literally just been coding for a few days, there's so much more I need to learn . I've just been watching a lot of videos , and finding random exercises to do , however just ordered the book programming , principles and practice using c++ so I'll read that and my computer science class starts soon as well, so I'll learn better ways, however , any suggestion is always welcomed. Thanks for the advice. – ReMaKe Sep 14 '16 at 21:11
  • ***I've just been watching a lot of videos*** That is where you went wrong! Get a c++ book from here: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list You will not learn c++ from online videos or tutorials. – drescherjm Sep 14 '16 at 21:13
  • Consider using a structure to group item name and price. Also consider using a container to hold more than one of these instances. That allows you to use a loop which saves typing. – Thomas Matthews Sep 14 '16 at 21:20
  • Now would be a good time to learn to use a *debugger*. The debugger allows you to execute each statement, one at a time, and *watch* values of variables. – Thomas Matthews Sep 14 '16 at 21:21
  • @drescherjm Thanks for the link , the book that was voted as the "best" introductory book , that's actually the one I just recently got . – ReMaKe Sep 14 '16 at 21:30
  • @ThomasMatthews I'll definitely look into your suggestions, and learn more about them. – ReMaKe Sep 14 '16 at 21:31

2 Answers2

2

I only see cout statements for the five prices, not for saletax or grandtotal, which would explain them not being displayed.

MikeThomas
  • 544
  • 3
  • 6
2

It is not being displayed, because you never print it.

Add these line at the end of your main function and you will see it.

cout << "Total: " << grandtotal << endl;
cout << "Tax: " << saletax << endl;
pr0gramist
  • 8,305
  • 2
  • 36
  • 48
  • Thanks for the help , though when I added that line , it displayed the "total" , and "tax" but didn't actually calculate them, however , looking at the way you wrote those codes, I just added `cout << "subtotal: " << price1 + price2 + price3 + price4 + price5;` But thank you so much – ReMaKe Sep 14 '16 at 20:57
  • @ReMaKe How would anyone but you know what you added in code that you did not post? We can't possibly help with `main()` when you don't post the whole thing especially when the problem is in the part that is not posted. – drescherjm Sep 14 '16 at 20:59
  • ***it displayed the "total" , and "tax" but didn't actually calculate them*** The part of your `main()` that you posted calculates these! – drescherjm Sep 14 '16 at 21:04
  • @drescherjm I did post the whole thing. For some reason it wasn't displaying the calculated results until I put that extra line of code . But it's fine , with all the replies I got I was able to figure it out . Big thanks – ReMaKe Sep 14 '16 at 21:05
  • Then you are missing a `}` at the end of the code you posted since `main()` will not compile without it. – drescherjm Sep 14 '16 at 21:05