So, have a program I'm working on and so far things seem like they are going like they are supposed to except one thing. When I print the menu for the user and they input something, if the input is not the input for exiting the program, it prints the menu again twice instead of once, and cant see whats up with it.

Thank you.

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

int main()
{
	// setting up array and a few integers i may need//
	unsigned char imgSet[10][65538] = {0} ;
	//char *input;
	char inputarray[2];
	char *menu;
	char l,L,r,R,a,A,h,H,v,V,t,T,d,D,m,M,q,Q;
	char menuinput=0;
	int i,vSize, hSize;
	int control = 0;
	l = 'l';
	L = 'L';
	r = 'r';
	R = 'R';
	a = 'a';
	A = 'A';
	h = 'h';
	H = 'H';
	v = 'v';
	V = 'V';
	t = 't';
	T = 'T';
	d = 'd';
	D = 'D';
	m = 'm';
	M = 'M';
	q = 'q';
	Q = 'Q';
	menu = ("*****OPTION*****MENU***** \nL: Load image \nR: Remove image \nP: Print image \nA: Average image \nH: Horizontal derivative\nV: Vertical derivative\nT: compute hisTogram\nD: search Digram\nM: local Maxima\nQ: Quit program\n*************************\n\n");
	
	
	
	
	while((control == 0))
	{
	
	printf("%s\nenter option\n: ",menu);
	scanf("%c", &menuinput);

	if((menuinput == l) ||(menuinput == L))
		printf("input is L\n");
	else if((menuinput == 'r') ||(menuinput == 'R'))
		printf("input is %c\n",menuinput);
	else if((menuinput == 'a') ||(menuinput == 'A'))
		printf("input is %c\n",menuinput);
	else if((menuinput == 'h') ||(menuinput == 'H'))
		printf("input is %c\n",menuinput);
	else if((menuinput == 'v') ||(menuinput == 'V'))
		printf("input is %c\n",menuinput);
	else if((menuinput == 't') ||(menuinput == 'T'))
		printf("input is %c\n",menuinput);
	else if((menuinput == 'd') ||(menuinput == 'D'))
		printf("input is %c\n",menuinput);
	else if((menuinput == 'm') ||(menuinput == 'M'))
		printf("input is %c\n",menuinput);
	else if((menuinput == 'q') ||(menuinput == 'Q'))
		printf("Terminating\n",control = 1);
	else
		printf("\n");

	}
	





}

Recommended Answers

All 6 Replies

Line 42 only gets the character entered, its still leaves the new-line character in the stream so when you loop line 42 scanf's the new-line character and then produces the menu again.

Line 42 only gets the character entered, its still leaves the new-line character in the stream so when you loop line 42 scanf's the new-line character and then produces the menu again.

ah tyvm ^^

ah tyvm ^^

You'll have to translate that.

You'll have to translate that.

means Thank You very much. and ^^ is like happy eyebrows.

now that I rewrote it, I'm still getting an issue with it printing twice.(Ive been tinkering with it as well)

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


int main()
{
	// setting up array and a few integers i may need//
	unsigned char imgSet[10][65538] = {0} ;
	//char *input;
	char *filename[80];
	char *menu;
	char l,L,r,R,a,A,h,H,v,V,t,T,d,D,m,M,q,Q;
	char menuinput=0;
	int x,i,vSize, hSize;
	int control1 = 0;
	int control2 = 0;
	l = 'l';
	L = 'L';
	r = 'r';
	R = 'R';
	a = 'a';
	A = 'A';
	h = 'h';
	H = 'H';
	v = 'v';
	V = 'V';
	t = 't';
	T = 'T';
	d = 'd';
	D = 'D';
	m = 'm';
	M = 'M';
	q = 'q';
	Q = 'Q';
	menu = ("*****OPTION*****MENU***** \nL: Load image \nR: Remove image \nP: Print image \nA: Average image \nH: Horizontal derivative\nV: Vertical derivative\nT: compute hisTogram\nD: search Digram\nM: local Maxima\nQ: Quit program\n*************************\n\n");
	
	//void readImage(const char *fname, unsigned char *img);
	
	
	while((control1 == 0))
	{
	
	printf("%senter option:",menu);
	scanf("%c", &menuinput);

	if((menuinput == l) ||(menuinput == L))
	{
		printf("Enter filename:");
		scanf("%s",&filename);
		printf("%s\n\n",filename);

	}
	else if((menuinput == 'r') ||(menuinput == 'R'))
		printf("input is %c\n",menuinput);
	else if((menuinput == 'a') ||(menuinput == 'A'))
		printf("input is %c\n",menuinput);
	else if((menuinput == 'h') ||(menuinput == 'H'))
		printf("input is %c\n",menuinput);
	else if((menuinput == 'v') ||(menuinput == 'V'))
		printf("input is %c\n",menuinput);
	else if((menuinput == 't') ||(menuinput == 'T'))
		printf("input is %c\n",menuinput);
	else if((menuinput == 'd') ||(menuinput == 'D'))
		printf("input is %c\n",menuinput);
	else if((menuinput == 'm') ||(menuinput == 'M'))
		printf("input is %c\n",menuinput);
	else if((menuinput == 'q') ||(menuinput == 'Q'))
		printf("Terminating\n",control1 = 1);
	else
		;

	}
	





}

Im testing it with input of L, which will eventually send the name of a file to another function, but again, once it "executes" i get 2 menus again.

After line 45 put this

fgetc(stdin);

This is a simple but non-rigorous solution.

AH!!! OK, so I wasn't reading your reply correctly the 1st time. Now changed the input to a string and seems to not print double again.

Once again tyvm =)

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.