correct my program plz

Hello, i'm trying to write a program that will help a robot driving a car to execute 3 actions:stop,decelerate or go depending to the color: red, orange or green. itried it with "if" and "switch" but the program always print stop with every color. Here's the two programs;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  #include<stdio.h>
char feu;
int main()
{
  scanf("%c",&feu);
  if(feu='red')
  {
    printf("stop");
  }
  else
  {
    if(feu='orange')
    printf("decelerate");
    else
    printf("go");
  }
}
//the second program:
#include<stdio.h>
char feu;
int main()
{
     printf("give a color");
    scanf("%c",&feu);
 switch(feu)
 {
 case 'red':printf("stop");
 break;
 case 'orange':printf("decelerate");
 break;
 case 'green':printf("go");
 break;
 }

}
been a while since i've done C, but %c will be reading in one character and then you go on to test to see if that character is the string "red".

edit: if you compile this you should find this out :)

edit2: uses a std::string to hold your colour and read it in using this:
http://www.cplusplus.com/doc/tutorial/basic_io/

Last edited on
if u want to use pure C u need to store the input in a c-string and compare it
using strcmp() from <string.h>

1
2
3
4
5
6
7
8
9
10
11
12
char szColor[ 32 ];

scanf( "%s", szColor );

if( strcmp( szColor, "red" ) == 0 ) // strcmp() returns 0 if both string are equal
    // ...
else if( strcmp( szColor, "orange" ) == 0 )
    // ...
else if( strcmp( szColor, "green" ) == 0 )
    // ...
else
    // ... 


you cannot use switch w/ case
Last edited on
i'm actually using code blocks
i need help with my project please anybody..
i'm actually using code blocks

Are u using C or C++ ? if C++, stop using stdio and use IOstreams
Last edited on
Topic archived. No new replies allowed.