I'm missing something in this program.



Hi everybody,

This is my first time taking a C++ course and I need some help. I'm missing something in this program. When I run the program and introduce the Amount Sold, it doesn't show the number that I had introduce, it just show the formula. Can you please help me?

Problem said: If the amount-sold is greater than $200 subtract discount from amount-sold to determine bill, otherwise bill is equal to amount sold.

#include <iostream.h>
#include <iomanip.h>

int main()
{
int Asold;
cout<<"Enter amount sold:";
cin>>Asold;
if (Asold > 200)
cout<<"Your Bill = Asold - Discount\n";
else
cout<<"Bill=Asold\n";
return 0;
}
If you leave the formula in quotations, the compiler treats it as a string. It doesn't equate it. Take the part you want to equate our of the quotes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream> // drop the .h.  Not needed for any compiler made in the last decade
//#include <iomanip> not needed for this program

using namespace std; // Needed, otherwise you have to prepend std:: to every instance of cout and cin

int main()
{
  int Asold;
  cout<<"Enter amount sold:";
  cin>>Asold;

  if (Asold > 200)
    cout<<"Your Bill = " << Asold - Discount << "\n"; // Discount is not defined.  This will give an error.
  else
    cout<<"Bill=" << Asold << "\n";
  return 0;
}
Last edited on
In C++, anything between two " symbols is a string.

So, here's a string: "cats on a mat"

Is this a string - "234"? Yes, because it is between two " symbols. It's a string in which the symbols being shown are numbers, but it's still a string.

So is this a string - "34 - 17"? Yes, because it is between two " symbols. It's a string in which the symbols being shown look like a mathematical operation, but it's still a string.

So how about this - "Asold - Discount" Yes, because it is between two " symbols. It's a string in which the symbols being shown look like a mathematical operation, but it's still a string.

So now do you see the problem?

I expect you wanted this: cout<<"Your Bill = " << Asold - Discount;

Only "Your Bill = " should be a string. This bit Asold - Discount is a mathematical operation that will give you a number. Don't forget that you need to actually create the variable Discount. So far, you haven't done that.
Last edited on
Moschops & Stewbond ,Thanks so much!
Topic archived. No new replies allowed.