hey...can anyone show me what am doin wrong here?? cant get this program to loop till the user enters a character other than "y"....it prompts the first time and its a succes. prompts the second time but does not wait for response....

#include <stdio.h>
#include <string.h>
#define LEN 20
main()
{
	int PorC, pid, status;
	char path[LEN+1];
	char argu[LEN+1];
	char resp;
	do
 	{
		if ((PorC=fork())==0)
		{
			printf ("Enter the path name: ");
			scanf ("%s",path);
			printf ("Enter the argument: ");
			scanf ("%s",argu);
/*opens the program provided as the path..the path is in the  same directory as the main prog so the name of the prog is used*/
			execl (path, path, argu, 0);
			fprintf(stderr, "exec() call failed\n");
			exit(-1);
		}
		else
		{
			printf("this is parent");
			pid = wait(&status);
		//exit(-1);
		}
	printf("\nDo you wish to continue? (Y/N): ");
	scanf("%c", &resp);
	}while (resp == 'y');
}

Recommended Answers

All 6 Replies

Please specify what you want the program to do.

the program pompts the user for a pathname and an argument... there is a main program that does the display of the argument..after the user enters the pathname and argument, the program then displays the argument if the pathname exists and then prompts the user to either continue or quit

scanf("%c", &resp);

The problem is with this part of the code.

When you get a prompt "Do you wish to continue? (Y/N): " you are typing in two charaters
1. 'y'
2. '\n'

so the second time it prompts it is assuming '\n' as input and exiting the loop.

so better use:

resp=getchar();

Happy coding,
Sukhmal

Also, please read this series on scanf() , especially the character and string entries.

instead of this :-

printf("\nDo you wish to continue? (Y/N): ");
scanf("%c", &resp);

try this :-

char a;
-
-
-
-
-
a=getch();//or a=getche(), mind the difference between GETCH() AND GETCHE()

Now your normal code , i.e

if(a!='y'||a!='Y')
{
           do this....;
}
else
{
           do that;
}

thanx alot guys

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.