New programmer stuck on self teaching question

Hi Guys, im currently teaching myself c++ in my offtime and im really enjoying it. However when i get to stops in the road it can be hard to push forward without some guidance. thats where you guys come in. im currently working on
a simple tast...

Take 10 integer inputs from user and store them in an array.
Again ask user to give a number. Now, tell user whether that number is present in array or not.

to expand on the question im currently using two arrays. one to hold random numbers, and one to hold the guess attempts of the user. the problem im running into is that if i guess the numbers in order, they will show up as correct, but if i guess the numbers out of order, its likes the array isnt being searched for the guessed number. ive included code below. please be kind, as im about 6 months in.


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

//1) Take 10 integer inputs from user and store them in an array.
//2) Again ask user to give a number. Now, tell user whether that number is present in array or not.


using namespace std;

int main ()
{

srand (time(nullptr));
int randomNumberArray [10]; // randomNumber array.
int guessedNumbers[10]; // array to hold users guess attempts.


cout << "We will now assign 10 random numbers (between 1 and 20) to an array." << endl;
for (int i = 0; i < 10; i++){
    randomNumberArray [i] = rand () % 20 + 1;
}
for (int i = 0; i < 10; i++){
    cout << randomNumberArray [i] << endl; // displaying array for testing purposes.
}

cout << "You will now try to guess what numbers are in the array:" << endl;
cout << "What is your first guess?"<< endl;

for (int i = 0; i < 10; i++){
cin >> guessedNumbers[i];
    if (guessedNumbers[i] == randomNumberArray[i])
    {
        cout << "You have guessed a correct number!";
    }
    else if (guessedNumbers[i] != randomNumberArray[i])
    {
        cout << "Incorrect, please guess again: ";
    }
}

}
Last edited on
Again ask user to give a number.
Now, tell user whether that number is present in array or not.

A number. One number.
Present in array. Does the array contain the number?
Is any of the elements in the array equal to the number?
Can you find number from the array?

That is different from what you do.

You compare guess i to exactly one element of the array; the ith element.

You can repeat the "guess and check" if you want:
1
2
3
4
5
6
7
8
9
10
for ( /* as many times as you like */ ) {
  int guess {};
  if ( std::cin >> guess ) {
    auto p = std::find( randomNumberArray, randomNumberArray+10, guess );
    if ( p != randomNumberArray+10 )
      std::cout << "Element found\n";
    else
      std::cout << "Element not found\n";
  }
}

In the above code the Standard Library algorithm std::find does the "dirty work".
Learning to use those algorithms is useful.
Learning to invent logic for a task is important too.
This won't answer your question, however, I think it's still worth pointing out.

1.) First, for your code:
1
2
3
4
5
6
for (int i = 0; i < 10; i++){
    randomNumberArray [i] = rand () % 20 + 1;
}
for (int i = 0; i < 10; i++){
    cout << randomNumberArray [i] << endl; // displaying array for testing purposes.
}

You should be able to put it into one for loop like so:
1
2
3
4
for (int i = 0; i < 10; i++) {
    randomNumberArray [i] = rand () % 20 + 1;
    cout << randomNumberArray [i] << endl; // displaying array for testing purposes.
}

It should serve the same function.


2.) Also, I recommend being consistent with your bracket placement. While it's not required, it does make your code more neat and readable. There are two common formats:
1
2
3
if (boolVal == true) {

}

and
1
2
3
4
if (boolVal == true)
{

}

Both formats are equally good.

3.) Indent. You already do this for most stuff which is good. The only part you didn't do this for was after the int main() {
For example:
1
2
3
4
5
int main() {
    // Your code here. It's indented.

    return 0;
}


4.) At the end of int main(), generally it's good to put "return 0;". Why? To be honest, I'm not 100% sure the reason as I'm still learning myself. But I believe it has to do with int main having a data type (int), so it's expected to return a value like all functions except void (you'll learn more about this later).

Format:
1
2
3
4
int main() {

    return 0;
}


Other than that, looks pretty good to me! I'm sure someone on the forums will be able to help you figure out your question. :)
Last edited on
So you already got plenty hints about lexical exactitude and a solution as well, here just my idea, not to store the ten numbers to guess in an array, instead use flags/bits numbered from 1..20 and mark "the good ones" with true while the others stay false. Vector<bool> would do, but bitset offers a function to count set bits.
My suggestion:
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
#include <iostream>		/* cout, cin, endl */
#include <bitset>
#include <ctime>       /* time */
using namespace std;

bitset<21> z_flag(string("0"));	// rightmost bit not used

int main()
{
	unsigned guess;

	srand (time(NULL));	// haphazardly seed
	for (; z_flag.count() < 10;)  // don't mind the same number twice
		z_flag[rand() % 20 + 1] = true;  // mark the "good ones"
	for (;;)			// do forever
	{
		cout << "Enter a number 1..20 (0 to quit)\n";
		cin >> guess;
		if (guess == 0) break;  // user stopped at will
		if (guess > 20)
		{
			cout << guess << " is too big. ";
			continue;
		}
		if (z_flag[guess])
			cout << "You have guessed a correct number!\n";
		else
			cout << "No-no-no-no-no...\n";
	}
//	return 0;
}

Edit: a typo
Edit: replaced exit(...); by break; The result is in this case the same, but looks nicer.
Edit: replaced if (cin >> guess) by cin >> guess;
Last edited on
Sorry, caedus, for the round-about answers...

The crux of the problem is to search through an array.

So, given an array of numbers, say, { 12, 94, -7, 13, 8 }, the question becomes:

Is 4 in the array? (because the user input the number 4)


How events are ordered is one of the weird things to get used to with programming.
Your chain of events is a little off. It should be:

  (1) Create an array of random numbers
  (2) Ask the user for a number
  (3) Find the number in the array
  (4) Tell the user whether or not you found the number

Currently you have step 2 part of step 3. Ask for the (single) number to look for first, then use a loop to see if that number is in the array.

I’m pretty sure you will get it now. Don’t be dismayed by seemingly rude answers. Few of us here intend to be rude, we are trying to help the best we know how.

Hope this helps.
All of your help is really appreciated. Its amazing how one answer can generate so many answers with such different ways of solving the problem.

Duthomhas Your help here is what im looking for the most. the other posts are great but a little a head of me using bitset and such.
ive redone the code and i have just one follow up question.
for step 2 and 3, should they be in the same loop? or two different loops?
what would you suggest? thanks.
You only need to ask for one number, so asking for a number should not be in a loop.

After that, you only need a loop to look through the array and see if you can find the number.

Hope this helps.
Topic archived. No new replies allowed.