View Single Post
Old 02-09-2007, 07:43 PM   #13 (permalink)
utsav
damn busy...
 
utsav's Avatar
 
Join Date: Sep 2006
Location: Jhansi/Meerut
Posts: 1,991
Default Re: Post ur C/C++ Programs Here

this is a program in which u will hav 2 enter any 5 nos between 1 to 49 and if the number matches with the numbers chosen by the computer, u will win the lottery




#include <cstdlib>
#include <iostream>
using namespace std;
void lotto();
void calc(int * a, int * b);
void menu();
// Main function. Seeds random function
// and calls menu function.
int main()


{
srand((unsigned)time(0)); // Seed the random function......
menu();
}
// This function prompts the user to pic
// k five numbers between 1 and 49.
// The function makes sure that the user
// picks numbers between 1 and 49,
// and that the user does not choose dup
// licate numbers. Then the computer
// chooses five random numbers. Error co
// ntrol is used to check if the
// computer chooses the same number twic
// e. Then the numbers are displayed
// to the screen. Finally the calc funct
// ion is called to calculate and
// output the results of the game...
void lotto()


{
int i, k, j, b, x, y;// variables for all the for loops....
int s, t, user_error;
int user[5], comp[5];// arrays for user and computers numbers....
int user_error_check, comp_error_check; // to find if numbers are duplicates....
// Prompt user to choose numbers
cout << "Please choose five numbers from 0 to 49" << endl;
for (i=0; i<5; i++)


{
cout << "Enter number " << i + 1 << ":" ;
cin >> user[i];
if (user[i] > 49 || user[i] < 0)


{
cout << "You did not enter a number from 0 to 49" << endl;
i--;
}

}
// ERROR CONTROL, to make sure that the
// user does not choose the
// same number twice.................
for(s=0; s<5; s++)
for(t=0; t<s; t++)


{
user_error_check = user[s];
if(user_error_check == user[t])


{
cout << "You have chosen duplicate numbers, please choose another number: " << endl;
cin >> user_error;
user[s] = user_error;
}
}

// Now the computer chooses numbers.....
// ........
for (k=0; k<5; k++)


{
comp[k] = (rand( ) % 49) + 1; // get comp to randomly generate 1 -49
}
// ERROR CONTROL, to make sure that the
// computer does not pick
// the same number twice.......

for(x=0; x<5; x++)
for(y=0; y<x; y++)


{
comp_error_check = comp[x];
if (comp_error_check == comp[y])


{
comp[x] = (rand() % 49) + 1;
}
} // no guarentee that the computer will not choose another number
// already in the array, but it's the be
// st i can do?????

cout << "You chose: " ;// output users numbers...
for (j=0; j<5; j++)


{
cout << user[j] << ", " ;
}

cout << endl;

cout << "The computer chose: "; // output computers numbers...
for (b=0; b<5; b++)


{
cout << comp[b] << ", " ;
}
cout << endl << endl;

calc(user, comp);// Obtain the results of the game......
}
// This function compares the computers
// random numbers to the numbers
// chosen by the user and lets the user
// know how many (if any) they
// have matched..............
void calc(int * a, int * b)


{
int i, k, match =0;
for (i=0; i<5; i++)
for (k=0; k<5; k++)
if ( a[i] == b[k] )


{
match += 1;
cout << "You have matched: " << a[i] << endl;
}
if (match == 0)


{
cout << "Sorry you have not matched any of the computers numbers..."
<< endl << endl;
}
if (match == 5)


{
cout << "CONGRATULATIONS, YOU HAVE WON THE LOTTERY!!!!!!!" << endl << endl;
}
}
// This function informs the user how to
// play the game and then allows
// the user to play the game until they
// enter 'q' or 'Q' at the prompt.
void menu()


{
char choice;
cout << endl;
cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$" << endl;
cout << "$(C)$" << endl;
cout << "$ $" << endl;
cout << "$ *The computer will randomly choose five numbers $" << endl;
cout << "$ between 1 and 49.$" << endl;
cout << "$ $" << endl;
cout << "$ *You can choose five numbers and try to match$" << endl;
cout << "$ the computers numbers...... $" << endl;
cout << "$ $" << endl;
cout << "$ **Do not pick the same number twice or pick a$" << endl;
cout << "$number less than 1 or greater than 49, o.k.?$" << endl;
cout << "$ $" << endl;
cout << "$ $" << endl;
cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$" << endl;
cout << endl;
while (choice != 'q' || choice != 'Q')


{

cout << "Make a selection from the menu: " << endl;
cout << "[P]- Play the clotto against the computer." << endl;
cout << "[Q]- Exit this program." << endl;
cin >> choice;
switch(choice)


{
case 'p':
case 'P':
lotto();
break;
case 'q':
case 'Q':
cout << "No lotto for you!" << endl;
exit(0);
break;
}
}
}
__________________
MSI GX660 with ATI 5870 :grin: ultimate gaming lappy :grin:
Dell Studio 15(1555)
1TB+1.5TB external|N86|ZTE Blade|5230|E63|EP-630|Soundmagic PL50|Sennheiser CXL 400|Meelec M11P+
www.techjunkiez.com
utsav is offline