Question

In: Computer Science

How can I make this not add to the counter when the guess is right? void...

How can I make this not add to the counter when the guess is right?

void guessGenerator(string hangmanStar, string wordFromWordtxt)  
{

   char temp = '\0';
   bool win = false;
   int counter = 0;

   while ((win == false) || (counter == 7))
   {
       char temp;
       cout << hangmanStar << endl;
       cout << "? ";
       cin >> temp;
       counter++;

       for (int i = 0; i < wordFromWordtxt.length(); i++)
       {
           if (wordFromWordtxt.at(i) == temp)
           {
               hangmanStar.at(i) = temp;
           }
       }

       if (wordFromWordtxt == hangmanStar)
       {
           win = true;
           cout << "You win." << endl;
           cout << "The word was: " << hangmanStar << endl;
       }
       else if (counter == 7)
       {
           hangManPicture(counter);
           cout << endl;
           cout << "You lost" << endl;
           cout << "Answer: " << wordFromWordtxt << endl;
           return;
       }
       else
       {
           if (hangmanStar.find(temp) == string::npos)
           {
               cout << endl;
               cout << "Oops";
               hangManPicture(counter);
               cout << endl;
           }
           else
           {
               hangManPicture(counter);
               cout << endl;
           }          
       }
   }
   return;
}

Solutions

Expert Solution

Your code seems to increment the counter by 1 for each guess. Now you want that if the guess is right, the counter shouldn't increment. So just use counter--; at the winning condition - This is set the counter back to the previous count. See below code in bold:

void guessGenerator(string hangmanStar, string wordFromWordtxt)  
{

   char temp = '\0';
   bool win = false;
   int counter = 0;

   while ((win == false) || (counter == 7))
   {
       char temp;
       cout << hangmanStar << endl;
       cout << "? ";
       cin >> temp;
       counter++;

       for (int i = 0; i < wordFromWordtxt.length(); i++)
       {
           if (wordFromWordtxt.at(i) == temp)
           {
               hangmanStar.at(i) = temp;
           }
       }

       if (wordFromWordtxt == hangmanStar)
       {
           win = true;
           cout << "You win." << endl;
           cout << "The word was: " << hangmanStar << endl; counter--;
       }
       else if (counter == 7)
       {
           hangManPicture(counter);
           cout << endl;
           cout << "You lost" << endl;
           cout << "Answer: " << wordFromWordtxt << endl;
           return;
       }
       else
       {
           if (hangmanStar.find(temp) == string::npos)
           {
               cout << endl;
               cout << "Oops";
               hangManPicture(counter);
               cout << endl;
           }
           else
           {
               hangManPicture(counter);
               cout << endl;
           }          
       }
   }
   return;
}


Related Solutions

Guessing game - when I guess, the guess that I make doesn't check correct even though...
Guessing game - when I guess, the guess that I make doesn't check correct even though it is correct. // use external file: line1 linetwo line three linefour line five // end external file. // begin program: #include <iostream> #include <fstream> #include <iomanip> #include <string> #include <time.h> #include <stdlib.h> using namespace std; string trimWSFuntion(string); void guessMess(string, string); int main() { //to store 3 lines string lineFromWordtxt1; string lineFromWordtxt2; string lineToTrim; ifstream myFile; string read_file_name; // this block gets the filename...
Please add to this Python, Guess My Number Program. Add code to the program to make...
Please add to this Python, Guess My Number Program. Add code to the program to make it ask the user for his/her name and then greet that user by their name. Please add code comments throughout the rest of the program If possible or where you add in the code to make it ask the name and greet them before the program begins. import random def menu(): print("\n\n1. You guess the number\n2. You type a number and see if the...
please show me or tell me a trick, on how can i tell right away when...
please show me or tell me a trick, on how can i tell right away when a characteristics equation(system) is 1)overdamped 2)underdamped 3)critically damped 4) nonlinear show each an example write neatly!!!!!
whats a hypothetical monetary proposal i can make for the econmy we are dealing with right...
whats a hypothetical monetary proposal i can make for the econmy we are dealing with right now?
Given a Queue of Integers with the interface: public void enqueue(Integer i) // add to end...
Given a Queue of Integers with the interface: public void enqueue(Integer i) // add to end public Integer dequeue() // remove from front public boolean isEmpty() // return true if empty Write a method rearrange(Queue q) that takes a queue of integers as a parameter and rearranges the order of the values so that all of the even values appear before the odd values and that otherwise preserves the original order of the list. For example, if a Queue contained...
Question: How can I counter-argue this viewpoint? (general summary or thoughts of this post for a...
Question: How can I counter-argue this viewpoint? (general summary or thoughts of this post for a debate)                 The Nurse Practitioners not being independently contracted makes getting their reimbursement more difficult (Does Contact with Managed Care Organizations Remain A barrier for Nurse Practitioners? 2017). This isn’t the ideal way for their reimbursement as they would probably just be on a salary, and not get compensated for all their visits. Such a high percentage of Managed Care Organizations not contracting with...
How do I add a method to make sure that the user inputs in uppercase only...
How do I add a method to make sure that the user inputs in uppercase only and anything entered in lower case throws an error as well as to make sure when they are halving the questioned number they don't enter any decimals? import java.util.*; public class TestCode { public static void main(String[] args) { String choice = "YES"; Random random = new Random(); Scanner scanner = new Scanner(System.in); ArrayList data = new ArrayList(); int count = 0,correct=0; while (!choice.equals("NO"))...
how can i make Activity duration estimates?
how can i make Activity duration estimates?
Java Counter Program I can't get my program to add the number of times a number...
Java Counter Program I can't get my program to add the number of times a number was landed on for my if statements for No12 through No2. My Code: import java.util.Scanner; import java.util.Random;    import java.lang.*;       public class Dice    {               public static void main(String[] args)        {            Scanner in = new Scanner(System.in);            int Continue = 1;            //randomnum = new Random();           ...
Make a java program of Mickey I have the starter program but I need to add...
Make a java program of Mickey I have the starter program but I need to add eyes and a smile to it. import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; import javax.swing.JFrame; public class Mickey extends Canvas { public static void main(String[] args) { JFrame frame = new JFrame("Mickey Mouse"); Canvas canvas = new Mickey(); canvas.setSize(400, 400); canvas.setBackground(Color.white); frame.add(canvas); frame.pack(); frame.setVisible(true); } public void paint(Graphics g) { Rectangle bb = new Rectangle(100, 100, 200, 200); mickey(g, bb); } public void...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT