For loop is not working

hello there, I need help implementing a for loop.
I am trying to generate multiple Diffrent tickets, so far it generates tickets but they are all the same !


#include <iostream>
#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */


using namespace std;

int main()
{
int min_ticket , max_ticket ;
int n1, n2, n3, n4, n5, n6;
cout << "Enter Minimum no. of ticket to be generated : ";
cin >> min_ticket;
cout << "Enter Maximum no. of ticket to be generated : ";
cin >> max_ticket;
int ticknum = min_ticket + rand() % (max_ticket - min_ticket);
cout << ticknum << endl;




//int n1, n2, n3, n4, n5, n6;


for (int c = 0; c < ticknum; c++)
{
n1 = 1 + (rand() % 47);
n2 = 1 + (rand() % 47);
n3 = 1 + (rand() % 47);
n4 = 1 + (rand() % 47);
n5 = 1 + (rand() % 47);

if (n1 != n2 && n1 != n3 && n1 != n4 && n1 != n5 &&
n2 != n3 && n2 != n4 && n2 != n5 &&
n3 != n4 && n3 != n5 && n4 != n5) {
break;
}
}

for (int i = 0; i < ticknum; i++)
{
n6 = 1 + (rand() % 27);
}
for (int i = 0; i < ticknum; i++)
{
cout << "Ticket " << n1 << " " << n2 << " " << n3 << " " << n4 << " " << n5 << " " << n6 << endl;
}

return 0;
}
Your loop is fine, your logic is off.

1st, please use code tags.


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
#include <iostream>
#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */


using namespace std;

int main()
{
int min_ticket , max_ticket ;
int n1, n2, n3, n4, n5, n6;
cout << "Enter Minimum no. of ticket to be generated : ";
cin >> min_ticket;
cout << "Enter Maximum no. of ticket to be generated : ";
cin >> max_ticket;
int ticknum = min_ticket + rand() % (max_ticket - min_ticket);
cout << ticknum << endl;




//int n1, n2, n3, n4, n5, n6;


for (int c = 0; c < ticknum; c++)
{
n1 = 1 + (rand() % 47);
n2 = 1 + (rand() % 47);
n3 = 1 + (rand() % 47);
n4 = 1 + (rand() % 47);
n5 = 1 + (rand() % 47);

if (n1 != n2 && n1 != n3 && n1 != n4 && n1 != n5 &&
n2 != n3 && n2 != n4 && n2 != n5 &&
n3 != n4 && n3 != n5 && n4 != n5) {
break;
}
}

for (int i = 0; i < ticknum; i++)
{
n6 = 1 + (rand() % 27);
}
for (int i = 0; i < ticknum; i++)
{
cout << "Ticket " << n1 << " " << n2 << " " << n3 << " " << n4 << " " << n5 << " " << n6 << endl;
}

return 0;
}


Look at line 46. You're printing it ticknum times... but the value of n1,n2,n3 doesn't change


I think what you need here, is an array.

int n1, n2, n3, n4, n5, n6; would become int n[6];

Now you can do some work. Consider:


add #include <ctime>

after using namespace std;

Before main, a prototype.
int rollrnd();


as soon as you declare int main (){
Make your first command srand(time(0)); This makes the random seed generator based on the current time.

in main you have;

1
2
3
4
for (int i=0; i<ticketsize; i++)
{
n[i] = rollrnd();
}




and your function after main ends with

1
2
return 0;
}


would be something like:

1
2
3
4
5
int rollrnd()
{
n=rand()%47+1;
return n;
}



Now you have n[0], n[1],n[2],n[3] and so on randomized.

Instead of if (n1!=n2 && n1.....)
You can now

1
2
3
4
for (int i=0 to ticketnum-1)
if (n[i]==n[i+1]) {
n[i]=rollrnd();  // re-randomize it on the spot.
)




By the way "using namespace std;" is a bad practice too...

Now you have unlocked the power of arrays, and functions... Good luck!
Last edited on
I thought the value of n1,n2,n3 changes because I put in a loop in line 25, so that statement executes according to the ticket numbers. however when it executes, it generates the same numbers.

