Question

In: Computer Science

Write a C program that asks the user to guess a number between 1 and 15(1...

Write a C program that asks the user to guess a number between 1 and 15(1 and 15 are included). The user is given three trials.

This is what I have so far.

/*
Nick Chioma
COP 3223 - HW_2
*/

#include <iostream>
#include <time.h> // needed for time function
#include <stdlib.h> // needed for srand, rand functions

int main () {
  
   int numbertoguess, num, correct, attempts;
  
   srand(time(NULL)); //this initializes the random seed, making a new one every time
   numbertoguess = rand() % 15 + 1; //generates a number between 1 and 15
  
   //Guessing game begins
   printf("Welcome to the guess-a-number game!\n");
   printf("I'm thinking of a number between 1 and 15. What is it? : ");
  
   correct = 0;
   attempts = 0;
  
   while (attempts <= 3) {
       scanf("%d", &num);
  
   if (num > numbertoguess) {
       attempts++;
       printf("No, try something lower : ");
   }
   else if (num < numbertoguess); {
       attempts++;
       printf("No, try something higer: ");
   }
   if (attempts == 3); {
       printf("Sorry, you failed!");
   }
   if (num == numbertoguess) {
       printf("That was %d!", numbertoguess);
       correct = 1;
   }
  

}
  
   return 0;
  
}

My problem is:

1) The program does not actually end after 3 tries

2) If you do guess the correct answer the program does not stop looping and I am not sure why.

3) I also have been getting a weird "[Error] Id returned 1 exit status

If anyone could give me some insight that would be amazing, thank you.

Solutions

Expert Solution

Please find the updated program.. Let me know if you need any further assistance...

C Program:

/*
   Nick Chioma
   COP 3223 - HW_2
*/

#include <stdio.h>
#include <time.h> // needed for time function
#include <stdlib.h> // needed for srand, rand functions

int main () {

   int numbertoguess, num, correct, attempts;

   srand(time(NULL)); //this initializes the random seed, making a new one every time
   numbertoguess = rand() % 15 + 1; //generates a number between 1 and 15

   //Guessing game begins
   printf("Welcome to the guess-a-number game!\n");
   printf("I'm thinking of a number between 1 and 15. What is it? : ");

   correct = 0;
   attempts = 0;

   while (attempts < 3 && correct==0) {
scanf("%d", &num);

       if (num > numbertoguess) {
           attempts++;
           if (attempts == 3) {
printf("Sorry, you failed!");
correct = 1;
}
else {
printf("No, try something lower: ");
}
       }
       else if (num < numbertoguess) {
           attempts++;
           if (attempts == 3) {
printf("Sorry, you failed!");
correct = 1;
}
else {
printf("No, try something higher: ");
}
       }
       else if (num == numbertoguess) {
           printf("That was %d!", numbertoguess);
           correct = 1;
       }
   }

   return 0;
}

Sample Run:


Related Solutions

In C#, write a console-based program that asks a user to enter a number between 1...
In C#, write a console-based program that asks a user to enter a number between 1 and 10. Check if the number is between the range and if not ask the user to enter again or quit. Store the values entered in an array. Write two methods. Method1 called displayByVal should have a parameter defined where you pass the value of an array element into the method and display it. Method2 called displayByRef should have a parameter defined where you...
Question : Write a C program that asks the user to enter an integer between 1...
Question : Write a C program that asks the user to enter an integer between 1 and 7 that represents a weekday number (1 = Sunday, 2 = Monday , …… , 6 = Friday , 7 = Saturday) and it prints the day (i.e., Sunday, Monday, …… , Friday or Saturday). The program continuously asks the user to enter a weekday number till the user responds by ‘N’. and give me an output please use printf and scanf #include...
2. Write a program C++ that asks the user for a number (not necessary to force...
2. Write a program C++ that asks the user for a number (not necessary to force any particular requirements). Write a function with the following signature: double square(double x) that returns the square of the user's number (x * x). 3. Write a C++ program that asks the user for an integer. Write a function that returns 1 of the number is even, and 0 if the number is odd. Use this function signature: int isEven(int x). 4. Write a...
C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                       ...
C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names             Quit If the user selects Guess-number, your program needs to call a user-defined function called int guess-number ( ). Use random number generator to generate a number between 1 – 100. Prompt the user to guess the generated number and print the following messages. Print the guessed number in main (): Guess a number: 76 96: Too large 10 Too small 70 Close...
1/Write a C program that asks the user to enter 5 nums and adds each number...
1/Write a C program that asks the user to enter 5 nums and adds each number to a total. The program should then display the total. To add to the total, the program must call the function void sum(int value, int *ptotal) passing the value the user entered and the address of the total (a local variable in main). The function should add the value to the total. Note that the function is void and does not return anything. Don't...
write a program that: 1) asks the user to enter grades, a negative number is the...
write a program that: 1) asks the user to enter grades, a negative number is the signal to stop entering. (while loop) - add each grade to a list (do not add the negative number to the list) after the while loop to enter grades is finished: 2) use a for loop on the list to calculate the average of all numbers in the list 3) use a for loop on the list to find the largest number in the...
Java Program 1. Write a program that asks the user: “Please enter a number (0 to...
Java Program 1. Write a program that asks the user: “Please enter a number (0 to exit)”. Your program shall accept integers from the user (positive or negative), however, if the user enters 0 then your program shall terminate immediately. After the loop is terminated, return the total sum of all the previous numbers the user entered. a. What is considered to be the body of the loop? b. What is considered the control variable? c. What is considered to...
Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
Write a C program that asks the user to enter 15 integer numbers and then store them in the array.
Write a C program that asks the user to enter 15 integer numbers and then store them in the array. Then, the program will find the second largest element in array and its index without sorting the array. For example, In this array {-55,-2,1, 2, -3, 0, 5, 9, 13, 1, 4, 3, 2, 1, 0}, the second largest element is 9 [found at index 7].
Write a program that asks the user to enter an unsigned number and read it. Then...
Write a program that asks the user to enter an unsigned number and read it. Then swap the bits at odd positions with those at even positions and display the resulting number. For example, if the user enters the number 9, which has binary representation of 1001, then bit 0 is swapped with bit 1, and bit 2 is swapped with bit 3, resulting in the binary number 0110. Thus, the program should display 6. COMMENT COMPLETE CODE PLEASE
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT