### HELPPPPPPPPPPPPPPPPPP

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283`` `````` #include #include #include using namespace std; int main () { const int SIZE = 5; char O[SIZE]; bool win=false, repeat=false; int guess, compnum, turn=0; for(int i=0; i < SIZE; i++) { int srand(time(NULL)); compnum = rand() % 9 + 1; cout << compnum << " "; } for (int i=0; i < (SIZE); i++) { O[i] = '-'; cout << O[i] << " "; } do { cout << "\n\nEnter your guesses separated by a space: \n"; turn ++; for(int i=0; i> guess; if (guess > 9) { cout << "\nThat is an invalid number to guess.\n"; } } if (compnum == guess) { cout <<"\n\nYou finished breaking the code! It took you " << turn << " turn(s)!\n"; win = true; for (int i=0; i < (SIZE); i++) { O[i] = 'A'; cout << O[i] << " "; } break; } //else if() // is in the row but in wrong place //{ // O[] = 'C'; //} else { for (int i=0; i < SIZE; i++) { O[guess] = '-'; cout << O[i] << " "; } } repeat = false; }while(win == false); }``````
Last edited on

(You may find this odd, but trying to figure out what a program must do by looking at source code which isn't even working correctly can be hard.)
Its a guessing game. Just trying to fix this program. So you generate 5 random numbers. Then you have to ask the user to guess 5 of those numbers that were generated. So lets say the secret combo is 5 6 7 8 9 and the user enters 7 6 5 8 3. Then it should be outputted as.

7 6 5 8 3

C A C A -

C means that it is in the secret code, but in the wrong slot.
A means the user guessed a slot correctly.
- means the user missed altogether.

So then the user is prompted to guess over and over until they get all A's
Last edited on

also no global variables
Last edited on
 7 6 5 8 3 C A C A - C means that it is in the secret code, but in the wrong slot. A means the user guessed a slot correctly. - means the user missed altogether.

Ha ha, poop jokes.

My intuition tells me you got C and A backwards, shouldn't it be Correct/Almost? Anyway, for me it was easier writing my own version of this program than to correct yours.

Here it is if you want it:
http://pastebin.com/NqxxZwmN

The differences between my version and yours are:

1) I split up the work in multiple functions, and do not attempt to do everything in main()

2) I use as many elements of C++98 as I can, which may make the code a bit harder to understand, but you have the reference for that:

http://cplusplus.com/reference/

Last edited on
lol :) Thank you for trying to help me - but darn that's some confusing work! Wanna keep it simple.
Last edited on
Just need a simple code for this
Maybe itll work if I put it into different functions
 Just need a simple code for this

 Maybe itll work if I put it into different functions

OK. I'll give you instructions for writing code similar to mine, but "simpler" (as in not using the goodies provided by modern C++).

First step: break up your program into more functions, each with a dedicated role.

Let's have a global constant SIZE (globals are outside functions).
`const int SIZE = 5;`

Let's also write a simple main() to get an idea about what our other functions should be:
 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````#include #include #include const int SIZE = 5; int main() { int actual[SIZE]; // will store the randomly generated numbers int guessed[SIZE]; // will store the user's temporary guessed numbers std::srand(std::time(NULL)); // seed the random number generator // this function will fill the `actual' array with random numbers, 1-9 generate_actual(actual); std::cout << "Input a sequence of " << SIZE << " numbers from 1 to 9:\n"; while (true) { // this function will read user input into the `guess' array read_guess(guess); // this function will print the appropriate "A C A - -" kind of code show_similarity(guess, actual); if (are_the_same(guess, actual)) { std::cout << "Congratulations, you have won!\n"; break; } else std::cout << "Try again!\n\n"; } std::system("PAUSE"); // you may not need this line }``````

Now what you need to do is write the functions that were used above.

