Need Help with C++ Programming assignment

i'm currently a student struggling to understand C++ Programming and in dire need of assistance.

I am making a Random Number Game, and have been given feedback that i am not so sure of (I did not have any prior Programming qualifications). I have ADHD and its really hard to grasp and comprehend without the proper help my tutor wont give me in person.


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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* My first C++ program */
#include <stdio.h> //header files
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>

main()
{
    int highest = 99, lowest = 1;
    int range = (highest - lowest) + 1;
    int win = 0, game = 0;
    float number_guess = 0;
    int i = 0; //integer definers
    int attemptsCount = 0; //counts number of attempts

    do // A3 loop
    {
        srand((unsigned int)time(0)); /*starting random number generation system(time(0)) for a new number everytime*/
        float randomnumber = (float)(rand() % 99 + 1); /*'rand' in the range 1 to 99 which is attached to function above*/
        number_guess = 0; //defines the two number inputs in question and their relation to one another (higher or lower)

        do // A2 loop
        {
            while ((number_guess < 1 || number_guess > 99) && attemptsCount < 10) //defines boundaries that are not the asked requirement of not between 1 and 99, as well as caps Attempt Count as 5
            {
                printf("I'm thinking of a Number, Input a Number between 1 and 99 and try to guess what it is!");
                printf("(Hint, Number is %f)\n", randomnumber);
                scanf_s("%f", &number_guess); //Allows for number inputs

                if (number_guess < 1)
                {
                    printf("Thats below 1..."); // Tells user they have gone below 1 and against whats been instructed
                    attemptsCount++;
                }
                else if (number_guess > 99)
                {
                    printf("Thats above 99..."); // Tells user they have gone above 99 and against whats been instructed
                    attemptsCount++;
                }
            }

            if ((fabs(randomnumber - number_guess) <= 0.1))
            {
                printf("That's the correct number! Well done! Next Round \n"); //Tells user they have won and stops the program 
                win++;
            }
            else if (number_guess > randomnumber)
            {
                printf("My number is smaller\n"); //defines when number is larger than random
                attemptsCount++;
            }
            else if (number_guess < randomnumber)
            {
                printf("My number is bigger\n"); //defines when number is smaller than random
                attemptsCount++;
            }

        } 
        while (attemptsCount < 10 && win == 0); // End of A2

        if (attemptsCount == 10)
        {
            printf("You have exceeded your attempts, Thank you Come Again."); //Ends program when 10 attempts, previously defined, have been exceeded in this case
        }
    } 
 
    while (win < 4); // End of A3
    {
        printf("Congratulations, you won the game");
        return 0;
    }

} //end of program 



/* That's it! */
Feedback I was given:

"(a) you need to reset 'win' to zero after each A2 cycle, otherwise the loop control won't work (line 60)

(b) this means you need a different variable to control your A3 loop (line 69) - so create a new one to do this

(c) you also need to terminate the A3 loop if the user has to many goes at A2 (line 65 - you print a farewell message, but there is nothing to stop the loop continuing)

(d) when fixing (c), don't jump out of the loop; create another variable to check that each A2 round has actually been won

(e) you also need to reduce the number of A2 attempts for each A3 loop. Currently you are checking 'attemptsCount < 10' and you need to change this to 'attemptscount < maxAttempts' where maxAttempts is another new (integer) variable which starts at 10 but reduces to the number of guesses actually needed at the previous A2."

And now i have to convert the part of code that asks for the player to insert a number between 1-99 into a function, Use an array to file and store all number inputs by the player, then demonstrate the use of a two dimensional array.
Last edited on
welcome.
please put code in code tags, the <> on the editor or
[code][/code]
blocks.

a,b,c and d are pretty much the same problem just detailed out (confusingly to a beginner perhaps). Your loop termination logic needs repairs; there are a few ways to do it, the suggested ones are a simple approach that I think will work. Fix the logic, however it makes sense to you.

e is related but probably worth its own comment. Do a-d first, then circle back here after.

