954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

run error

ok i was just fiddiling around macking this program to see what would happen and it worked fine but for some reason now it doesnt do what the program tells it to do
this is my code

"#include <iostream>
using namespace std;
int main()
{
int taco,pizza,chicken,hamburger,hotdog,;
taco=0;
cout<<"from 1 to 3 enter what taco is then press enter";
cin>>taco;
if(taco=1){pizza=2;}
if(taco=2){pizza=3;}
if(taco=3){pizza=4;}
if(pizza=2){chicken=3;}
if(pizza=3){chicken=4;}
if(pizza=4){chicken=5;}
if(chicken=3){hamburger=4;}
if(chicken=4){hamburger=5;}
if(chicken=5){hamburger=6;}
if(hamburger=4){hotdog=5;}
if(hamburger=5){hotdog=6;}
if(hamburger=6){hotdog=7;}
cout<<"taco="<<taco<<"pizza="<<pizza<<"chicken="<<chicken<<"hamburger="<<hamburger<<"hotdog="<<hotdog;;
system ("pause");
}";

for some reason no mater what number i use (when runing it) it always puts the answer
taco=3pizza=4chicken=5hamburger=6hotdog=7press any key to continue . . .

grunge man
Junior Poster
170 posts since Mar 2006
Reputation Points: 10
Solved Threads: 2
 

>>and it worked fine but for some reason now it doesnt do what the program tells it to do'

Yes, it does do exactly what you told it to do. But that may or may not be what you want it to do. :mrgreen:

major problem is that you are using the = operator instead of == operator in the if statements

if( taco == 1 )
   /blabla
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

ok i was just fiddiling around macking this program to see what would happen and it worked fine but for some reason now it doesnt do what the program tells it to do this is my code

int main()
{
int taco,pizza,chicken,hamburger,hotdog,;
cout<<"from 1 to 3 enter what taco is then press enter";
cin>>taco;

<em><strong>if(taco=1)</strong></em>{pizza=2;}
<em><strong>if(taco=2)</strong></em>{pizza=3;}
<strong> <em>if(taco=3)</em></strong>{pizza=4;} // 3rd stmt

cout<<"taco="<<taco<<"pizza="<<pizza<<"chicken="<<chicken<<"hamburger="<<hamburger<<"hotdog="<<hotdog;;
system ("pause");
}";

The lines which i have underlined in your code are the statements which are causing problem to you. What you wanted to do was some condition checking but ended up assigning new values to the variable "taco" and overwriting the ones inputted by the user.
Hence your third stmt if (taco = 3) .... ends up assigning the value of 3 to taco and not checking whether the taco variable is 3 or not. That stmt alogn with the stmst i have highlighted should use the == operator instead of using the assignment operator which is = .

SO just put if (taco == 3) and like wise in your code and it should work fine.

Also using the system ("pause") to make the prog wait is considered bad programming practice since it invokes the OS specific routine "pause". Use getchar () instead, it achieves the same purpose.

Hope it helped, bye.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

>>and it worked fine but for some reason now it doesnt do what the program tells it to do'

Yes, it does do exactly what you told it to do. But that may or may not be what you want it to do. :mrgreen:

actually let me rephrase. when i first creaded the program it did what i wanted it to do but now the last time i ran it, with the same code, no matter what i input it always comes out the same. the only thing i can think of is that there might be something wrong with the compiler.

also
if there's a reason why this is happeningfor some reason no mater what number i use (when runing it) it always puts the answer
taco=3pizza=4chicken=5hamburger=6hotdog=7press any key to continue . . .
from my code then pleas look over my code and tell me what the problem might be

grunge man
Junior Poster
170 posts since Mar 2006
Reputation Points: 10
Solved Threads: 2
 
the only thing i can think of is that there might be something wrong with the compiler. from my code then pleas look over my code and tell me what the problem might be


Blaming the compiler is no way of solving the problem and the solution to your problem i have already stated in my previous post. Look there.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

nevermind the dude above answerd my qustion

grunge man
Junior Poster
170 posts since Mar 2006
Reputation Points: 10
Solved Threads: 2
 

~s.o.s~ would it be more correct if i made all of my = into ==

grunge man
Junior Poster
170 posts since Mar 2006
Reputation Points: 10
Solved Threads: 2
 

> int taco,pizza,chicken,hamburger,hotdog,;
Does this even compile?
That trailing , looks like a syntax error to me.

> the only thing i can think of is that there might be something wrong with the compiler.
Er, no - it's all your code.
The chance of a newbie stumbling on a compiler bug is close enough to zero as to make no difference.

If you've got past "hello world", then you can be pretty sure the compiler is installed and working correctly.

You might want to add these options to your compiler (since you seem to be using dev-c++)
-W -Wall -ansi -pedantic -O2 -Werror
Those will trap a lot of programming mistakes before you get to run the code and observe "weird" behaviour.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 
~s.o.s~ would it be more correct if i made all of my = into ==

Simply put it this way, when u want to compare some values you use the comparision operator which is == .
When you want to assign a value to a variable you need to use the assignment operator which is = .
Simple as abc.

So wat u want to do in ur current case ? U want to compare the values inputted by the user isnt it ? SO use the comparision operator. That will set you right and make your program work.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You