473,796 Members | 2,585 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C problem, likely simple but I can't see it!

sam
hi, i am new to C/C++ and have just finished a program based on the UK
national lottery, where you enter 6 numbers, six are generated by the
computer and there are appropriate messages and a compare function to decide
your winnings. I am using Microsoft's C++ version 6 standard, the program
is as follows:

/* * * * * * * * * * * * * * * * * * * *
* *
* Lottery Simulation By Sam Halston *
* *
* * * * * * * * * * * * * * * * * * * * */


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define NMAX 6
int a[NMAX];
int b[NMAX];
void getIntArray(int x[]);
void bubbleSort(int x[]);
void printArrays(int x[], int y[]);
void getrandomarray( int x[]);
void winnings();
int wins = 0;

int main(void) {
//welcome
printf("\t Welcome To The National Lottery!\n");
printf("\tPleas e Enter Six Whole Numbers Between 1 And 49\n\n");

getIntArray(a);
getrandomarray( b);
bubbleSort(a);
bubbleSort(b);
printf("Your Numbers In Numerical Order Are: \n");
printArrays(a, b);
winnings();
return 0;
}


void getIntArray(int x[])

{
int n = 0;
int temp;

here://I know goto loops are not liked in C, but this is the best way to
achieve this feature in my opinion!
;

do {
printf("Choose A Number\n");
scanf("%d", &temp);
if (temp==0) break;
if (n==5)
{
printf("All Six Numbers Have Been Entered, Press Zero To Confirm\n");

}

if ((temp<01) || (temp>49))
{
printf("%s\n", "Sorry, That Is An Unacceptable Number. Please Re Enter");
goto here;
}

if (temp==a[0])
{
printf("%s\n", "Sorry, You Have Already Selected That Number. Please Make
Another Choice");
goto here;
}
if (temp==a[1])
{
printf("%s\n", "Sorry, You Have Already Selected That Number. Please Make
Another Choice");
goto here;
}
if (temp==a[2])
{
printf("%s\n", "Sorry, You Have Already Selected That Number. Please Make
Another Choice");
goto here;
}
if (temp==a[3])
{
printf("%s\n", "Sorry, You Have Already Selected That Number. Please Make
Another Choice");
goto here;
}
if (temp==a[4])
{
printf("%s\n", "Sorry, You Have Already Selected That Number. Please Make
Another Choice");
goto here;
}
if (temp==a[5])
{
printf("%s\n", "Sorry, You Have Already Selected That Number. Please Make
Another Choice");
goto here;
}

else
a[n++] = temp;
}while (1);
}
void bubbleSort(int x[])
/* It sorts in non-decreasing order the six positions of a. It uses
* a technique called the bubble sort method.
*/
{
int lcv;
int limit = NMAX-1;
int temp;
int lastChange;

while (limit) {
lastChange = 0;
for (lcv=0;lcv<limi t;lcv++)

if (x[lcv]>x[lcv+1]) {
temp = x[lcv];
x[lcv] = x[lcv+1];
x[lcv+1] = temp;
lastChange = lcv;
}
limit = lastChange;
}
}
void printArrays(int x[], int y[])

/* These values are printed out, six per line. */
{
//print user array
for (int i=0; i<NMAX;)
{
printf("\t%d ", x[i++]);
if (i%6==0)
printf("\n");
}
printf("\n");

printf("The Drawn Numbers In Numerical Order Are: \n");
//print random array

for (i=0; i<NMAX;){
printf("\t%d ", y[i++]);
if (i%6==0)
printf("\n");
}
printf("\n");

}

void getrandomarray( int x[])
{

//make random numbers
srand((unsigned int)time((time_ t *)NULL));

for (int i=0; i<NMAX; i++)
{
x[i] = (rand()%49)+1; //This safety feature makes the
generator regenerate should any
for(int j=0; j<i; j++) //of its numbers be the same as previous
numbers.
{
if(x[i] == x[j])
i--;
}
}

}
void bubblesort(int x[])
/* It sorts in non-decreasing order the first 6 positions of b. It uses
* the bubble sort method.
*/
{
int lcv;
int limit = NMAX-1;
int temp;
int lastChange;

while (limit)
{
lastChange = 0;
for (lcv=0;lcv<limi t;lcv++)
{
/* Notice that the values in positions LIMIT+1 .. in
* their final position, i.e. they are sorted right */
if (a[lcv]>a[lcv+1])
{
temp = x[lcv];
x[lcv] = x[lcv+1];
x[lcv+1] = temp;
lastChange = lcv;
}
limit = lastChange;
}

}
}
void winnings()
{

for(int i=0; i<6; i++)
{
for(int j=0; j<6; j++)
{
if(a[i]==b[j])
{
wins++;
}
}

if(wins==0)
{
printf("You Haven't Matched Any Numbers, Sorry\n");
return;
}
if(wins==1)
{
printf("You Have Matched One Number, Sorry\n");
return;
}

}
if(wins==2)
{
printf("You Have Matched Two Numbers, Sorry\n");
return;
}
if(wins==3)
{
printf("You Have Matched 3 Numbers. Well Done, You Have Won £10\n");
return;
}
if(wins==4)
{
printf("You Have Matched 4 Numbers. Well Done, You Have Won £500\n");
return;
}
if(wins==5)
{
printf("You Have Matched 5 Numbers. Well Done, You Have Won £300,000!\n");
return;
}
if(wins==6)
{
printf("You Have Matched 3 Numbers. Congratulations , You Have Won
£5,000,000!!\n" );
return;
}

}
ok-so i guess its a mess to most of you, it compiles and runs no bother,
just doesnt give a correct readout at the end of how many numbers have
matched! iv looed at this for 4 days now, seems to me that only one or no
numbers are being matched almost as if its doing a true and false as to
whether any numbers match. trouble is, the lottery rules are 0, 1 or 2
numbers matched = £0, 3 =£10, 4=£500, 5=£300,000, 6= £5,000,000. any quick
fixes or suggestions as to an alternative end will be greatly appreciated,
but if i could stick to this structure it would be good as i am trying hard
to understand even what i've written! did anyone find this or am i just
doomed to not be a programmer!?

thanks in advance

sam
Jul 22 '05 #1
10 1413

"sam" <sam@home> wrote in message
news:q4******** ************@br ightview.com...
hi, i am new to C/C++ and have just finished a program based on the UK
national lottery, where you enter 6 numbers, six are generated by the
computer and there are appropriate messages and a compare function to decide your winnings. I am using Microsoft's C++ version 6 standard, the program
is as follows:

/* * * * * * * * * * * * * * * * * * * *
* *
* Lottery Simulation By Sam Halston *
* *
* * * * * * * * * * * * * * * * * * * * */


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
in C++, these should be

#include <cstdio>
#include <cstdlib>
#include <ctime>

#define NMAX 6
int a[NMAX];
int b[NMAX];
void getIntArray(int x[]);
void bubbleSort(int x[]);
void printArrays(int x[], int y[]);
void getrandomarray( int x[]);
void winnings();
int wins = 0;

int main(void) {
//welcome
printf("\t Welcome To The National Lottery!\n");
printf("\tPleas e Enter Six Whole Numbers Between 1 And 49\n\n");

getIntArray(a);
getrandomarray( b);
bubbleSort(a);
bubbleSort(b);
printf("Your Numbers In Numerical Order Are: \n");
printArrays(a, b);
winnings();
return 0;
}
good short program. Often newcomers make hidiously long programs, good one!


void getIntArray(int x[])

{
int n = 0;
int temp;

here://I know goto loops are not liked in C, but this is the best way to
achieve this feature in my opinion!
;

do {
printf("Choose A Number\n");
scanf("%d", &temp);
if (temp==0) break;
if (n==5)
{
printf("All Six Numbers Have Been Entered, Press Zero To Confirm\n");

if the last number is entered invalid, then you are asking them to press
zero, which is invalid, infinite loop!

}

if ((temp<01) || (temp>49))
{
printf("%s\n", "Sorry, That Is An Unacceptable Number. Please Re Enter");
goto here;
}

if (temp==a[0])
{
printf("%s\n", "Sorry, You Have Already Selected That Number. Please Make
Another Choice");
goto here;
}
if (temp==a[1])
{
printf("%s\n", "Sorry, You Have Already Selected That Number. Please Make
Another Choice");
goto here;
}
why didnt you do:
for (int loop=0; loop<NMAX; ++loop)
if (temp==a[loop])
// ...

For two reasons. One its easier to read as its far shorter. Two, its more
maintainable. What would you do if you wanted to do a lottery with 8
numbers? Or consider 1000, what would you do then? (i know its extreme, bit
its meant to make a point)

if (temp==a[2])
{
printf("%s\n", "Sorry, You Have Already Selected That Number. Please Make
Another Choice");
goto here;
}
if (temp==a[3])
{
printf("%s\n", "Sorry, You Have Already Selected That Number. Please Make
Another Choice");
goto here;
}
if (temp==a[4])
{
printf("%s\n", "Sorry, You Have Already Selected That Number. Please Make
Another Choice");
goto here;
}
if (temp==a[5])
{
printf("%s\n", "Sorry, You Have Already Selected That Number. Please Make
Another Choice");
goto here;
}

else
a[n++] = temp;
}while (1);
}
void bubbleSort(int x[])
/* It sorts in non-decreasing order the six positions of a. It uses
* a technique called the bubble sort method.
*/
{
int lcv;
int limit = NMAX-1;
int temp;
int lastChange;

while (limit) {
lastChange = 0;
for (lcv=0;lcv<limi t;lcv++)

if (x[lcv]>x[lcv+1]) {
temp = x[lcv];
x[lcv] = x[lcv+1];
x[lcv+1] = temp;
lastChange = lcv;
}
limit = lastChange;
}
}
there are shorter ways of doing this, e.g.

for (int i=0; i<NMAX; ++i)
for (int j=i; j<NMAX; ++j)
{
// compare and swap items
}



void printArrays(int x[], int y[])

/* These values are printed out, six per line. */
{
//print user array
for (int i=0; i<NMAX;)
{
printf("\t%d ", x[i++]);
if (i%6==0)
this prints a newline after the first number only, why? Again, you should
use NMAX for maintainability
printf("\n");
}
printf("\n");

printf("The Drawn Numbers In Numerical Order Are: \n");
//print random array

for (i=0; i<NMAX;){
printf("\t%d ", y[i++]);
if (i%6==0)
printf("\n");
}
printf("\n");

}

void getrandomarray( int x[])
{

//make random numbers
srand((unsigned int)time((time_ t *)NULL));

for (int i=0; i<NMAX; i++)
{
x[i] = (rand()%49)+1; //This safety feature makes the
generator regenerate should any
the rand() function requires you to seed the random number generator, do
something like
srand(time(NULL )); // seed rand gen based on time

also, the lower bits returned by rand() are not very random, consider using
something like:
rand() / (RAND_MAX / 49);

for(int j=0; j<i; j++) //of its numbers be the same as previous
numbers.
{
if(x[i] == x[j])
i--;
}
}

}
void bubblesort(int x[])
this has already been declared!
/* It sorts in non-decreasing order the first 6 positions of b. It uses
* the bubble sort method.
*/
{
int lcv;
int limit = NMAX-1;
int temp;
int lastChange;

while (limit)
{
lastChange = 0;
for (lcv=0;lcv<limi t;lcv++)
{
/* Notice that the values in positions LIMIT+1 .. in
* their final position, i.e. they are sorted right */
if (a[lcv]>a[lcv+1])
{
temp = x[lcv];
x[lcv] = x[lcv+1];
x[lcv+1] = temp;
lastChange = lcv;
}
limit = lastChange;
}

}
}
void winnings()
{

for(int i=0; i<6; i++)
{
for(int j=0; j<6; j++)
{
if(a[i]==b[j])
{
wins++;
}
}
this will only work if the numbers are in the same index of the array, e.g.
if you have
1,2,3,4,5,6
and
2,3,4,5,6,7

then this will report no numbers the same! Even though 5 are the same

you need to use two loops to do this.

if(wins==0)
{
printf("You Haven't Matched Any Numbers, Sorry\n");
return;
}
if(wins==1)
{
printf("You Have Matched One Number, Sorry\n");
return;
}

}
if(wins==2)
{
printf("You Have Matched Two Numbers, Sorry\n");
return;
}
if(wins==3)
{
printf("You Have Matched 3 Numbers. Well Done, You Have Won £10\n");
return;
}
if(wins==4)
{
printf("You Have Matched 4 Numbers. Well Done, You Have Won £500\n");
return;
}
if(wins==5)
{
printf("You Have Matched 5 Numbers. Well Done, You Have Won £300,000!\n"); return;
}
if(wins==6)
{
printf("You Have Matched 3 Numbers. Congratulations , You Have Won
£5,000,000!!\n" );
return;
}
i would use something like:
static char[NMAX][]sWins = {"£0", "£0", "£10", "£300,000", "£5,000,000 "};
printf("You have won %s", sWins[wins]);

which I am sure you agree, is far neater

}
ok-so i guess its a mess to most of you, it compiles and runs no bother,
just doesnt give a correct readout at the end of how many numbers have
matched! iv looed at this for 4 days now, seems to me that only one or no
numbers are being matched almost as if its doing a true and false as to
whether any numbers match. trouble is, the lottery rules are 0, 1 or 2
numbers matched = £0, 3 =£10, 4=£500, 5=£300,000, 6= £5,000,000. any quick fixes or suggestions as to an alternative end will be greatly appreciated,
but if i could stick to this structure it would be good as i am trying hard to understand even what i've written! did anyone find this or am i just
doomed to not be a programmer!?

thanks in advance

sam


you have made a good attempt. The program seems well structured apart from
your 'goto'. Instead of the goto have your structure do something like:

int ValidNums=0;
while(1)
{
// get a number
// check valid, if valid then ValidNums++
if (ValidNums == NMAX-1)
break;
}

but consider a quit option to allow the user to quit gracefully.
Also, scanf is not brilliant for getting numbers, try typing a letter and
find out! In C, I used to do scanf("%s"); and then use strtol() to
check/convert it to a number. However there are better C++ solutions that
somebody may point out.

Hope this helps
Allan
Jul 22 '05 #2
sam writes:
hi, i am new to C/C++ and have just finished a program based on the UK
national lottery, where you enter 6 numbers, six are generated by the
computer and there are appropriate messages and a compare function to decide your winnings. I am using Microsoft's C++ version 6 standard, the program
is as follows:

/* * * * * * * * * * * * * * * * * * * *
* *
* Lottery Simulation By Sam Halston *
* *
* * * * * * * * * * * * * * * * * * * * */


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define NMAX 6
int a[NMAX];
int b[NMAX]; <snip>

Try to use more descriptive names than a and b. One is a client and the
other is the government, is it not? It would be easier to help if these had
some meaning.
void getIntArray(int x[]);
void bubbleSort(int x[]);
void printArrays(int x[], int y[]);
void getrandomarray( int x[]);
void winnings();
int wins = 0;

int main(void) {
// welcome
printf("\t Welcome To The National Lottery!\n");
printf("\tPleas e Enter Six Whole Numbers Between 1 And 49\n\n");

getIntArray(a);
getrandomarray( b);
bubbleSort(a);
bubbleSort(b);
printf("Your Numbers In Numerical Order Are: \n");
printArrays(a, b);


Are you happy with the results of that print? Knowing the answer to that
divides the problem into two major "hunks".

I scanned around but didn't spot anything noteworthy. I suppose there may
well be an answer by the time I post this.

<snip>
Jul 22 '05 #3
I've added som comments to your code, hope that helps.

/Andreas
"sam" <sam@home> skrev i meddelandet
news:q4******** ************@br ightview.com...
hi, i am new to C/C++ and have just finished a program based on the UK
national lottery, where you enter 6 numbers, six are generated by the
computer and there are appropriate messages and a compare function to decide your winnings. I am using Microsoft's C++ version 6 standard, the program
is as follows:

/* * * * * * * * * * * * * * * * * * * *
* *
* Lottery Simulation By Sam Halston *
* *
* * * * * * * * * * * * * * * * * * * * */


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define NMAX 6
int a[NMAX];
int b[NMAX];
You definately should initiate these, like
init a[NMAX] = null;
or so.
void getIntArray(int x[]);
void bubbleSort(int x[]);
void printArrays(int x[], int y[]);
void getrandomarray( int x[]);
void winnings();
int wins = 0;

int main(void) {
//welcome
printf("\t Welcome To The National Lottery!\n");
printf("\tPleas e Enter Six Whole Numbers Between 1 And 49\n\n");

getIntArray(a);
getrandomarray( b);
bubbleSort(a);
bubbleSort(b);
printf("Your Numbers In Numerical Order Are: \n");
printArrays(a, b);
winnings();
return 0;
}


void getIntArray(int x[])

{
int n = 0;
int temp;

here://I know goto loops are not liked in C, but this is the best way to
achieve this feature in my opinion!
;

do {
printf("Choose A Number\n");
scanf("%d", &temp);
if (temp==0) break;
if (n==5)
{
printf("All Six Numbers Have Been Entered, Press Zero To Confirm\n");
}

if ((temp<01) || (temp>49))
{
printf("%s\n", "Sorry, That Is An Unacceptable Number. Please Re Enter");
goto here;
}

if (temp==a[0])
{
printf("%s\n", "Sorry, You Have Already Selected That Number. Please Make
Another Choice");
goto here;
}
if (temp==a[1])
{
printf("%s\n", "Sorry, You Have Already Selected That Number. Please Make
Another Choice");
goto here;
}
if (temp==a[2])
{
printf("%s\n", "Sorry, You Have Already Selected That Number. Please Make
Another Choice");
goto here;
}
if (temp==a[3])
{
printf("%s\n", "Sorry, You Have Already Selected That Number. Please Make
Another Choice");
goto here;
}
if (temp==a[4])
{
printf("%s\n", "Sorry, You Have Already Selected That Number. Please Make
Another Choice");
goto here;
}
if (temp==a[5])
{
printf("%s\n", "Sorry, You Have Already Selected That Number. Please Make
Another Choice");
goto here;
}

else
a[n++] = temp;
}while (1);
}
void bubbleSort(int x[])
/* It sorts in non-decreasing order the six positions of a. It uses
* a technique called the bubble sort method.
*/
{
int lcv;
int limit = NMAX-1;
int temp;
int lastChange;

while (limit) {
lastChange = 0;
for (lcv=0;lcv<limi t;lcv++)

if (x[lcv]>x[lcv+1]) {
temp = x[lcv];
x[lcv] = x[lcv+1];
x[lcv+1] = temp;
lastChange = lcv;
}
limit = lastChange;
}
}
void printArrays(int x[], int y[])

/* These values are printed out, six per line. */
{
//print user array
for (int i=0; i<NMAX;)
{
printf("\t%d ", x[i++]);
if (i%6==0)
printf("\n");
}
printf("\n");

printf("The Drawn Numbers In Numerical Order Are: \n");
//print random array

for (i=0; i<NMAX;){
printf("\t%d ", y[i++]);
if (i%6==0)
printf("\n");
}
printf("\n");

}

void getrandomarray( int x[])
{

//make random numbers
srand((unsigned int)time((time_ t *)NULL));

for (int i=0; i<NMAX; i++)
{
x[i] = (rand()%49)+1; //This safety feature makes the
generator regenerate should any
for(int j=0; j<i; j++) //of its numbers be the same as previous
numbers.
{
if(x[i] == x[j])
i--;
}
}

}
void bubblesort(int x[])
/* It sorts in non-decreasing order the first 6 positions of b. It uses
* the bubble sort method.
*/
{
int lcv;
int limit = NMAX-1;
int temp;
int lastChange;

while (limit)
{
lastChange = 0;
for (lcv=0;lcv<limi t;lcv++)
{
/* Notice that the values in positions LIMIT+1 .. in
* their final position, i.e. they are sorted right */
if (a[lcv]>a[lcv+1])
{
temp = x[lcv];
x[lcv] = x[lcv+1];
x[lcv+1] = temp;
lastChange = lcv;
}
limit = lastChange;
}

}
}
void winnings()
{

for(int i=0; i<6; i++)
{
for(int j=0; j<6; j++)
{
if(a[i]==b[j])
{
wins++;
}
}

You forgot to end the first for-loop! Add one '}'!
if(wins==0)
{
printf("You Haven't Matched Any Numbers, Sorry\n");
return;
}
if(wins==1)
{
printf("You Have Matched One Number, Sorry\n");
return;
}

}
The above '}' shouldn't be there!
if(wins==2)
{
printf("You Have Matched Two Numbers, Sorry\n");
return;
}
if(wins==3)
{
printf("You Have Matched 3 Numbers. Well Done, You Have Won £10\n");
return;
}
if(wins==4)
{
printf("You Have Matched 4 Numbers. Well Done, You Have Won £500\n");
return;
}
if(wins==5)
{
printf("You Have Matched 5 Numbers. Well Done, You Have Won £300,000!\n"); return;
}
if(wins==6)
{
printf("You Have Matched 3 Numbers. Congratulations , You Have Won
£5,000,000!!\n" );
return;
}

}
ok-so i guess its a mess to most of you, it compiles and runs no bother,
just doesnt give a correct readout at the end of how many numbers have
matched! iv looed at this for 4 days now, seems to me that only one or no
numbers are being matched almost as if its doing a true and false as to
whether any numbers match. trouble is, the lottery rules are 0, 1 or 2
numbers matched = £0, 3 =£10, 4=£500, 5=£300,000, 6= £5,000,000. any quick fixes or suggestions as to an alternative end will be greatly appreciated,
but if i could stick to this structure it would be good as i am trying hard to understand even what i've written! did anyone find this or am i just
doomed to not be a programmer!?

thanks in advance

sam

Jul 22 '05 #4
You definately should initiate these, like
init a[NMAX] = null;
or so.

If you mean initialize, they are by default. What is

init a[NMAX] = null;

Suppose to mean? Do you mean:

int a[NMAX] = {0};

?


Jul 22 '05 #5

"Xenos" <do**********@s pamhate.com> skrev i meddelandet
news:c7******** *@cui1.lmms.lmc o.com...
You definately should initiate these, like
init a[NMAX] = null;
or so.

If you mean initialize, they are by default. What is

init a[NMAX] = null;

Suppose to mean? Do you mean:

int a[NMAX] = {0};

?


Yes, i'm sorry. It was a long time ago since i coded C.

/Andreas
Jul 22 '05 #6
On Fri, 7 May 2004 18:55:04 +0100, "sam" <sam@home> wrote:
hi, i am new to C/C++ and have just finished a program based on the UK
national lottery, where you enter 6 numbers, six are generated by the
computer and there are appropriate messages and a compare function to decide
your winnings. I am using Microsoft's C++ version 6 standard, the program
is as follows:

"Before":
void winnings()
{

for(int i=0; i<6; i++)
{
for(int j=0; j<6; j++)
{
if(a[i]==b[j])
{
wins++;
}
}

if(wins==0)
{
printf("You Haven't Matched Any Numbers, Sorry\n");
return;
}
if(wins==1)
{
printf("You Have Matched One Number, Sorry\n");
return;
}

}
if(wins==2)
{
printf("You Have Matched Two Numbers, Sorry\n");
return;
}

...
"After":

void winnings()
{
printf("a: ");
for (int i = 0; i < 6; i++)
printf("%d ", a[i]);
printf("\nb: ");
for (int i = 0; i < 6; i++)
printf("%d ", b[i]);

for(int i=0; i<6; i++)
{
for(int j=0; j<6; j++)
{
if(a[i]==b[j])
{
wins++;
}
}

if(wins==0)
{
printf("You Haven't Matched Any Numbers, Sorry\n");
return;
}
if(wins==1)
{
printf("You Have Matched One Number, Sorry\n");
return;
}

}
if(wins==2)
{
printf("You Have Matched Two Numbers, Sorry\n");
return;
}
// ....

Notice anything different? ;-)
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #7
"sam" <sam@home> wrote in message
news:q4******** ************@br ightview.com...
hi, i am new to C/C++ and have just finished a program based on the UK
national lottery, where you enter 6 numbers, six are generated by the
computer and there are appropriate messages and a compare function to decide your winnings. I am using Microsoft's C++ version 6 standard, the program
is as follows:

<snip>

void winnings()
{

for(int i=0; i<6; i++)
{
for(int j=0; j<6; j++)
{
if(a[i]==b[j])
{
wins++;
A "break;" would work well here, as you are done processing once you find
the match, since they are sorted.

}
}

You need a } HERE, it was moved down below the following...
if(wins==1)
{
printf("You Have Matched One Number, Sorry\n");
return;
}

}
....to HERE. So fix that.

Also:
if(wins==6)
{
printf("You Have Matched 3 Numbers. Congratulations , You Have Won
£5,000,000!!\n" );
return;
}
wins == 6, but the message says 3.

any quick
fixes or suggestions as to an alternative end will be greatly appreciated,
but if i could stick to this structure it would be good as i am trying hard to understand even what i've written! did anyone find this or am i just
doomed to not be a programmer!?


Well.... the code, in general, sucks, but then we all start somewhere. ;-)

--
Mabden

Jul 22 '05 #8
sam
thanks for the help, it's looking a lot tidier now! i still cant get the
numbers to compare properly at the end though, only 0 and 1 numbers works,
as if im still doing a true or false comparison, why is it so hard to
compare numbers!? im trying to get at:

if the first number from the user inputted array equals or is equivalent to
any of the numbers from the random generator, add 1.

if the second.. etc

then we should have up to 5 (6) matches

i thought of covering all possible matches one at a time, ie if a[0] = b[0],
b[1] etc, but that would take ages and won't look so good!
Jul 22 '05 #9
sam wrote:

thanks for the help, it's looking a lot tidier now! i still cant get the
numbers to compare properly at the end though, only 0 and 1 numbers works,
as if im still doing a true or false comparison, why is it so hard to
compare numbers!?


It isn't.
Let me show you something. The beauty of consistent code indentation and how
it helps to identify some type of bugs.

Lets look at your winnings function(), but this time reformatted such
that the indentation is consistent, correct and stands out a little bit more
by using 2 spaces instead of just 1

void winnings()
{

for(int i=0; i<6; i++)
{
for(int j=0; j<6; j++)
{
if(a[i]==b[j])
{
wins++;
}
}

if(wins==0)
{
printf("You Haven't Matched Any Numbers, Sorry\n");
return;
}
if(wins==1)
{
printf("You Have Matched One Number, Sorry\n");
return;
}
}

if(wins==2)
{
printf("You Have Matched Two Numbers, Sorry\n");
return;
}
if(wins==3)
{
printf("You Have Matched 3 Numbers. Well Done, You Have Won £10\n");
return;
}
if(wins==4)
{
printf("You Have Matched 4 Numbers. Well Done, You Have Won £500\n");
return;
}
if(wins==5)
{
printf("You Have Matched 5 Numbers. Well Done, You Have Won £300,000!\n");
return;
}
if(wins==6)
{
printf("You Have Matched 3 Numbers. Congratulations , You Have Won £5,000,000!!\n" );
return;
}
}

Do you notice something?
The if-s handling the case for 0 and 1 are not at the same indentation level
then the other cases! Also the if-s for cases 0 and 1 are at the complete
wrong indentation level. You need to compare *all* numbers, not just a[0] with
all b[j] to decide if there are 0 or 1 matches.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
2665
by: cody | last post by:
I have to write an algorithm with must ensure that objects are put in buckets (which are always 4 in size). The objects have two properties: A and B. It is not allowed that in a bucket are objects with the same A or B value. But there can be more than one object with A = null or B = null in the bucket. Sometimes there is only one valid solution, sometimes there are more valid solutions, and sometimes there isn't a complete solution at...
6
1313
by: cleveralex1212 | last post by:
Sorry to disturb. I'm truly a beginner in C++ and my knowledge of it is really not much. I've been facing a serious problem during these few days. Let me describe my situation as clearly as I can and I do hope you guys can give me a helping hand. Suppose I have a number of people's personal information such as name, age, hobbies, etc. How can I store them in a way that can be easily amended by each person? How can I group the people with...
11
6640
by: Abhishek | last post by:
I have a problem transfering files using sockets from pocket pc(.net compact c#) to desktop(not using .net just mfc and sockets 2 API). The socket communication is not a issue and I am able to transfer data across.On the serve I am using Socket 2 API (recv function to read bytes)and not using ..NET. I use FileStream to open the file on the pocket pc, then associate a BinaryReader object with the stream and call ReadBytes to read all the...
8
9764
by: Sarah | last post by:
I need to access some data on a server. I can access it directly using UNC (i.e. \\ComputerName\ShareName\Path\FileName) or using a mapped network drive resource (S:\Path\FileName). Here is my problem: my vb.net program has problems with UNC. If the UNC server is restarted or goes off-line, my VB.net program crashes. The code for UNC access to the file is included below and is put in the tick event of a form timer control running every...
3
6095
by: Juan Antonio Villa | last post by:
Hello, I'm having a problem replicating a simple database using the binary log replication, here is the problem: When the master sends an update to the slave, an example update reads as follows: UPDATE MainInfo SET dAddress='38 Holland Blvd', dCity='miami', dState='FL', dZip='33000', dCountry='USA', dPhone='999987565', dNum='AC15857', dName='Michael A Scott' WHERE did=22'
5
6030
by: Harold Howe | last post by:
I am having a problem deserializing objects from a library when the following conditions exist: 1- The library is strongly named 2- The serialized file was created with version 1.0 of the assembly 3- I am trying to deserialize from an EXE that references version 2.0 of the assembly 4- Both version 1.0 and 2.0 of the assembly reside in the GAC (no policy redirects exist).
9
5761
by: HC | last post by:
Hello, all, I started out thinking my problems were elsewhere but as I have worked through this I have isolated my problem, currently, as a difference between MSDE and SQL Express 2005 (I'll just call it Express for simplicity). I have, to try to simplify things, put the exact same DB on two systems, one running MSDE and one running Express. Both have 2 Ghz processors (one Intel, one AMD), both have a decent amount of RAM (Intel system...
16
4352
by: EM.Bateman | last post by:
Working on Visual Studio .Net I've implemented a class: #ifndef CONTRIBUTOR_H #define CONTRIBUTOR_H enum Gender {male=1, female, unk}; #include <iostream> #include <iomanip> #include <string>
22
2524
by: Wilson | last post by:
i am learning to program using c++ and was set a task of making a simple encryption algorithim. I choose to start with one where simply each letter is replaced with its equivilent in the alphabet when written backmards, i am hoping to make this more complex soon. Below is the program i wrote but it does not work, it simply returns the exact same text you enter. Could you please advise on how to sort this, and also suggest any ways of...
4
3171
by: Dave Burns | last post by:
I am self hosting a Web Service in a Windows service. I am trying to start the service using the NT AUTHORITY\NetworkService account. I get a NullReferenceException on ServiceHost.Open() in the ServiceBase.OnStart() method. SCM throws a 1067 error as well. I get the same problem with NT AUTHORITY\LocalService. When I use the LocalSystem account, my domain account, or another regular user account, the windows service starts just fine.
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9525
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10452
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.