Don't even touch the array and function until you have done the above. One change at a time, test and debug it, then try the next item. Take it slowly. Keep a backup of your code in case you need to undo changes.

not critical, but you are using C or C++ 1998 and earlier headers. That is, your code looks like it is nearly 30 years old.

#include <stdio.h> //should be <cstdio>, but this is not a good header to use in most c++ programs.
#include <math.h> //should be <cmath>
#include <stdint.h> //<cstdint>
#include <stdlib.h> //<cstdlib> also not great in c++ programs
#include <time.h> //<ctime> or better, <chrono>
and consider <random> instead of rand(), which is C and terrible.

typically you use double always and float never, unless you have a good reason (interface to something that uses float or space critical needs where the extra space isnt available or want to save space).

c++ uses cout, cin and not printf/scanf. Those are for C programs, or very old C++ code, or special purpose code.
Last edited on
Sorted the code format, sorry about that

Glad to have the abridge, I am a huge beginner, but my tutor is saying not to use break or exit as loop termination. Could i possibly get an example of where these terminations should go and be worded?
Lines 19-20: Do not call srand() multiple times. srand() sets the RNG to a particular starting point. Calling srand() repeatedly can cause the RNG to return the same random numbers. srand() should be called ONCE at the beginning of main().
http://www.cplusplus.com/reference/cstdlib/srand/

Are you learning C or C++? printf/scranf are C routines. cin and cout are the C++ way of doing I/O.
One possible first refactor as C++ could be as below. You don't say whether it's 10 guesses per round or 10 per game. 10 over the whole game of 4 rounds seems a bit mean for guessing a real number! Usually these number guessing games are restricted to integers within a range so that the maximum required guesses can be determined (7 for 1 - 99 [log299 rounded up is 7]):

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <random>
#include <cmath>

std::mt19937 rng(std::random_device {}());

const int highest { 99 }, lowest { 1 };
const int MaxAttempts { 10 };
const int rounds { 4 };
const double delta { 0.1 };

double getGuess(int& attemptsCount) {
	double number_guess {};

	while ((number_guess < lowest || number_guess > highest) && attemptsCount < MaxAttempts) {
		std::cout << "Attempt " << attemptsCount + 1 << " out of " << MaxAttempts << ". Enter guess: ";
		std::cin >> number_guess;

		if (number_guess < lowest) {
			std::cout << "Thats below " << lowest << "...\n";
			++attemptsCount;
		} else if (number_guess > highest) {
			std::cout << "Thats above " << highest << "...\n";
			++attemptsCount;
		}
	}

	return number_guess;
}

int main() {
	std::uniform_real_distribution<double> distrib(lowest, highest);

	int wins {}, attemptsCount {};

	do {
		const double randomnumber { distrib(rng) };
		bool won {};

		std::cout << "\nRound " << wins + 1 << " of " << rounds << '\n';
		std::cout << "I'm thinking of a Number, Input a Number between " << lowest << " and " << highest << " and try to guess what it is!\n";
		std::cout << "Hint, Number is " << randomnumber << '\n';

		do {
			const double number_guess {getGuess(attemptsCount)};

			if (attemptsCount < MaxAttempts)
				if ((std::fabs(randomnumber - number_guess) <= delta)) {
					++wins;
					won = true;
					std::cout << "That's the correct number! Well done!";
					if (wins != rounds)
						std::cout << " Next round";

					std::cout << '\n';

				} else if (number_guess > randomnumber) {
					std::cout << "My number is smaller\n";
					++attemptsCount;
				} else {
					std::cout << "My number is bigger\n";
					++attemptsCount;
				}

		} while (attemptsCount < MaxAttempts && !won);

		if (!won) {
			std::cout << "You have exceeded your attempts. Thank you and come again.\n";
			return 0;
		}
	} while (wins < rounds);

	std::cout << "Congratulations, you have won the game\n";
}

Last edited on
Topic archived. No new replies allowed.