code not executing correctly while in a void function

Hello. I am fairly new to C++ so i apologize in advance if this is a dumb question.

I am trying to write a program that will generate a random number, check to see if that number is located within an array, and if not to place it in the array. I have managed to get it to work when the checking code is located in the main function but not when it is enclosed in it's own void function. More detailed explanation of problem after code segments.

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

using namespace std;

int hand [45];
int c, x, y, d, a, b, f;

int main()
{
    srand((unsigned)time(0));    
    for (int a=0; a<45; a++)
    {
        c = (rand()%108)+1;
        for (int f=0; f<45; f++)
        {
         if (hand [f] == c)
            cout << "duplicate" << endl;
         else if (hand [f] == 0)
             hand [a] = c;
         }
        cout << hand [a] << endl;
    }
    cout << "number ";
    cin >> x;
    cout << hand [x-1] << endl;
    return 0;
}



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
void check ()
{
     for (int f=0; f<45; f++)
     {
         if (hand [f] == c)
            cout << "duplicate" << endl;
         else if (hand [f] == 0)
             hand [a] = c;
     }
}

int main()
{
    srand((unsigned)time(0));    
    for (int a=0; a<45; a++)
    {
        c = (rand()%108)+1;
        check ();
        cout << hand [a] << endl;
    }
    cout << c;
    cout << "number ";
    cin >> x;
    cout << hand [x-1] << endl;
    return 0;
}


As I said the code is supposed to generate a random number, check it against an array, and if it is not present place it in the array and print it on the screen. If there is a duplicate entry it is supposed to print duplicate on the screen and start over until it generates 45 unique random numbers. After which it prompts you to chose a position within the array and display the number.

The first code works the way it is supposed to. However the project i am working on requires that i use my "check" function multiple times and in the interest of reducing code length to save space i would like to be able to run it from it's own separate function that can be called. Hence the Void check function in the second code segment.

When i compile and run the second code it prints out one random number followed by nothing but zero's and an occasional "duplicate" line. However when i enter address 1 at the end of the program it displays the last random number that was generated. Any address other than one retrieves a zero from the array.

I have a feeling that this is happening because I am not terminating the void check function properly. I have tried to terminate it with return and break but am still unable to make it work properly. Any assistance that anyone would be willing to give me with this problem is greatly appreciated.

EDIT: The issue with printing one random number and nothing but zero's after that is a minor issue since that is merely there for testing purposes while the project is in development. Also the output problem has been solved thanks to Albatross.

The bigger problem of the void check() function not storing data correctly within the array is still withstanding.
Last edited on
Welcome to the forum,

Return and break are against C++'s syntax rules, so you're right there. However, you're wrong about what causes the problem.

cout << hand [a] << endl; Has to be inside your check() function. I'm not 100% sure why, so I don't want to give any wrong answers, but it has to be.

-Albatross
Last edited on
Unfortunately that does not fix the problem, but in fact creates additional symptoms. I have compile and run the second segment of code with cout << hand [a] << endl; both at the end of the function and enclosed in brackets as part of the else if statement.

In both cases it generates and prints i'm guessing 45 random numbers on the screen but it prints each number 45 times, hence the reason i am guessing it's 45 numbers. On top of that it still only stores the final random number generated in slot 0 of the array and leaves the rest at their default 0 value.

Also i would like to know what you mean by return and break are against C++'s syntax rules.

I could be using them incorrectly because i have a very hard time understanding the tutorial on this site. I am not trying to insult the person that wrote it. However i feel that the person who wrote it tried to use a more complex writing style than necessary. Sentences are broken up in such a way that you almost have to read them backwards at times to understand what is being said.

IMHO a tutorial should be written the simplest and most straight forward way possible.

Sorry i got a little off topic but i felt it needed to be said.

when i enclose the cout << hand [a] << endl; and a break in the else if statement it solves the problem of printing the number 45 times.

I do appreciate the attempt to solve my problem.
Last edited on
I meant outside of the for loop that uses f. Sorry.

-Albatross
Albatross++

Void functions don't need to terminate so save your returns and breaks elsewhere.
I'd also recommend you stick to using variables that are local to main()'s scope and not global variables.
Last edited on
after reading through the functions section of the tutorial for the 4th time i finally figured out why this program was misbehaving. I completely forgot to set my variable a as a parameter to be passed to void check(). i'm not entirely sure if this is the correct way to solve the problem, but it has solved the problem. further input as to whether or not this is a good or bad idea is still appreciated as i am trying to learn C++
Last edited on
Topic archived. No new replies allowed.