mercredi 17 février 2016

area of shape game - close but don't understand what i'm doing wrong - i'm a beginner go easy please

    You'll get the gist of this when you read my code the is supposed to be

   the output i have worked for days on it and cannot figure it out my code

will be below this. This is what the console output should look like ** - * -

                                                    • ie
                      • Area of a Shape * - * - * - * - * -

      Choose a shape:

      1. square

      2. rectangle

      3. triangle

      4. quit game

      ">" 1(\n)

      Your square has a side length of 2.(\n)

      Can you guess the area of the square? 4(\n)

      Good job! You earned a point.

      Total points so far is 1.

                                                            • -

      Choose a shape:

      1. square

      2. rectangle

      3. triangle

      4. quit game

      ">" 1

      Your square has a side length of 6.

      Can you guess the area of the square? 36

      Good job! You earned a point.

      Total points so far is 2.

                                                            • -

      Choose a shape:

      1. square

      2. rectangle

      3. triangle

      4. quit game

      ">" 2

      The width of your rectangle is 1 and the length is 7.

      Can you guess the area of the rectangle? 7

      Good job! You earned 1 point.

      Total points so far is 3.

                                                            • -

      Choose a shape:

      1. square

      2. rectangle

      3. triangle

      4. quit game

      ">" 2

      The width of your rectangle is 6 and the length is 5.

      Can you guess the area of the rectangle? 25

      Guess again: 36

      Guess again: 11

      Sorry, the area was 30. Try again.

      Total points so far is 3.

                                                            • -

      Choose a shape:

      1. square

      2. rectangle

      3. triangle

      4. quit game

      ">" 5

      You entered an incorrect value.

                                                            • -

      Choose a shape:

      1. square

      2. rectangle

      3. triangle

      4. quit game

      ">" 3

      The base of your triangle is 2 and the height is 2.

      Can you guess the area of the triangle? 4

      Guess again: 1

      Guess again: 8

      Sorry, the area was 2. Try again.

      Total points so far is 3.

                                                            • -

      Choose a shape:

      1. square

      2. rectangle

      3. triangle

      4. quit game

      ">"4

      Thanks for playing!

      You earned a total of 3 points!!


here's my code so far

#include <stdio.h>
#include <math.h>


void print_menu()
{
printf("- * - * - * - * - * - * - * - * - * - * - * - * - * - * -\n- * - * -        * - * - * Area of a Shape * - * - * - * - * -\n");
printf("Square  enter 1\n");
printf("Rectangle enter 2\n");
printf("Triangle enter 3\n");
printf("Exit Program enter 4\n\n");
printf(">");
}


int main()
{
//int inRange = 1 + randomObject.nextInt(11);
//int inRange = 2 + randomObject.nextInt(11);
int n, choice, points, answer1, answer2, answer3 = 0;
//int square, rectangle, triangle;
//print_menu();




    while(choice != 4)
    {

            print_menu();
            scanf("%d", &choice);
            /*
            printf("- * - * - * - * - * - * - * - * - * - * - * - * - * - *-\n- * - * - * - * - * Area of a Shape * - * - * - * - * -\n");
            printf("1.square enter 1\n");
            printf("2.rectangle enter 2\n");
            printf("3.triangle enter 3\n");
            printf("4.quit game enter 4\n");
            */
     if (choice == 1)
     {
             printf("%d\n", n);
                    printf("Your square has a side length of. %i \n", n);
                    printf(" Can you guess the area of the square?. %d \n", n);
                    scanf("%d", &answer1);

   if (answer1 == rand() % 10 + 1)
                            {

                printf("%d\n", n);
                printf("Good job! you earned a point.\n");
                points++;
                printf("Total points so far is %d\n\n", points);
                scanf("%d", &points);
                printf("- * - * - * - * - * - * - * - * - * - * - * - * - * - * -");
                            }
                            else 
                            {
                printf("Guess again:  ");
                printf("Guess again:   \n\n");
                printf("Sorry, the area was %d. Try again.", answer1);
                printf("Total points so far is %d\n\n", points);
                printf("- * - * - * - * - * - * - * - * - * - * - * - * - * - * -");
                            }

     }




     else if (choice == 2)
     {
                    printf("The width of your rectangle is %d and the length is %d.\n", n, answer2);
                    printf("Can you guess the area of the rectangle? %d\n\n", points);
                    scanf("%d", &answer2);

                    if (answer2 == rand() % 10 + 1)
                    {
                printf("%d\n", n);
                    printf("Good job! You earned a point.\n");
                    points++;
                    printf("Total points so far is %d.", points);
                    printf("- * - * - * - * - * - * - * - * - * - * - * - * - * - * -");
                    }
                    else
                    {
                    printf("Sorry, the area was %d. Try again.\nTotal points so far is %d.\n",n, points);
                                    printf("- * - * - * - * - * - * - * - * - * - * - * - * - * - * -");
                    }

     }




     else if (choice == 3)
     {
                    printf("The base of your triangle is %d and the height is %d.\n",n, n);
                    printf("Can you guess the area of the triangle? %d \n\n", n);
                    scanf("%d", &answer3);

                    if (answer3 == rand() % 10 + 2)
                    {
                    printf("Good job! You earned a point.\n");
                    points++;
                    printf("Total points so far is %d.", points);
                    printf("- * - * - * - * - * - * - * - * - * - * - * - * - * - * -");
                    }
                    else
                    {
                    printf("Sorry, the area was %d. Try again.\nTotal points so far is %d.\n", answer3, points);
                    printf("- * - * - * - * - * - * - * - * - * - * - * - * - * - * -");
                    }

              }

              if (choice==4) /* User can choose to exit*/
             {
                    printf("Thanks for playing!\nYou earned a total of %d points!!!", points);

            }

    }

}

if someone could please help me and explain what i'm doing wrong, i would greatly appreciate it i hit a serious speed bump. Instructions:

Write a program that does the following:

  1. prompts the user to choose a shape from a given list: square, rectangle, triangle

  2. uses a random number generator to provide dimensions for the shape

a. for the square and rectangle, the values should be between 1 – 10, inclusive

b. for the triangle, the values should be even numbers between 2 – 10, inclusive

  1. gives the user 3 chances to enter the correct area of the shape

  2. gives the user 1 point each time the area is guessed correctly within the 3 tries

  3. lets the user choose another shape and guess its area

  4. shows the user the total number of points earned each time a new shape is chosen as well as at the end when the user chooses to quit the program




Aucun commentaire:

Enregistrer un commentaire