In C++ the order you do things matters. This is OK
int Pfries = 80;
cout<< "Enter the number of fries ordered:";
cin>>Nfries;
int fries = Pfries * Nfries;
because at the point where you calculate fries
both Pfries
and Nfries
have values.
But this (which is what you do) is not OK
int Pfries = 80;
int fries = Pfries * Nfries;
cout<< "Enter the number of fries ordered:";
cin>>Nfries;
In this calculation you use the Nfries
variable before it has been given a value. This is technically known as undefined behaviour, which is another way of saying that the effects of this code are unpredictable.
What you seem to be thinking is that you've written a formula fries = Pfries * Nfries
and C++ will recalculate that formula if any of the variables get different values. But that's not how C++ (or most programming languages) work. Instead your program is a sequence of statements which get executed in a given order, and the values used are the values that the variables have at the time when the given statement is executed.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…