0

This is the relevant code, I'm trying to write a function to pick type of account and when I run this, it keeps running in a weird loop. Any solutions?

  void AccType(){
wrongInput:;
        int typCho;
        printf("What type of account do you want to open?\nPress 1 for Current\n2 for savings\n3 for retirement:\n");
        scanf("%d",typCho);
        switch (typCho) {
        case 1:
                strcpy(PracRec.AccTyp,"Current");
                break;
        case 2:
                strcpy(PracRec.AccTyp,"Savings");
                break;
        case 3:
                strcpy(PracRec.AccTyp,"Retirement");
                break;
        default:
                printf("Please enter a valid choice!!\n");
                goto wrongInput;
        }
}
Bas_Anar
  • 7
  • 4
  • 2
    Turn on compiler warnings. – Cheatah Jun 10 '21 at 20:54
  • 5
    You invoked *undefined behavior* here `scanf("%d",typCho);` by passing `int` where `int*` required and using an (indeterminate) value of uninitialied non-static local variable. Usually it will lead to Segmentation Fault. You are (un)lucky! – MikeCAT Jun 10 '21 at 20:55
  • ah thanks I'm still new to this whole programming stuff so I keep making these newbie mistakes – Bas_Anar Jun 10 '21 at 20:59
  • 1
    You shouldn't try to create loops with `goto` – instead use real loop semantics (`for`, `while`, `do - while`). As you'd have a nested loop and switch, you might use `goto` to exit the loop, though. The difference? Well, an explicit loop shows immediately what's going on, leaving nested loops with `goto` is commen/accepted practice. A loop instantiated with `goto` is much less clearly visible. – Aconcagua Jun 10 '21 at 21:20
  • Regarding when to use `goto`: 1. [Is it ever advantageous to use 'goto' in a language that supports loops and functions? If so, why?](https://stackoverflow.com/q/24451/12149471) 2. [GOTO still considered harmful?](https://stackoverflow.com/q/46586/12149471) – Andreas Wenzel Jun 10 '21 at 21:28
  • @Bas_Anar: Compiler warnings can help avoid these "newbie mistakes". For instance `gcc -Wall` gives you a warning about your `scanf` bug: https://godbolt.org/z/MEc8effz7. Figure out how to turn on warnings, always always use them, and never never ignore them! If you can't figure out what a warning means, ask, but don't even try to run your code until you've got it resolved. – Nate Eldredge Jun 10 '21 at 21:39
  • Thank you everyone, this is some great advice! I'll be sure to avoid using goto statements from now on. – Bas_Anar Jun 11 '21 at 01:07

1 Answers1

0

The second argument of scanf needs to be a pointer (memory adress) to an integer...

So, replace the line:

scanf("%d",typCho);

with:

scanf("%d", &typCho);

& is an operator that returns the memory address of the operand at right.

This is needed because scanf is going to change the value inside the memory address which relates to the variable.

When you put only typCho as the argument, it probably made the program to write the input at a random memory location, as the variable was also uninitialized.

Also, consider avoiding the use of goto, as pointed in the previous comments.

You could easily do the same thing with a construct like:

do { ...code... } while (condition);