Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am getting an error in my code where the variable guess is being used without being initialized. This is for a number guessing game

I am getting an error in my code where the variable guess is being used without being initialized. This is for a number guessing game in c++ and at the end the game asks if you want to play again. what doesn't work is if I select H for guess higher or L for lower. Can someone help me fix my code?

source code:

#include

using namespace std;

int main() { int high = 100, low = 1, midpoint; midpoint = (high - low) / 2; int guess, // the guess the program outputs mid; //the new midpoint between the remaining values.

char ch = 'y';

do { cout << "Think of a number between 1 and 100. "; cout << "Is it " << midpoint << "? (h/l/c): "; cin >> ch;

if (ch == 'c') { cout << "Great! Do you want to play again? (y/n): "; cin >> ch;

if (ch == 'y') cout << "Is it " << midpoint << "? (h/l/c): "; }

switch (ch) { case 'h': if (midpoint > guess) mid = (midpoint - guess) / 2; else mid = (guess - midpoint) / 2;

if (mid == 0) mid++; guess = midpoint; midpoint = guess + mid; break;

case 'l': if (midpoint > guess) mid = (midpoint - guess) / 2; else mid = (guess - midpoint) / 2;

if (mid == 0) mid--; guess = midpoint; midpoint = guess - mid; break;

default: cout << "invalid input"; }

}while (ch == 'y');

return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_step_2

Step: 3

blur-text-image_step3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

5-8 What are the advantages and disadvantages of the BYOD movement?

Answered: 1 week ago