Suppose you want to develop a program to play lottery. The program randomly generates a Lottery of a two-digit number, prompts the user to enter a two-digit number, and determines whether the user wins according to the following rule:
If the user matches the lottery in exact order , the awards is $10,000.
If the user input matches the lottery, the awards is $3,000.
If one digit in the user input matches a digit in the lottery, the awards is $1,000.
- I'm having trouble all the numbers that i input it prints out "You matched both numbers. You win $3,000", please help.

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

int main()
{
	int guessNum1,guessNum2,lotteryNum1,lotteryNum2;
	double lotramd,userg;
	char ans;
	srand(time(NULL));
	do{ 
		lotramd= rand()%(100)+1;
		printf("Dehkhoda Jackpot Lottery\n");
        do{
			printf("Enter your lottery numbers:"); 
			scanf("%lf",&userg);
			
			if (userg == lotramd)
				printf("Exact match! You win $10,000\n");
			else if (guessNum2 == lotteryNum1 && guessNum1 == lotteryNum2)
				printf("You matched both numbers. You win $3,000\n");
			else if (guessNum1 == lotteryNum1
					 || guessNum1 == lotteryNum2
					 || guessNum2 == lotteryNum1
					 || guessNum2 == lotteryNum2)
				printf("You matched one digit! You win $1,000\n");
			else
				printf("Sorry, you did not get any matches!");		
		}
		while(userg != lotramd);
		
		printf("Play Again? (y/n)");
	
		scanf(" %c",&ans);
	}while(ans=='y');
	
	return 0;
}

You can break a number down with division and remainder:

123 % 10 = 3
123 / 10 = 12

These two operations will allow you to test both digits.

You can break a number down with division and remainder:

123 % 10 = 3
123 / 10 = 12

These two operations will allow you to test both digits.

Can you clarify this using my code? please

Can you clarify this using my code?

Translation: "Will you do it for me?"

No. Try it yourself and ask a specific question if you have issues.

In response to post that started this thread, I took your code down and compiled it in VC++ 2008 express Edition. Your syntax for the random number generation may be just print zero in the end.
:)
Look into doing this

e.g. lotramd=1+(rand()%100);

&&(not)

> lotramd=rand()%(100)+1;
Look into changing that entire randomization section.

But what may be of use to you is to read the solution as a string use scanf(%s,var_), since it is one number and you aren't reading space. Then just convert each character its respective integer representation and then do your comparisons from there.

I wrote a simple program that does what I said

//Title: Comparing integer positions in a number
//Programmer: Matthew Fraser


#include <stdio.h>
#include <string.h>
#include <ctype.h>


int main()
{
	char guess[20];
	char actual[20];
	int num1, num2, number; 
	 
	number=12;//Just for argument sake

	itoa(number,actual,10);//Converting number to a string...You can look up syntax
	
	printf("Enter a number please\n");
	scanf("%s", guess);

	if(strcmp(actual,guess)==0)//Comparing if the strings are equal
	{
		printf("Direct match\n\n");
	}
	else
	{
		if(guess[0]==actual[0])//Comparing if the first digits are equal
		{
			printf("First digit matches");
		}
	}
	getch();
	return 0;
}

Um..I'm new to posting sorry if coding is inadequate but how does one make pasted code keep its formatting..Hope I'm not breaking any rules by asking this.. :scared:

Edited 5 Years Ago by WaltP: Added CODE Tags -- please use them.

Attachments
//Title: Comparing integer positions in a number
//Programmer: Matthew Fraser

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main()
{
	char guess[20];
	char actual[20];
	int num1, num2, number; 
	 
	number=12;//Just for argument sake

	itoa(number,actual,10);//Converting number to a string...You can look up syntax
	
	printf("Enter a number please\n");
	scanf("%s", guess);

	if(strcmp(actual,guess)==0)//Comparing if the strings are equal
	{
		printf("Direct match\n\n");
	}
	else
	{
		if(guess[0]==actual[0])//Comparing if the first digits are equal
		{
			printf("First digit matches");
		}
	}
	getch();
	return 0;
}

By reading the words on the background of the box you typed your post in. And all over the place CODE Tags are explained.

Look into doing this

e.g. lotramd=1+(rand()%100);

&&(not)

lotramd=rand()%(100)+1;

You realize that both of these statements are functionally identical, right?

>itoa(number,actual,10);
It's not a problem per se, but itoa is not a standard function. The OP may not be able to use it. A portable, if heavy, alternative is sprintf.

>scanf("%s", guess);
Awesome. Now what happens if I type in a 30 digit number? You neglected to do two things, one that is specifically for string input and the other a general good idea with scanf:

if (scanf("%19s", guess) != 1) {
    /* Failed to read a string */
}

It's always a good idea to check input functions for success, and when reading strings a field width protects you from buffer overflow. Without it, scanf is no better than gets in terms of being completely unsafe.

Edited 5 Years Ago by Narue: n/a

Thanks for the insight..I was just providing a simple example of logic. I shall however change my programming outlook, Cheers :$ Stay humble :P

This article has been dead for over six months. Start a new discussion instead.