Click to See Complete Forum and Search --> : having problem on getting TWO inputs


probug
May 16th, 2007, 09:57 PM
frens, i am new to c programming and am having problem how to handle two inputs seperately by scanf() or getchar().

I mean, I want to get Y or N first time, and then, i again, i want know whether the value is h ( high) or l (low) if the answer is N.

do you any idea?

while((response=getchar())!='y')
{
printf("h or l");
getchar(ch);
if(ch=='h')
do something;
else
do something;
printf("Y or N");
}


Am i missing something here?

mariocatch
May 16th, 2007, 10:21 PM
#include <iostream>
using namespace std;

int main()
{

char ch;
char response;
do
{
system("cls");
printf("You may quit at anytime by entering 'Y'\n");

printf("Select h or l (y to quit): _\b");
ch = getchar();
tolower(ch);
if(ch == 'l')
printf("You've selected 'l'\n");
else if(ch == 'h')
printf("You've selected 'h'\n");
else if(ch == 'y')
{
system("cls");
printf("Quitting\n\n");
break;
}

getchar();
printf("\n");
printf("Would you like to quit?\n");
printf("(Y)es or (N)o: _\b");
response = getchar();
tolower(response);
if(response == 'y')
{
system("cls");
printf("Quitting\n\n");
break;
}

printf("Press enter/return to continue\n");

}while((response=getchar())!='y');

return 0;
}


do while loop works better in your scenario.

probug
May 16th, 2007, 11:41 PM
what i am trying to guess the number you've imagined between 1 ~ 100 in binary search way.



int main(void)
{
int guess=50;
char response;

printf("Pick a number between 1~100.\n ");
printf("I will guess then. Type y for if matched, n otherwise.\n");

printf("Is your number %3d? ",guess);

while((response=getchar())!='y')
{
printf("Is the number high \"h\" or low \"l\" : ");
scanf("%c",&ch);
if(ch=='h')
guess/=2;
else if(ch=='l')
guess=(guess+100)/2;
else
printf("Wrong choice, h or l only");
printf("Well, then, is is %d?\n",guess);
while(getchar()!='\n'); // not sure, whether we need or not
continue;
}
printf("I know I could do it.\n");

return 0;
}


=============
OUTPUT
Pick a number between 1~100.
I will guess then. Type y for if matched, n otherwise.
Is your number 50? n
<b>
Is the number high "h" or low "l" : Wrong choice, h or l onlyWell, then, is is 5
0?</b>

I am having problem here, i just want to print "Is the number high or low". But it comes Wrong choice, h or I only welll...etc too. How can I correct?