Hello and greetings to everyone. (I'm new here)
Anyways.........
I have a problem with my coding in C.
What I'm trying to do is to let a person pick a number between 1 to 3. (the switch function checks your answer and tells you what you have picked)
Then, if the answer is not 1, 2, or 3, the program loops and asks again.
Here is my code
Code:
#include <stdio.h>
int main()
{
int a, b, c;
printf("Pick between 1, 2 or 3\n");
printf("INPUT A NUMBER:");
scanf("%d", &a);
while((a!=1)||(a!=2)||(a!=3))
{
switch (a)
{
case 1:
printf("You picked 1");
break;
case 2:
printf("You picked 2");
break;
case 3:
printf("You picked 3");
break;
default:
printf("Your answer is not valid");
break;
}
}
getchar();
}
Here's what happens when the program is run.
If you input 1, the line "You picked 1" appears infinitely. (this also happens with 2, 3 with their respective answers)
Finally, if i type in any number (which will lead to default) it shows the line "Your answer is not valid"
Can anyone point out to me what went wrong with my code?
I also tried to do the same thing without using the "switch" function
Code:
#include <stdio.h>
int main()
{
int a, b, c;
printf("Pick a number from 1, 2 or 3\n");
printf("Input a number:");
scanf("%d", &a);
while(a != 1) /* ,a != 2, a != 3*/
{
printf("INPUT AGAIN:");
scanf("%d", &a);
}
if(a = 1)
{
printf("You inputted 1");
}
else if(a = 2)
{
printf("You inputted 2");
}
else if(a = 3)
{
printf("You inputted 3");
}
}
but whenever i put the things in the comment box (beside the while function)
the program doesnt work properly.
I would really appreciate it if someone could point out my mistakes because i wanna get creative and improve my programming. Thanks.