No. Read my edit ^^^^
so how would the code look like, I am not sure how to implement your changes.
This is enough to get you started... you figure out the rest, and if you have questions, post your code (in code tags) and ask away...

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
#include <iostream>
#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */
#include <ctime>

using namespace std;

int Rollrnd();


int main()
{
    srand(time(0));
    int min_ticket , max_ticket ;
    int n[6];
    cout << "Enter Minimum no. of ticket to be generated : ";
    cin >> min_ticket;
    cout << "Enter Maximum no. of ticket to be generated : ";
    cin >> max_ticket;
    int ticknum = min_ticket + rand() % (max_ticket - min_ticket);
    cout << ticknum << endl;




//int n1, n2, n3, n4, n5, n6;


    for (int c = 0; c < ticknum; c++)
    {
        n[c]=Rollrnd();

//if (n1 != n2 && n1 != n3 && n1 != n4 && n1 != n5 &&
//n2 != n3 && n2 != n4 && n2 != n5 &&
//n3 != n4 && n3 != n5 && n4 != n5) {
//break;                 // You get to figure this out.... have fun! lol
    }
    cout << "Ticket ";
    for (int i = 0; i < ticknum; i++)
    {
        n[6] = rand() % 27+1;
    }
    for (int i = 0; i < ticknum; i++)
    {
        cout << n[i]  << " ";
    }
    cout << endl;

    return 0;
}

int Rollrnd()
{
    int number=rand()%47+1;
    return number;
}
Last edited on
alright, I appreciate it although I understand nothing. I was just gonna use int instead of array because I need to generate millions. Anyways thanks a lot.
Millions of ints, millions of int elements. It's all the same to the computer. The difference is the way you keep track of them, I'll take an array any day over individual variables.

Basically the only change I really made was I gave you a function that rolls a random number.
So you can call it over and over and not have to repeat the rand statement.
and
instead of n1,n2,n3....
You now have n[1], n[2], n[3]....
instead of separate variables, you now have your variables in a container. It's a lot easier to work with.


Last edited on
is there any way to modify the code but keep it as simple as possible, We have learned anything about arrays in my class so I would like to just use int variables

// Here is the one fact about arrays (vectors too) that will serve you the best.
// Arrays start counting at zero instead of one.
// It's name[0] , name [1], name[2] and not name[1], name[2], name[3]...
// Just remember the computer doesn't see 1 - 10, it sees 0 - 9.

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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <ctime>

using namespace std;

int Rollrnd();   // This is a prototype. It tells the compiler that somewhere
                       // later in the program, there will be a function named  Rollrnd()
                       // and it will be of integer type, and take no parameters.


int main()
{
    srand(time(0));   // This tells the compiler that every time rand() is called
                             // it is to base the seed off of the current time, thus apprearing
                            // to give you a truly random number (Even though it's really not)

    int min_ticket , max_ticket ;
  //  int n[6];           Let's delete this and move it.
    cout << "Enter Minimum no. of ticket to be generated : ";
    cin >> min_ticket;
    cout << "Enter Maximum no. of ticket to be generated : ";
    cin >> max_ticket;
    int ticknum =rand() % (max_ticket - min_ticket) +min_ticket ;
    int n[ticknum];     // down to here. What this does is it initializes n to ticketnum (not 6)
                               // ensuring the array is always to proper size.
    cout << ticknum << endl;


    for (int c = 0; c < ticknum; c++)   // This is a "for" loop it counts from 0 to ticketnum
    {
        n[c]=Rollrnd();                // This sends n [ the value of c] to the Rollrand function and receives
                                               // a random number to place into n[c]
                                               // as the value of c increases, you are randomizing your entire array.

//if (n1 != n2 && n1 != n3 && n1 != n4 && n1 != n5 &&
//n2 != n3 && n2 != n4 && n2 != n5 &&
//n3 != n4 && n3 != n5 && n4 != n5) {
//break;                                                       Look at the loop above and see if you can come up with something
                                                               // better than this. Remember... it's n[1], not n1
                                                               // think about carefully... 
                                                               // if n[0]!=n[1] && n[0]!=..... Now you aren't limited to 6 numbers.
                                                               // n goes from 0 to ticknum I bet you could make a loop here to replace that
                                                              //  nugget of awesome you have there...
    }
    cout << "Ticket ";                              // Prints the word ticket.
    for (int i = 0; i < ticknum; i++)           // a for loop that counts from 0 to ticketnum
    {
        n[6] = rand() % 27+1;                   // No clue what you were doing here.
    }
    for (int i = 0; i < ticknum; i++)       // again -  a for loop that counts from 0 to ticketnum
    {
        cout << n[i]  << " ";                  // print n[i] + " "   as i increases it prints your entire array
    }
    cout << endl;                                     // prints a blank line

    return 0;                                       // return value for the main function
}                                                       // closes the main

