Linked Lists help

How do I modify this program to track the numbers guessed by the user using a linked list. So basically, i need to keep track if someone already guesses a number and then output if the user did.

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
#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

int main()
{
    srand(time(0));

    int number = rand()%100+1;
    int guess;

    cout << "Im thinking of a number between 1-100. Try to guess what it is: ";
    cin >> guess;

    while(guess!=number)
    {
        if(guess>number)
        {
            cout << "That's too high, guess again: ";
            cin >> guess;
        }
        if(guess<number)
        {
            cout << "That's too low, guess again: ";
            cin >> guess;
        }
    }

    if(guess==number)
    {
        cout << "That's right! It's " << number << endl;
    }

    return 0;
}
Last edited on
closed account (E0p9LyTq)
You don't need a linked list to keep track of previously made guesses. A simple boolean array would work. I added a count of the number of guesses made as well.

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
#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
   srand(time(0));

   int number = rand() % 100 + 1;
   int guess;
   int tries = 1;
   bool guessed[101] = { false };

   std::cout << "I'm thinking of a number between 1-100.\nTry to guess what it is: ";
   std::cin >> guess;

   while (guess != number)
   {

      if (guessed[guess] == true)
      {
         std::cout << "You've already guessed that number!  Try again: ";
      }

      if (guess > number)
      {
         std::cout << "That's too high, guess again: ";
      }
      
      if (guess < number)
      {
         std::cout << "That's too low, guess again: ";
      }
      
      guessed[guess] = true;
      std::cin >> guess;
      tries++;
   }

   if (guess == number)
   {
      std::cout << "\nThat's right! It's " << number << ".\n";
      std::cout << "You took " << tries << " guesses.\n";
   }

   return 0;
}
Well this is a homework assignment and I kinda have to use a linked list.
Last edited on
Topic archived. No new replies allowed.