• C

How to re-prompt a user to input an accepted input in c

I have the following code:

#include<stdio.h>
char checkInput0(void);

int main() {
  char output;
  output = checkInput0();
  printf("The output is %c", output);
}

char checkInput0(void){
  char option0 = '\0',check0;
  char c;
    
  do{
    printf("Please enter the your choice of A or B: ");
    scanf("%c %c",c ,option0);  
    if(c || option0 != 'A' && 'B' ){
      while((check0 = getchar()) != 0 && check0 != '\n' && check0 != EOF);
      printf("[ERR] Please enter A or B.\n");
    }else{
      break;
    }
  }while(1);
  return option0;
}

Open in new window


and it prompts the user to input something but regardless of the input, I get a segmentation fault.
What I would like the program to do is to Prompt the user to Please enter  your choice of A or B: and if the User enters an A or B it would output: The output is <Their choice>. anything else is entered it should print the following message: [ERR] Please enter A or B.\n.
I can't seem to get it to work.
All help Will be greatly appreciated.
Thanks in advance
Gehteuch NixanAsked:
Who is Participating?
 
David Johnson, CD, MVPConnect With a Mentor OwnerCommented:
// ConsoleApplication3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<stdio.h>


char checkInput0(void) {
	int character;
	char option0 = '\0';
	do {
		printf("Please enter the your choice of A or B: ");
		character = getchar();
		option0 = (char)character;

		if ((option0 != 'A') && (option0 != 'B'))
		{
			printf("[ERR] Please enter A or B.\n");
		}
		else {
			break;
		}
	} while (1);
	return option0;
}


int main() {
	char output;
	output = checkInput0();
	printf("The output is %c", output);
	
}

Open in new window

0
 
ZoppoCommented:
Hi Gehteuch Nixan,

you need to pass pointers to sscanf instead of variables, i.e:
scanf("%c %c",&c ,&option0);

Open in new window

Hope that helps,

ZOPPO
0
 
Gehteuch NixanAuthor Commented:
Thank you, I no longer get a segmentation fault, but it re-prompts regardless and never prints out  "The output is A/B"
0
Improved Protection from Phishing Attacks

WatchGuard DNSWatch reduces malware infections by detecting and blocking malicious DNS requests, improving your ability to protect employees from phishing attacks. Learn more about our newest service included in Total Security Suite today!

 
David Johnson, CD, MVPOwnerCommented:
if ((c != "a") && (c #= "b"))
0
 
sarabandeConnect With a Mentor Commented:
you better use fgets for input. if using scanf and the user did a wrong input you have spoiled stdin and it needs a reset and clear of the fail state before you can get a new input.

char checkInput0(void){
  char option0 = '\0',check0;
  char c;
  char input[128] = { '\0' };
    
  do{
    printf("Please enter the your choice of A or B: ");
    if (!fgets(input, sizeof(input)-1, stdin))
         break;   // error or eof by ctrl+z
    
    if(input[0] != 'A' && input[0] != 'B' ){
            printf("[ERR] Please enter A or B.\n");
    }else{
      break;
    }
  }while(1);
  return option0;
}

Open in new window


note, input contains a newline character  because the user input needs to be terminated by <enter>.

you also could use gets instead of fgets, but gets has no maximum size argument and doesn't add a '\n' as done by fgets what can be a big advantage if reading longer text from stdin.

Sara
0
 
sarabandeCommented:
Using fgets or getchar should solve the issue.

Sara
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.