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 Java: Write a program that generates a random number and asks the user to guess...
In Java: Write a program that generates a random number and asks the user to guess the number and keeps track of how many guesses it took If the user input is negative or zero then the loop must stop reading further inputs and display how many guesses they used If they guess the correct number display a message telling them they got it and exit the program If they guess the wrong number (but still a legal guess) you...
17. Write a program that generates a random number and asks the user to guess what...
17. Write a program that generates a random number and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number. 18. Enhance the program that you wrote for Programming...
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...
Write a program that generates a random number between 1 and 100 and asks the user...
Write a program that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number. Your program should also keep a...
Write a program using a Scanner that asks the user for a number n between 1...
Write a program using a Scanner that asks the user for a number n between 1 and 9 (inclusive). The program prints a triangle with 2n - 1 rows. The first row contains only the square of 1, and it is right-justified. The second row contains the square of 2 followed by the square of 1, and is right justified. Subsequent rows include the squares of 3, 2, and 1, and then 4, 3, 2 and 1, and so forth...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT