I need help fixing up my code

https://sites.google.com/site/uiccs141/programs/2-mastermind
I need help, I am trying to make my output something like the one on the website I posted. When I run the program, I am not getting what I am supposed to be getting.

#include <string.h>
#include <iostream>
#include <time.h>
#include <cstdlib>
#include <iomanip>
using namespace std;


//-------------------------------------------------------------------
int main()
{
char userInput;
int RandIndex = (rand() % 10); //first random number
int RandIndex1 = (rand() % 10); //second random number
int RandIndex2 = (rand() % 10); // third random number
int num1;
float numOrder;
float guessCount;


cout << "Program: 2 MasterMind" << endl;
cout << "The program selects 3 distinct random digits 0..9." << endl;
cout << "On each turn you guess 3 digits. The program indicates" << endl << "how many are correct."; //Introduction
cout << " You have 10 moves to guess the number." << endl << "Good luck!" << endl;
cout << endl;
cout << "Press 's' to set the three digits, or 'r' to randomize them:" << endl << endl;
cin >> userInput;
cout << "Input of 000 displays the hidden digits. Input of 999 exits the program.";
cout << endl << endl << "In place" << " Out of place" << endl;
cout << "--------" << " ------------" << endl;



if (userInput == 's'){ //s input
for(int i=0;i<10;i++){
guessCount = 0;
if (guessCount< 10)

guessCount = guessCount + 1;


numOrder = 0;
if (numOrder < 10){
numOrder = numOrder + 1;}
cout << numOrder << "." << "Your guess:" << endl;

cin >> setw(999) >> num1;
cout << "You entered " << num1 << endl;

}
}
if (userInput == 'r'){ //r input
for(int i=0;i<10;i++){
guessCount = 0;
if (guessCount< 10)

guessCount = guessCount + 1;


numOrder = 0;
if (numOrder < 10){
numOrder = numOrder + 1;}
cout << numOrder << "." << "Your guess:" << endl;
cout << "You entered: " << RandIndex << RandIndex1 << RandIndex2 << endl;
}
}
cout << endl;
cout << "Exiting...";

return 0;
}

Hello jthoma90,

Welcome to the forum.

While I work on figuring out your program you can work on this.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.

Andy
Hello jthoma90,

Here is your program with a few changes:

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <string.h>
#include <iostream>
#include <ctime>  // <--- Should use the C++ header file "ctime"
#include <cstdlib>
#include <iomanip>
#include <cctype>  // <--- Added.

//using namespace std;  // <--- Best not to use.


//-------------------------------------------------------------------
int main()
{
	srand(static_cast<std::size_t>(time(NULL)));  // <--- Added.

	char userInput{ 's' };
	int RandIndex = (rand() % 10); //first random number
	int RandIndex1 = (rand() % 10); //second random number
	int RandIndex2 = (rand() % 10); // third random number
	int num1{};
	float numOrder{};  // <--- "double" is the preferred type.
	float guessCount{};


	std::cout << "Program: 2 MasterMind" << std::endl;
	std::cout << "The program selects 3 distinct random digits 0..9." << '\n';
	std::cout << "On each turn you guess 3 digits. The program indicates" << '\n' << "how many are correct."; //Introduction Changed the "endl" to "\n".
	std::cout << " You have 10 moves to guess the number." << '\n' << "Good luck!" << std::endl;
	std::cout << std::endl;

	//std::cout << "Press 's' to set the three digits, or 'r' to randomize them: ";
	std::cout << "Press 's' to set the three digits, or 'r' to randomize them: " << userInput << '\n';  // <--- Used for testing.

	//std::cin >> userInput;  // <--- Uncomment for normal use.
	userInput = std::tolower(userInput);  // <--- Added to make sure input is in lower case. Uses header file "cctype".

	std::cout << "Input of 000 displays the hidden digits. Input of 999 exits the program.";
	std::cout << std::endl << std::endl << std::setw(40) << ' ' << "In place" << " Out of place" << std::endl;
	std::cout << std::setw(40) << ' ' << "--------" << " ------------" << std::endl;



	if (userInput == 's') { //s input
		for (int i = 0; i<10; i++)
		{
			guessCount = 0;  // <--- needs to move above for loop.

			if (guessCount< 10)
				guessCount = guessCount + 1;  // <--- Or just guesscount++;

			numOrder = 0;

			if (numOrder < 10)
			{
				numOrder = numOrder + 1;
			}

			std::cout << numOrder << "." << " Your guess: ";

			std::cin >> num1;  // <--- Changed. Removed the "setw".
			std::cout << std::setw(11) << ' ' << "You entered " << num1 << std::endl;
			std::cout << std::endl;  //<--- Used for testing as a break point.
		}
	}

	if (userInput == 'r') { //r input
		for (int i = 0; i<10; i++)
		{
			guessCount = 0;  // <--- needs to move above for loop.

			if (guessCount< 10)
				guessCount = guessCount + 1;

			numOrder = 0;

			if (numOrder < 10)
			{
				numOrder = numOrder + 1;
			}

			std::cout << numOrder << "." << "Your guess:" << std::endl;
			std::cout << "You entered: " << RandIndex << RandIndex1 << RandIndex2 << std::endl;
		}
	}

	std::cout << std::endl;

	std::cout << "Exiting..." << std::flush;

	return 0;
}

