CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2007
    Posts
    20

    having problem on getting TWO inputs

    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?
    Code:
    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?
    "burning desire to learn"
    own guru, NP

  2. #2
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    403

    Re: having problem on getting TWO inputs

    Code:
    #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.
    Last edited by mariocatch; May 16th, 2007 at 10:24 PM.

  3. #3
    Join Date
    Apr 2007
    Posts
    20

    Re: having problem on getting TWO inputs

    what i am trying to guess the number you've imagined between 1 ~ 100 in binary search way.

    Code:
    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?
    "burning desire to learn"
    own guru, NP

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured