Guess a randomized number from an array

Hi, I'm kind of new to the c++ language (and any other language aswell for that matter) and I'm currently working on a program that will randomize 20 numbers, from 1-100 and save them into an array. You are then to guess a number and if the number you guess is one of the randomized numbers you get a message telling you that you were correct, else it would tell you the guessed number is not correct.
I'm having trouble getting it to work, even though I enter the right answer, it's telling me that it's wrong. Help is appreciated!
If possible aswell, I would love for someone to explain how I could add a feature that would ask the user if he/she wanted to guess again (Do you want to guess again y/n).

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
#include <iostream>
#include <conio.h>

using namespace std;

main()
{
      srand(time(NULL));
      int i,array[19],answer;
       
      for (i=0;i<20;i++)
      {
          array[i] = (rand()%100)+1;
          cout<<array[i]<<endl;  // I just used this so that I could see all the randomized numbers so I could be certain that I entered one of the randomized numbers when I were to test the script.
                    }
          cout<<"Guess the number:"<<endl;
          cin>>answer;
          if (answer==array[i])
          {
                          cout<<"You were right "<<answer<<" is one of the numbers!"<<endl;
                          }
                          else 
                          cout<<"Unforunately "<<answer<<" wasn't one of the numbers"<<endl;
          
          
          
          
          
          getch();
          }
bump
First of all: be careful with your array initialization. Your for loop goes from 0 to 19, which are 20 items! In an array, the last element is always at (size-1).

Then, take a good look at main(). It's a function, and every function must have a return type! In case of main, the standard dictates that main must return an int. Make it int main() and have it return 0 at the end.

Now, I think you're making a mistake by not realizing that at the point of your question, 'i' is 20. 'i' is defined outside the scope of the for, which means the final value is remembered. In this case, the for loop stops when i = 20 (the first value where i is no longer < 20).

The odd thing is that your code should have crashed long ago. Firstly, your last random value is written to array[19], which doesn't exist since your array is only [0,18] (size 19). Then, in your if, it should crash because it is trying to access array[20], which also doesn't exist.

Before you work on your "guess again" problem, you should fix these things, because they are very, very bad. So bad, in fact, that you should have seen some kind of runtime error... Any chance you're using Dev-C++?
yes I use dev-c++.. :P
If you insist on doing that, use the version with a compiler that isn't a decade old.

http://orwellengine.blogspot.com/

As for writing and reading just beyond the size of an array; that will only cause a segFault if the array is right at the edge of the memory that the OS has given to the program. If it isn't, it's quite possible to read and write beyond the end of the array but not actually cause a segFault, as the memory you're reading/writing is yours. You're trashing your own data, or trashing your own empty space. That's not to say that this is a good thing; it isn't. It's bad. Very bad. There are numerous tools that can help you find these bugs in your code.
Last edited on
Topic archived. No new replies allowed.