• 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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
David Johnson, CD, MVPOwnerCommented:
if ((c != "a") && (c #= "b"))
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

sarabandeCommented:
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
David Johnson, CD, MVPOwnerCommented:
// 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sarabandeCommented:
Using fgets or getchar should solve the issue.

Sara
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.