Quick C++/C# Problem

So I wrote this guessing game in C#, and I want the user to be able to type 'y' or 'Y' at the end to restart the game. I've included a "do-while" loop, but for some reason it only repeats this part of the code, instead of the whole program:
1
2
3
4
5
6
7
8
9
10
11
12
13
i++;
                Console.WriteLine("Congratulations!");
                if (i <= 10)
                {
                    Console.WriteLine("You guessed {0} times! You must've known!", i);
                }
                else
                {
                    Console.WriteLine("You guessed {0} times! You can do better than that!", i);
                }
                Console.WriteLine("Play again (Y or N)?");
                answer = Convert.ToChar(Console.ReadLine());
                i = 0;

Here's the whole program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Satterwhite_Project_3
{
    public class GuessingGame
    {
        private static Random randomNum = new Random();
        public static void Main(string[] args)
        {
            int guess, i = 0;
            char answer;
            int GuessGenerator = randomNum.Next(1, 1000);
            Console.WriteLine("Guess a number: ");
            guess = Convert.ToInt32(Console.ReadLine());
            do
            {
                while (guess != GuessGenerator)
                {
                    if (guess > GuessGenerator)
                    {
                        Console.WriteLine("Too high.");
                        i++;
                    }
                    else
                    {
                        Console.WriteLine("Too Low.");
                        i++;
                    }
                    Console.WriteLine("Guess a number: ");
                    guess = Convert.ToInt32(Console.ReadLine());
                }
                i++;
                Console.WriteLine("Congratulations!");
                if (i <= 10)
                {
                    Console.WriteLine("You guessed {0} times! You must've known!", i);
                }
                else
                {
                    Console.WriteLine("You guessed {0} times! You can do better than that!", i);
                }
                Console.WriteLine("Play again (Y or N)?");
                answer = Convert.ToChar(Console.ReadLine());
                i = 0;
            }
            while ((answer == 'Y') || (answer == 'y'));
            
        }
    }
}
Hey,

The problem is fairly simple. Essential things from your game are outside the do-while loop when they have to be inside. The reason only the part you showed us is being repeated instead of the whole program, is because of this. Once you win the game, "guess" will be equal to "GuessGenerator", right? So the while loop you have at the top of the do-loop will be skipped. The player needs to get a new number and need to be able to start guesing again -

full program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Satterwhite_Project_3
{
	public class GuessingGame
	{
		private static Random randomNum = new Random();
		public static void Main(string[] args)
		{
                       
		        char answer;
			do
			{
                                int guess, i = 0; 
				int GuessGenerator = randomNum.Next(1, 1000); // Has to be inside the do-loop
				Console.WriteLine("Guess a number: "); // Has to be inside the do-loop
				guess = Convert.ToInt32(Console.ReadLine()); // Has to be inside the do-loop
				while (guess != GuessGenerator)
				{
					if (guess > GuessGenerator)
					{
						Console.WriteLine("Too high.");
						i++;
					}
					else
					{
						Console.WriteLine("Too Low.");
						i++;
					}
					Console.WriteLine("Guess a number: ");
					guess = Convert.ToInt32(Console.ReadLine());
				}
				i++;
				Console.WriteLine("Congratulations!");
				if (i <= 10)
				{
					Console.WriteLine("You guessed {0} times! You must've known!", i);
				}
				else
				{
					Console.WriteLine("You guessed {0} times! You can do better than that!", i);
				}
				Console.WriteLine("Play again (Y or N)?");
				answer = Convert.ToChar(Console.ReadLine());
				i = 0;
			} while ((answer == 'Y') || (answer == 'y'));

		}
	}
}


P.S. I know both c++ and c# have if statements, but this is 100% C#
Last edited on
Topic archived. No new replies allowed.