Thread: Help converting!!!

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    2

    Help converting!!!

    Hi, I'm trying to finish this program for art class and my friend helped me but he put it in some weird stuff that I don't understand. Is there a way to change this code into code that I can use? I use devc++ and we've always been taught to use cout<<"lol"<<endl; and stuff like that but he used some weird printf commands and stuff that I don't get, any help would be appreciated thanks.


    Code:
    #include <cstdlib>
    #include <stdio.h>
    #include <time.h>
    #define title "MathTest"
    #define quests 9 // questions in one game\set
    #define miss 6 // wrong answers allowed before gameover
    //Addition test params (number range): 
    #define add_min 1
    #define add_max 9
    //Subtraction test params (number range): 
    #define sub_min 1
    #define sub_max 9
    //Multiplication test params (number range): 
    #define mul_min 2
    #define mul_max 9
    //Division test params (number range): 
    #define div_min 2
    #define div_max 9
    char uname[16]; // users name
    int key = -1; // for main menu, keyboard input
    int one, two, sol; // variables for test
    int i, q;
    int tries; // tries left (to give right answer)
    //statistics:
    int st_right, st_wrong, st_total, st_questions;
    int sl_right, sl_wrong, sl_total;
    bool solved, playing; // booleans
    
    int random(int min, int max) //returns random in range min..max
    {
        return rand() % (max - min + 1) + min;
    }
    int random(int max) //returns a random in range 1..max
    {
        return rand() % max + 1;
    }
    
    void comment(char con) // returns a random comment\taunt for a specified situation
    {
        if (con == '~') // wrong answer
        {
            switch (random(3))
            {
            case 1: printf("Wrong answer (>_<)"); break;
            case 2: printf("You should try again."); break;
            case 3: printf("Be carefull."); break;
            }
        }
        if (con == '@') // lost
        {
            switch (random(3))
            {
            case 1: printf("You lost the game (;_;)"); break;
            case 2: printf("Game over."); break;
            case 3: printf("How could you let this happen? (T_T)"); break;
            }
        }
        if (con == '!') // right answer
        {
            printf("Right!");
            // todo: add a switch statement here
        }
        if (con == 'v')
        {
            printf("Victory!");
            // todo: add a switch statement here
        }
        if (con == '<') // lower!
        {
            printf("Try smaller number.");
            // todo: add a switch statement here
        }
        if (con == '>') // higher!
        {
            printf("Try higher number.");
            // todo: add a switch statement here
        }
        if (con == '+') // just a bit higher
        {
            printf("A bit higher...");
            // todo: add a switch statement here
        }
        if (con == '-') // just a bit lower
        {
            printf("A bit lower...");
            // todo: add a switch statement here
        }
    }
    
    void stats() // displays global statistics
    {
        printf("\n\t***Game statistics***\n");
        printf("\tTotal questions: %d\n", st_questions);
        printf("\tTotal answers: %d\n", st_total);
        printf("\tRight answers: %d\n", st_right);
        printf("\tWrong answers: %d\n", st_wrong);
        if (st_total != 0)printf("\tAccuracy: %d%c\n", st_right * 100 / st_total, '%');
    }
    
    void stats_local() // displays local statistics
    {
        printf("\n\t***Round statistics***\n");
        printf("\tRound result: ");
        if (playing) printf("Victory\n"); else printf("Defeat\n");
        printf("\tTotal questions: %d\n", q);
        printf("\tTotal answers: %d\n", sl_total);
        printf("\tRight answers: %d\n", sl_right);
        printf("\tWrong answers: %d\n", sl_wrong);
        printf("\tAccuracy: %d%c\n", sl_right * 100 / sl_total, '%');
    }
    
    void test(char type) // test\game function.
    {
        sl_right = 0; sl_wrong = 0; sl_total = 0;
        playing = true;
        for (q = 1; q <= quests && playing; q++)
        {
            tries = miss;
            solved = false;
            //Generate math problem
            if (type == '+')
            {
                one = random(add_min, add_max);
                two = random(add_min, add_max);
                sol = one + two;
                printf("\n%d. %d + %d = ?\n", q, one, two);
            }
            if (type == '-')
            {
                sol = random(sub_min, sub_max);
                two = random(sub_min, sub_max);
                one = sol + two;
                printf("\n%d. %d - %d = ?\n", q, one, two);
            }
            if (type == '/')
            {
                sol = random(div_min, div_max);
                two = random(div_min, div_max);
                one = sol * two;
                printf("\n%d. %d / %d = ?\n", q, one, two);
            }
            if (type == '*')
            {
                one = random(mul_min, mul_max);
                two = random(mul_min, mul_max);
                sol = one * two;
                printf("\n\n%d. %d * %d = ?\n", q, one, two);
            }
            //Answering loop
            while (tries > 0 && !solved)
            {
                //Get the answer from user
                printf("   Your answer > "); scanf("%d", &i);
                sl_total++;
                if (i == sol) // right answer
                {
                    sl_right++;
                    comment('!');
                    solved = true;
                }
                else // wrong answer
                {
                    sl_wrong++;
                    tries--;
                    if (tries > 0)
                    {
                        // Provide different comments, depending on how near the answer was.
                        if (i < sol + 4 && i > sol) comment('-');
                        else if (i > sol - 4 && i < sol) comment('+');
                        else if (i < sol + 16 && i > sol) comment('<');
                        else if (i > sol - 16 && i < sol) comment('>');
                        else comment('~');
                        printf("\nYou have %d tries remaining!\n", tries);
                    }
                    else // no tries\lives remaining - game over
                    {
                        solved = true;
                        playing = false;
                        comment('@');
                        printf("\nThe correct answer was... %d!", sol);
                    }
                }
            }
        }
        q--; // decrease q by one to hold actual number of passed questions
        st_questions += q;
        st_total += sl_total;
        st_right += sl_right;
        st_wrong += sl_wrong;
        if (playing) comment('v');
        stats_local();
    }
    int main()
    {
        srand(time(NULL)); // set random seed from current time
        //Get users name:
        printf("What is your name?\n > "); gets(uname);
        while (1) // main loop:
        {
            printf("\n\n");
            printf("Welcome, %s, to %s!\n", uname, title);
            printf("Please choose one of the following options: \n");
            printf(" [1] Addition\n");
            printf(" [2] Subtraction\n");
            printf(" [3] Division\n");
            printf(" [4] Multiplication\n");
            printf(" [5] Statistics\n");
            printf(" [0] Exit\n");
            //Get input:
            printf(" > "); scanf("%d", &key);
            //Cases:
            if (key == 1) test('+');
            if (key == 2) test('-');
            if (key == 3) test('/');
            if (key == 4) test('*');
            if (key == 5) stats();
            if (key == 0) return 1;
        }
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    30
    I suggest that you just google "printf". There are plenty of references on what it's about.
    As far as I know, there's no tool for converting "printf" to "cout".

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    This is what happens when you have others do your homework for you. Good luck on your tests.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting decimals to individuals
    By clag in forum C Programming
    Replies: 27
    Last Post: 10-03-2009, 03:08 PM
  2. Replies: 1
    Last Post: 09-20-2009, 07:39 AM
  3. Converting Sign Magnitude Integer Binary Files
    By Arthur Dent in forum C Programming
    Replies: 7
    Last Post: 09-13-2004, 10:07 AM
  4. Converting from Screen to World Coordinates
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 05-11-2004, 12:51 PM
  5. converting string to float
    By twhubs in forum C Programming
    Replies: 1
    Last Post: 09-16-2001, 09:02 AM