How do I add a loop in c that when the user guesses the wrong answer, it will loop them back so they can guess again?

0 votes
asked Feb 16, 2018 by Katelyn Price (120 points)
/*me
5DProcess#135.c
Determine the dimensions of the dog fence using given clues*/

#include <stdio.h>

int main()
{
   
//Declare variables
    int total;
    int length;
    int width;
    
    printf("Given information:\n");
    printf("Dawn and Myrna's father want them to build a fence for their dog.\n");
    printf("They have 74 feet of fencing.\n");
    printf("They want the length to be one more than twice the width.\n");
    
    
//Assigning variables (asking the user what it thinks the width will be)
    printf("Guess what the width is:\n");
    scanf("%d", &width);
 
 
/*Manipulate variables
This will determine the total and the length using the width that the user gave us */
    length = 1 + (width*2);
    total =  width + width + length + length;
   
    
    
/*Displaying Results (width)
 aka telling the user if they are right or wrong, too high or too low once thay have guessed the width*/
if(total == 74){
        printf("You are correct! The width is 12 feet! Good job!\n");
}      

else if (total> 74){
        printf("You are incorrect. If the width is %d, as you guessed, then the length would be %d.\n", width, length);
        printf("Together the sum would be %d, which is too high. Please push run again.\n", total);
        
}

else if (total < 74) {
        printf("You are incorrect. If the width is %d, as you guessed, then the length would be %d.\n", width, length);
        printf("Together the sum would be %d, which is too low. Please push run again.\n", total);
        
}
    
    return 0;
}

1 Answer

0 votes
answered Feb 20, 2018 by anonymous

Consider a program that allows the user to practice simple addition infinitely.

#include <iostream>

#include <cstdlib>

using namespace std;

int answer;

int numone;

int numtwo;

for(;;){

numone = rand() % 26;

numtwo = rand() % 26;

cout << numone << " + " << numtwo << " ?" << endl;

do{

cin >> answer;

}while(answer != numone + numtwo);

cout << "Correct" << endl;

}

commented Feb 21, 2018 by anonymous
Forgot to put in int main, but you know where it goes
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...