Thread: how do I rest/sum only the half?

  1. #1
    Registered User lalorobot's Avatar
    Join Date
    Jun 2012
    Posts
    6

    Question how do I rest/sum only the half?

    I try making a little program that sums or rests depending on user input how do I make that? ps. I want to sum/rest an integer not float

    if you want to see the source code here it is:
    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <string.h>
    int MaxNum = 1000; //The computer will guess the number in a maximum of 10 tries
    char Q [5] = "Q";
    char QPH [5];  //The actual Quit the user will type in the game
    char P [5] = "P";
    char moreOrLess[100];
    char yes [2] = "Y";
    char NoItIsLess [3] = "NL";
    char NoItIsMore [3] = "NM";
    int num = 500;
    
    
    int main()
    {
        system("cls");
        HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
        SetConsoleTextAttribute(consoleHandle, 10);
        printf("                              Guess the number!                              \n"); //The spaces are to align it to the center
        SetConsoleTextAttribute(consoleHandle, 9);
        printf("                        Press P to enter the game                              \n");
        SetConsoleTextAttribute(consoleHandle, 11);
        printf("                         Press Q to exit the game                              \n");
        SetConsoleTextAttribute(consoleHandle, 14);
        printf("       Type Help to see the instructions (recomended for first time users)                              \n");
        SetConsoleTextAttribute(consoleHandle, 12);
        scanf("%s", QPH);
        if (strcmp(Q, QPH) == 0)
        {
            SetConsoleTextAttribute(consoleHandle, 15);
            printf("                         Come back again!                         ");
            SetConsoleTextAttribute(consoleHandle, 7);
        }
        if (strcmp(P, QPH) == 0)
        {
            SetConsoleTextAttribute(consoleHandle, 15);
            printf("Think of a number from 1 to 1000 I will guess it. ");
            printf("If you dont believe me lets try it...\n");
            printf("The number is %d.If it is correct say Y if it is incorrect and your number is more say NM or if it is incorrect amd it is less say NL\n", num);
            scanf("%s", moreOrLess);
            if (strcmp (yes, moreOrLess) == 0)
            {
                printf("Thanks for playing");
            }
            if (strcmp (NoItIsLess, moreOrLess) == 0)
            {
                num -= 250 ; //here is where I need help 
                printf("The number is %d", num);
            }
            if (strcmp (NoItIsMore, moreOrLess) == 0)
            {
                num += 250 ; //and here
                printf("The number is %d", num);
            }
        }
        SetConsoleTextAttribute(consoleHandle, 7);
        return 0;
    }
    Last edited by lalorobot; 06-15-2012 at 04:37 PM.

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    What you are trying to do is called a binary search. A quick Google search will bring you to this page, which has some sample implementations: Binary search algorithm - Wikipedia, the free encyclopedia

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Monitor displaying half good / half bad
    By Hiyo in forum Tech Board
    Replies: 3
    Last Post: 12-16-2006, 10:06 PM
  2. Half Humanoid, half Chimpanzoid
    By Zewu in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 06-19-2004, 04:23 PM
  3. get rid of rest
    By pode in forum Networking/Device Communication
    Replies: 1
    Last Post: 10-20-2003, 06:55 PM
  4. Wanna get some rest?
    By golfinguy4 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 07-09-2003, 10:27 PM
  5. rest
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-29-2002, 05:32 PM