Home > Net >  C Program can't input more than one variable before stopping
C Program can't input more than one variable before stopping

Time:02-04

While making a C program, I am not being able to get more than one user input at a time. After the program takes one input, it just stops and won't take any more input or continue the rest of the program. Image 1

The program works like intended when I dont use scanf and don't take input. Image 2

I have installed mingw and the c/c extensions, but it just doesn't work.

I tried debugging the the programs and also tried reinstalling both mingw and vscode multiple times but it doesn't work.

CodePudding user response:

You should be passing the address of the target variables to scan input into to scanf like scanf("%d", &l). Also remember to check the return value of the call to scanf. Quoting from cppreference.com, it returns the

Number of receiving arguments successfully assigned (which may be zero in case a matching failure occurred before the first receiving argument was assigned), or EOF if input failure occurs before the first receiving argument was assigned.

CodePudding user response:

The reason you cannot get more than one input is because an error occurred during your first call of scanf and the program does not make it to the second scanf call.

You need to add an & symbol before the variable name while calling scanf, like this:

scanf ("%d", &l);

The & is called the address operator, and you'll learn more about it on your path of learning C/C .

  • Related