Last edited on
I feel like I have just butchered your code, but I am trying to mess around and figure out what you have kindly layed out for me, this is what I am trying to do so far. I just don't know how to store the guesses and rand numbers right now. I havnt slept yet! :( anyways I know for sure I can not have global const

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172`` ``````#include #include using namespace std; void generate_actual(int actual); void read_guess(int guess); void show_similarity(int guess, int actual); int main() { const int SIZE = 5; int actual[SIZE]; // will store the randomly generated numbers int guessed[SIZE]; // will store the user's temporary guessed numbers // this function will fill the `actual' array with random numbers, 1-9 generate_actual(actual); cout << "Input a sequence of " << SIZE << " numbers from 1 to 9:\n"; while (true) { // this function will read user input into the `guess' array read_guess(guess); // this function will print the appropriate "A C A - -" kind of code show_similarity(guess, actual); if (are_the_same(guess, actual)) { cout << "Congratulations, you have won!\n"; break; } else cout << "Try again!\n\n"; } } void generate_actual(int actual) { for(int i=0; i < SIZE; i++) { int srand(time(NULL)); num = rand() % 9 + 1; } return; } void read_guess(int guess) { } void show_similarity(guess, actual) { if () { r += "A "; } else if () { r += "C "; } else { r += "- "; } } ``````
Last edited on
 anyways I know for sure I can not have global const

Are you absolutely sure?

Because that's an extremely stupid requirement and will make things harder for no good reason: all the functions will now need an extra `int size` parameter instead of simply using the global SIZE.

Or how about a macro? You could put a `#define SIZE 5 ` where the global was, for the same effect. A macro isn't technically a variable.

