-4

I'm trying to complete an assignment but I'm having difficulty with the math expressions and variables in general. I'm trying to make a program that takes user info on groceries and then outputs a receipt. Here is my code.

#include <iostream>
#include <string>
using namespace std;


int main()
{
    //user input
    string firstItem, secondItem;
    float firstPrice, secondPrice;
    int firstCount, secondCount;

    double salesTax = 0.08675;
    double firstExt = firstPrice * firstCount;
    double secondExt = secondPrice * secondCount;
    double subTotal = firstExt + secondExt;
    double tax = subTotal * salesTax;
    double total = tax + subTotal;


    //user input
    cout << "What is the first item you are buying?" << endl;
    getline(cin, firstItem);
    cout << "What is the price of the " << firstItem << "?" << endl;
    cin >> firstPrice;
    cout << "How many " << firstItem << "s?" <<endl;
    cin >> firstCount;
    cin.ignore();
    cout << "What is the second item you are buying?" << endl;
    getline(cin, secondItem);
    cout << "what is the price of the " << secondItem << "?" << endl;
    cin >> secondPrice;
    cout << "How many " << secondItem << "s?" << endl;
    cin >> secondCount;


    // receipt output
    cout << "1st extended price: " << firstExt << endl;
    cout << "2nd extended price: " << secondExt << endl;
    cout << "subtotal: " << subTotal << endl;
    cout << "tax: " << tax << endl;
    cout << "total: " << total << endl;

    return 0;
}

The program output either 0 for all or negatives.

Danny
  • 13
  • 3
  • One of the most important principles in coding is: start with something small and simple that works perfectly, then build up. Look at all the time and effort you put into writing this code before you noticed that none of it was working. – Beta Nov 10 '17 at 02:45
  • My blunt advice here is read a good book. Do not try to learn by trial and error. – Ajay Brahmakshatriya Nov 10 '17 at 03:00
  • 1
    Also don't tag `C` if your question is about `C++` – Ajay Brahmakshatriya Nov 10 '17 at 03:01
  • @Beta I did start with something simple. I started with the user input part and made sure that that worked before I started working on the output of the math expressions. – Danny Nov 10 '17 at 03:12
  • @ Ajay Brahmakshatriya My bad that was a miss click. – Danny Nov 10 '17 at 03:13
  • Your sequence of operation is - `Calculate -> User input -> Output` but it should be - `User input -> Calculate -> Output`. – H.S. Nov 10 '17 at 03:13
  • I'm sorry, I should have given that comment more thought before I posted it. The approach I'm suggesting is hard to sum up in this space, but once you knew you had I/O working, your first attempt at calculation could have been `int n; n=n+1; cin >> n; cout << n<< endl;`. The bug would have been much easier to see. – Beta Nov 12 '17 at 19:42

2 Answers2

2

Your calculations must go after you read in the values, not before. You're making your calculations based on uninitialized variables.

John Ellmore
  • 2,209
  • 1
  • 10
  • 22
  • Wow thanks. That makes sense. Is there anything else that would make my code better? – Danny Nov 10 '17 at 02:48
  • @Danny `using namespace std;` at global scope is considered a bad practice in general. Consider qualifying std's stuff with it's namespace name, eg `std::cin` `std::cout` `std::string`. Also, if you want your program to be better separated, consider using functions for your calculations. – Guillaume Racicot Nov 10 '17 at 02:52
  • For a homework assignment, I'd say it's decent (besides the `using namespace std` as mentioned). It's good practice to decompose your code into functions, but it's also good practice to know when that's over-engineering for the task at hand :). I'd say that this particular program is simple enough to live in the main function like you have it. – John Ellmore Nov 10 '17 at 02:54
  • @Guillaume Racicot Why is `using namespace std` considered a bad practice? Does it cause errors? – Danny Nov 10 '17 at 02:56
  • See https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – John Ellmore Nov 10 '17 at 02:56
2

A declaration and initialisation like

double firstExt = firstPrice * firstCount;

initialises firstExt to be the product of the current values AT THAT POINT of firstPrice and firstCount.

It doesn't set up some magic so that the value of firstExt is recalculated whenever the values of firstPrice or firstCount are changed.

In your case, firstPrice and firstCount are uninitialised variables when you do this. Accessing values of uninitialised variables of type int gives undefined behaviour.

What you need to do is something like

cout << "What is the price of the " << firstItem << "?" << endl;
cin >> firstPrice;
cout << "How many " << firstItem << "s?" <<endl;
cin >> firstCount;

firstExt = firstPrice*firstCount;   //  do the calculation here

If the value of firstExt is not needed until this point, you can declare it here instead;

double firstExt = firstPrice*firstCount;   //  do the calculation here

which means any earlier use of firstExt will give a compiler diagnostic.

Peter
  • 35,646
  • 4
  • 32
  • 74