Hi, I was just trying for the heck of it to make 2 switch statements in the Main function in a small program written in C. The first switch statement executes, however the second doesn't, and it ends up outputting the number options again BUT THEN JUST TERMINATES, without me being able to choose again? This is in C
What could be wrong?
Thanks in advance!! - Jonas
CODE:
-------------------------------------------------------------------------------------------------
#include <stdio.h>
int main()
{
//=============================
//ask a question
//=============================
puts("Choose a number?\n");
puts("1 - 1 to 20");
puts("2 - 2 to 20");
puts("3 - 10 to 1");
//=============================
//lets user input a number
//=============================
char b;
scanf("%c",&b);
//=============================
//first switch statement
//=============================
int x;
switch(b)
{
case '1':
for(x=0;x<20;x++)
printf("%d\n",x+1);
break;
case '2':
for(x=1;x<20;x=x+2)
printf("%d\n",x+1);
break;
case '3':
for(x=10;x>0;x--)
printf("%d\n",x);
}
//=============================
//same code as above now with the return statement
//=============================
puts("Choose a number?\n");
puts("1 - 1 to 20");
puts("2 - 2 to 20");
puts("3 - 10 to 1");
//=============================
//lets user input a number
//=============================
char c;
scanf("%c",&c);
//=============================
//second switch statement
//=============================
switch(c)
{
case '1':
for(x=0;x<20;x++)
printf("%d\n",x+1);
break;
case '2':
for(x=1;x<20;x=x+2)
printf("%d\n",x+1);
break;
case '3':
for(x=10;x>0;x--)
printf("%d\n",x);
}
return(0);
}
---------------------------------------------------------------------------
to format it. It's a little confusing as it is, just because of the indents and everything.
Also, it seems like the problem is that the second input is just assuming the newline (hitting enter) from the previous input. To ignore that, add spaces before %c in the first scanf string, like so:
Yeah! I don't know if this is for a school assignment or something, but if not, you can take a look at this... I cleaned up the code a little and changed it to a while loop like @anon75264233 suggested.
#include <stdlib.h>
#include <stdio.h>
int main ()
{
int x;
char c;
while (c != '0')
{
puts("\nChoose a number?\n");
puts("0 - exit program");
puts("1 - 1 to 20");
puts("2 - 2 to 20");
printf("3 - 10 to 1\n: ");
scanf(" %c", &c);
puts("");
switch (c)
{
case '0':
break;
case '1':
for (x = 1; x <= 20; x++)
printf("%d\n", x);
break;
case '2':
for (x = 2; x <= 20; x += 2)
printf("%d\n", x);
break;
case '3':
for (x = 10; x > 0; x--)
printf("%d\n", x);
break;
default:
puts("Invalid number\n");
}
}
return 0;
}
Thx Jeol! Really cool, I'm kinda new to programming, (as u might suspect). It's a great learning experience for me to see how other's might do it :)
It wasn't really a school assignment (other than me trying to challenge myself and learn by it). I'll start at the University this fall however, but I want to know a bit of coding before that.
I'm taking a course on Lynda.com, "up and running with C" But you might be right about it. Are there any books you would recommend? Not only regarding the C language, but maybe programming in general or some computer litteracy books maybe?
@duchemeister your project was very similar to a project I had in my C class. While loops are awesome. Although with mine we had to use printf. It offers more flexible feedback.
Do you recommend "the C programming language" to a newcomer? It just seems as though that it's kinda difficult if you don't already know a language to start with? or is it simply a matter of sticking to your guns, and come out a changed man on the other side?
C is a bit of a pain in the ass, but its what most languages are based off of today. If you can learn to program in C, then you should not have difficulties picking up other languages.