(Still, if I were you I'd go ahead with a global variable SIZE and explain that it makes things simpler. Macros are black magic of old.)

 ``1234567891011`` ``````void generate_actual(int actual) { for(int i=0; i < SIZE; i++) { int srand(time(NULL)); num = rand() % 9 + 1; } return; }``````

actual is supposed to be an array.
Since you're a beginner, you can be excused for writing it as `int actual[SIZE]`.

 ``12345678910111213141516`` ``````void generate_actual(int actual[SIZE]) { for(int i=0; i < SIZE; i++) { // int srand(time(NULL)); // fun fact, this defines a variable // named srand, just as this would: // int srand = time(NULL); // which is not what you want // also, why do you call srand() again? // we already called it in main(), one time is enough! actual[i] = rand() % 9 + 1; } // return; // useless return }``````

While I want to help you, my intent is not to write all your functions for you.
At this point I would suggest getting some sleep (since you keep bringing that up) and maybe reading from the tutorial when you're fresh.

http://cplusplus.com/doc/tutorial/
Last edited on
yeah, sadly it is a requirement to have no global const, thank you for helping me
I seem to not be able to figure out the rest.
Here is what I have so far.
no global const
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374`` ``````#include #include using namespace std; void generate_actual(int actual[]); void read_guess(int guess); void show_similarity(int guess, int actual[]); int main() { const int SIZE = 5; int tries = 0; int actual[SIZE]; int guessed[SIZE]; srand(time(NULL)); generate_actual(actual); cout << "Input a sequence of " << SIZE << " numbers from 1 to 9:\n"; while (true) { read_guess(guess); show_similarity(guess, actual); if (are_the_same(guess, actual)) { cout << "Congratulations, you have won!\n"; break; } else { cout << "Try again!\n\n"; } tries ++; } } void generate_actual(int actual[]) { int const SIZE = 5; for(int i=0; i < SIZE; i++) { actual[i] = rand() % 9 + 1; } } void read_guess(int guess) { return; } void show_similarity(int guess, int actual[]) { generate_actual(actual); if ( guesses == actual) { r += "A "; } else if (guesses == actual) { r += "C "; } else { r += "- "; } }``````
 no global const

Alright, then how about a macro replacement? `#define SIZE 5 `

You've already seen it's a pain; you added a useless `int const SIZE = 5;` in the generate_actual() function.

How many different SIZE variables are you willing to maintain? Because if you change one, you'll have to change all the rest to the same value!

Also, both actual and guess are arrays.
And I have to ask -- is it a requirement that you use arrays? You rejected my initial solution and I thought it was also because I had used std::vector.
yes I have to use arrays, and no macro replacement, never even heard of it. Need to finish this code quickly, running out of time :(
Needs a quick fix. Decided to us macro

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384`` ``````#include #include #include using namespace std; #define SIZE 5 void generate_actual(int actual[]); void show_similarity(int guess[], int actual[]); int main() { int guess[SIZE]; int actual[SIZE]; int tries = 0; srand(time(NULL)); generate_actual(actual); while (true) { show_similarity(guess, actual); if (guess == actual) { cout << "Congratulations, you have won!\n"; break; } else { cout << "Try again!\n\n"; } tries ++; } } void generate_actual(int actual[]) { for(int i=0; i < SIZE; i++) { actual[i] = rand() % 9 + 1; } } void show_similarity(int guess[], int actual[]) { int* num = new int[SIZE]; generate_actual(actual); int r = 0; cout << "\nEnter your guesses separated by a space: \n"; for( int i = 0; i < SIZE; i++) { cin >> num[SIZE]; if (num[SIZE] > 9) { cout << "\nThat is an invalid number to guess.\n"; } guess[i] = num[SIZE]; if (guess[i] == actual[i]) { cout << guess[i] << "A "; } else if (guess[i] != actual[i]) { cout << guess[i] << "- " ; } else { cout << guess [i] << "C "; } } return; }``````
 Need to finish this code quickly, running out of time :(

You're cute and helpless, like a flower.

Edit: well, if you want to use a macro, just add it, then remove all the extra `int SIZE` from the function parameters.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110`` ``````#include #include #include #include void generate_actual(int actual[], int SIZE) { for (int i=0; i < SIZE; ++i) actual[i] = std::rand() % 9 + 1; } bool read_guess(int guess[], int SIZE) { char buffer[256]; char *token; std::cin.getline(buffer, sizeof buffer); token = std::strtok(buffer, " "); for (int i=0; i < SIZE; ++i) { if (token == NULL) { std::cerr << "Not enough numbers were given.\n"; return false; } int temp = std::strtol(token, NULL, 10); if (temp < 1 || temp > 9) { std::cerr << "Given numbers must be between 1 and 9.\n"; return false; } guess[i] = temp; token = std::strtok(NULL, " "); } return true; } bool can_find(int n, int actual[], int SIZE) { for (int i=0; i < SIZE; ++i) if (n == actual[i]) return true; return false; } void show_similarity(int guess[], int actual[], int SIZE) { for (int i=0; i < SIZE; ++i) if (guess[i] == actual[i]) std::cout << "A "; else if (can_find(guess[i], actual, SIZE)) std::cout << "C "; else std::cout << "- "; std::cout << '\n'; } bool are_the_same(int guess[], int actual[], int SIZE) { for (int i=0; i < SIZE; ++i) if (guess[i] != actual[i]) return false; return true; } int main() { const int SIZE = 5; int actual[SIZE]; // will store the randomly generated numbers int guess[SIZE]; // will store the user's temporary guessed numbers std::srand(std::time(NULL)); // seed the random number generator // this function will fill the `actual' array with random numbers, 1-9 generate_actual(actual, SIZE); std::cout << "Input a sequence of " << SIZE << " numbers from 1 to 9:\n"; while (true) { // this function will read user input into the `guess' array if (!read_guess(guess, SIZE)) { std::cerr << "Let's try this again...\n"; continue; } // this function will print the appropriate "A C A - -" kind of code show_similarity(guess, actual, SIZE); if (are_the_same(guess, actual, SIZE)) { std::cout << "Congratulations, you have won!\n"; break; } else std::cout << "Try again!\n\n"; } std::system("PAUSE"); // you may not need this line }``````

Last edited on
Topic archived. No new replies allowed.