C programming help? (Beware of dumb questions)

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);
}
---------------------------------------------------------------------------
1 Like

You should use
```c
{code}
```

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:

scanf(" %c", &c);
1 Like

You should put each switch inside a while loop.

1 Like

Great thx! :P will do in the future!

And by the way, THANK YOU! SO MUCH!
It really helped me a lot.. i guess that's one of C's small cryptic personality traits xD

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;
}
2 Likes

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.

Thank you for your help!
- Jonas

Thank you for your help! :)
Improved the code a LOT.
Was actually the solution to the problem I originally had, before this issue :)

You might want to invest in a good book. C is a complex language and it's filled with traps and pitfalls and it's easy to develop bad habits.

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?

For C look here: http://iso-9899.info/wiki/Books, there's a "free" category and a "Learning C" category, which contains payed books.

The classic book is

A more modern one is

Others are listed in the link at the beginning of my post. Don't look at the prices, for some reason amazon shows the highest.

For other stuffs, see my post here and the ones below. I might update the list at some point, or even add it to the TS wiki.

2 Likes

@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.

ex.

char in;
scanf_s("Enter a char: ", &in);
printf("\nYour input was: %c", in);

Ouch. My heart skipped a beat. Why do you use a compiler dependent function? In this case scanf_s is a function that belongs to the Windows compiler.

On a side note, how do you paste coloured code?

Check out what @Jeol wrote as an answer for main post.. :)

Totally overlooked it, thanks :D

you have to put 3 grave marks at the begging and ending of the code block.

grave mark = `

1 Like

@spidernet we were forced to use Visual Studio.

Ouch, my feels :(

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.