int Rollrnd()                                    // Your function. Called int Rollrnd
                                                       // It is a function of integer type that takes no parameters
{
    int number=rand()%47+1;          // This assigns the integer variable "number" a value from 1 to 47
    return number;                          // this returns the number to line 33 where it was called, and
                                                     // assigns that to n[i];
}


// I hope this helps you understand.
Last edited on
closed account (48T7M4Gy)
Double posting is not good.
http://www.cplusplus.com/forum/beginner/177570/
I apologize for the double posting, I thought that I used the wrong forum and that's why nobody has answered me so I posted in the beginners forum. I am new here and this is my first question. I apologize and I appreciate all of your help thanks.
closed account (48T7M4Gy)
Don't let it happen again please.

Another don't is don't just copy what I have here. It is just to show you where/how you can overcome the problem of just producing the same ticket repeatedly. You'll still have to put in all the other checking for duplicates stuff you had.

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

using namespace std;

int main()
{
	int noOfTickets = 0;
	int n1, n2, n3, n4, n5, n6;

	cout << "How many tickets do you want?: ";
	cin >> noOfTickets;

	for (int i = 0; i < noOfTickets; i++)
	{

		n1 = 1 + (rand() % 47);
		n2 = 1 + (rand() % 47);
		n3 = 1 + (rand() % 47);
		n4 = 1 + (rand() % 47);
		n5 = 1 + (rand() % 47);
		n6 = 1 + (rand() % 47);

		cout << "Ticket #" << i << ' ' << n1 << " " << n2 << " " << n3 << " " << n4 << " " << n5 << " " << n6 << endl;
	}
	return 0;
}


How many tickets do you want?: 6
Ticket #0 39 5 19 24 18 27
Ticket #1 17 4 29 29 11 18
Ticket #2 45 29 3 21 23 22
Ticket #3 19 37 35 38 19 39
Ticket #4 21 2 32 18 21 7
Ticket #5 28 38 12 25 40 8
 
Exit code: 0 (normal program termination)
Last edited on
I did put the checks in an if statement, and I ran the program and it seems to work fine because the numbers don't repeat.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
	n1 = 1 + (rand() % 47);
		n2 = 1 + (rand() % 47);
		n3 = 1 + (rand() % 47);
		n4 = 1 + (rand() % 47);
		n5 = 1 + (rand() % 47);
		n6 = 1 + (rand() % 27);
		if (n1 != n2 && n1 != n3 && n1 != n4 && n1 != n5 &&
			n2 != n3 && n2 != n4 && n2 != n5 &&
			n3 != n4 && n3 != n5 && n4 != n5)
		{

			cout << "Ticket #" << i << ' ' << n1 << " " << n2 << " " << n3 << " " << n4 << " " << n5 << " " << n6 << endl;
		}
	}


I am not sure though if my if statement is working?
closed account (48T7M4Gy)
I am not sure though if my if statement is working?

You can test it by deliberately making a couple of duplicates and printing out a message "Duplicate found" but you'll need an else statement along the lines of:

1
2
3
4
if( all those tests you've got)
 cout ticket blah blah
else
 cout duplicate blah blah 

Thanks a lot I did test that if statement with an else and it seems to be working. so I now I should just omit that else part because I only want it to cout if those conditions are met, right?
closed account (48T7M4Gy)
right?


Personally, I'd leave it in but there are no right's or wrong's. Of course the end user buying a ticket doesn't need the message but maybe your teacher would see it as a good move, who knows?
alright, thanks.
Topic archived. No new replies allowed.