Help in this program.

Joined
May 14, 2022
Messages
2
Reaction score
0
#include <stdio.h>
#include <conio.h>
int main()
{
int a;
char ch = 'c';
while (ch == 'c')
{
printf ("Enter a digit 0 - 9:\n");
scanf("%d", &a);
switch (a)
{
case 0 : printf("Zero\n");
break;
case 1 : printf("One\n");
break;
case 2 : printf("Two\n");
break;
case 3 : printf ("Three\n");
break;
case 4 : printf("Four\n");
break;
case 5 : printf("Five\n");
break;
case 6 : printf("Six\n");
break;
case 7 : printf("Seven\n");
break;
case 8 : printf("Eight\n");
break;
case 9 : printf("Nine\n");
break;
default : printf("Illegal character\n");
}
printf ("Enter 'c' if you want to continue\n");
printf("Or any other character to end\n");
scanf("%c", &ch);
if (ch != 'c');
printf("End of session\n");
}
}

The program was ending was coming out of the loop after the first output, It was not taking input for the continue or end of session. Please help to find the mistake.
 
Joined
Mar 28, 2022
Messages
82
Reaction score
11
C:
    } // switch
    printf("Enter 'c' if you want to continue\n");
    printf("Or any other character to end\n");
    scanf("%c", &ch);
    if (ch != 'c'); // this semi-colen ends the 'if' statement
    printf("End of session\n"); // outside 'if' statement, executed every time
  } // while
 

xcr

Joined
May 21, 2022
Messages
1
Reaction score
0
When reading the input through scanf you are able to read the first character but you missed the null operator character which gets immediately picked up by the second scanf function which then triggers the loop ending condition. In order to fix this problem, you have to read the null operator to a variable to save it from being accidentally picked up.

C:
#include <stdio.h>
#include <conio.h>

int main() {
  int a;
  char null, ch = 'c'; // <---------- New null variable
  while (ch == 'c') {
    printf("Enter a digit 0 - 9:\n");
    scanf("%d%c", & a, & null); // <------- Changed scanf function
    switch (a) {
    case 0:
      printf("Zero\n");
      break;
    case 1:
      printf("One\n");
      break;
    case 2:
      printf("Two\n");
      break;
    case 3:
      printf("Three\n");
      break;
    case 4:
      printf("Four\n");
      break;
    case 5:
      printf("Five\n");
      break;
    case 6:
      printf("Six\n");
      break;
    case 7:
      printf("Seven\n");
      break;
    case 8:
      printf("Eight\n");
      break;
    case 9:
      printf("Nine\n");
      break;
    default:
      printf("Illegal character\n");
    }
    printf("Enter 'c' if you want to continue\n
            Or any other character to end\n");
    scanf("%c%c", & ch, & null); /// <------- Changed scanf function
    if (ch != 'c') { /// <----------------- Changed if statement
        printf("End of session\n");
        break;
    }
  }
  return 0;
}

As the previous post showed, the if statement should be changed to properly end the loop. This null terminator problem is also found in C++. Hope this helps.
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,061
Latest member
KetonaraKeto

Latest Threads

Top