Read the comments in the program.

I changed one of the header files "time.h" to the C++ header file "ctime". This does the same things.

I added a header file "cctype". You will see what I did in the middle of the beginning "cout" statements.

I added the constant at the top of the program partly as an example and to make changes easier.This way you can change th 's' to 'r' to make testing easier.when finished testing and everything is working correctly you can remove the line "constexpr char TESTTYPE{ 's' };" and remove th "TESTTYPE" from the line "char userInput{ TESTTYPE };" just leaving the empty{}s.

You initialized the first three "int"s with a call to "rand()". It may not be necessary to initialize all the other variables, but IMHO I feel it is a good idea.

Because you use "rand()" to initialize the first three "int"s I had to put "srand()" at the beginning of the "main" block to seed the RNG properly.This only needs to be done once.

"double" is preferred over "float", but neither of these variables need to be anything more than an "int". "number Order" and "guessCount" are just counters.Unless you have something else in mind for these variables a simple "int" will do.

The "cout" ststements I added the blank lines to break it up to make it easier to read and work with.

In the center section I changed it around a bit to show you what you can do to make testing easier.

In the third section I used the "setw()" to push the headings and dashed to the right to where they should be.

In the first for loop the first thing you do is set "guessCount" equal to zero.The problem is that "guessCount" will never achieve anything greater than one before it is reset to zero next time through the for loop.For the if statement to work properly the "guessCount = 0;" needs to be above the for loop. The same for the other if statement. The same is true for "numOrder".

I would suggest making the two if statements "if", "else if" and "else" statements. This way the "else" can deal with an invalid choice.

In the "s" section you enter the guess into a single variable, but you initialized three variables "randIndex?" with three different values. Now the question is how do you tale a single three digit number and compare it to three different variables? ou could redefine "num1" as a std::string and convert each element of the string into a seperate variable or input the number into three separate variables.

In the "r" section you have no user input, but print out the three variables that hold the three random numbers from earlier in the program. This is not what you want.

In each section you also need to print the results of checking the input number(s) to see haw many match and if any are in the right place or not and show the results on the screen. If you know about functions this would be a good place for one or two.

You have added to "guessCount", but never do anything with it beyond the if statement.

You have a good start, bu still have a ways to go yet. A lot if not all I have showed you will help you along the way.

Hope that helps,

Andy
closed account (9G3v5Di1)
andy can you help me writing a code please
This helps a lot Andy, thank you!
By any chance, would you know how to incorporate the in place and out of place values.
Thank you,
Joshua Thomas
Hello jthoma90,

By any chance, would you know how to incorporate the in place and out of place values.

I know it needs done, but I have not worked that far yet.

I think it is time for a new laptop because I am having so many problems with this one. One of the reasons I have not been able to do much.

I will see what I can come up with today.

Andy
Topic archived. No new replies allowed.