Hi,
I need your help for my program.

My code is :

void user()
{

	char str[15];
	int i=0;
	
	do{
		printf("\nPrint str.\n");
		fgets(str,15,stdin);
		i = strlen(str);
	}while(i>15);
}

void menu()
{
	printf("Press :\n"); 
	printf("0. \n");
	printf("1. \n");
	printf("2. \n");
}

int get_choice()
{
 int choice = 0 ;
 const char* const valid_choices = "012" ;

 do
 {
   fputs( "Your choice : ", stdout ) ;
   fflush( stdin ) ;
   choice = toupper( fgetc( stdin ) ) ;
   fgetc( stdin ) ;

   if( strchr(valid_choices, choice) == NULL )
   {
     fputs("Invalid option.\n");
     choice = 0 ;
   }
 } while( choice == 0 ) ;

 return choice ;
}



main()
{
	int choice=0;

	do
   	{
             menu() ;
      	     choice = get_choice() ;

      		switch(choice)
     		 {
          		case 0: user();
			   		 break ;
          		case 1: break ;
          		case 2: break;
		}
     		
	 }while(choice != 2);
  
}

When I press string with <15 number of characters all is ok.
But if my str is 17 characters , then is printed two times
'Your choice : Invalid Option'

Could anyone help me fix it ?
Thanks

Recommended Answers

All 8 Replies

What is this program supposed to do? If you could give us an example...say

When I run this program and I'm prompted for this and I press this - this happens, when in fact this should happen...I hope this makes sense

What is this program supposed to do? If you could give us an example...say

When I run this program and I'm prompted for this and I press this - this happens, when in fact this should happen...I hope this makes sense

A user choose between 3 operations.
For example 0. userlogin
1.deleteuser
2.quit

When a user press 0 then he has to insert his name and his surname.
If the user press name over 15 characters is invalid and loops until press valid name.
Then the same for his surname.

Another problem I have is that if press enter in menu choice then either 0 or 1 press or 2 the program prints 'invalid'

I added a fprintf function in your code...run and check the values of choice..

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


void user()
{

	char str[15];
	int i=0;
	
	do{
		printf("\nPrint str.\n");
		fgets(str,15,stdin);
		i = strlen(str);
	}while(i>15);
}

void menu()
{
	printf("Press :\n"); 
	printf("0. \n");
	printf("1. \n");
	printf("2. \n");
}

int get_choice()
{
 int choice = 0 ;
 const char* const valid_choices = "012" ;

 do
 {
   fputs( "Your choice : ", stdout ) ;
   fflush( stdin ) ;
   choice = toupper( fgetc( stdin ) ) ;
   fgetc( stdin ) ;

	fprintf(stdout, "choice->%d, %c\n", choice, choice);//check the value of choice

   if( strchr(valid_choices, choice) == NULL )
   {
     fputs("Invalid option.\n", stdout);
     choice = 0 ;
   }
 } while( choice == 0 ) ;

 return choice ;
}



main()
{
	int choice=0;

	do
   	{
             menu() ;
      	     choice = get_choice() ;

      		switch(choice)
     		 {
          		case 0: user();
			   		 break ;
          		case 1: break ;
          		case 2: break;
		}
     		
	 }while(choice != 2);
  
}

I am trying to write all from the begining.
So I need your help.

#include<stdio.h>

main()
{
 	char choice = '?' ;
 
	while( (choice!='D') && (choice!='K') && (choice!='T') )
	{	
		printf("Choice :");	
		choice = getchar();
		choice = toupper(choice);
	}
 }

When I press D,K, T or even 'enter' , all is ok.
When I press another letter , for example R
I take twro times 'Choice: Choice ' .

Any ideas

I am trying to write all from the begining.
So I need your help.

#include<stdio.h>

main()
{
 	char choice = '?' ;
 
	while( (choice!='D') && (choice!='K') && (choice!='T') )
	{	
		printf("Choice :");	
		choice = getchar();
		choice = toupper(choice);
	}
 }

When I press D,K, T or even 'enter' , all is ok.
When I press another letter , for example R
I take twro times 'Choice: Choice ' .

Any ideas

Try looking at this simple version of what your doing

#include <stdio.h>
#include <stdlib.h>

int main()
{
	char mych = 0;
 	fputs("enter a character->", stdout);
	
	mych = fgetc(stdin);

	fprintf(stdout, "mych->%c, %u\n", mych, mych);

	mych = fgetc(stdin);
 	
	fprintf(stdout, "mych->%c, %u\n", mych, mych);

	exit(EXIT_SUCCESS);
 }

You'll notice you entered one character but you can retrieve two...One is the character you entered plus the character for the newline. If you want to try an experiment try adding another

mych = fgetc(stdin);

to the above program...you'll find that the third mych = fgetc(stdin); waits for you to enter another character. I guess what this is trying to prove is - you have to get rid of the additional character before you loop again...

Thanks again
I understood what happens,
but is there any solution for my while-loop ?

I can't see it anymore not to work.

Thanks again
I understood what happens,
but is there any solution for my while-loop ?

I can't see it anymore not to work.

A very simple solution would be to read your character and then read the newline character into a dumby variable like

char ch, extrach;

fputs("enter a character->", stdout);
ch = fgetc(stdin);
extrach = fgetc(stdin);

If you read your characters like above then your loop will work...

Thanks a lot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.