does anybody some code for this

C

carl bloc

Have this much done but need help with this bit of code:

allow user to modify existing draw data. I think I need a counter to
give week numbers so the user can select a week number rather than a
date.

I need to view data for a selected week given the week number or date.

need some controls on not allowing the ball number to go >49 or <0 and
no duplicates.

the date needs some work on so the user can only enter numbers not
letters.

any help would be great.


/**********************************************************************************************
*
* Carl's Assignment
*
*
*
* Here is a list of tasks to consider
* ___________________________________
*
* * Change the Display Routine to display the complete list of
* 52 Records simultaniously.
* You'll need to alter the count
*
* * Alter the Comments, and add extra ones
*
* * Write a Routine to edit an existing Record
*
* * Write a Routine that checks that the Ball Values entered are
within Range ( 1 - 49 )
*
* * Write a Routine that checks that no duplicate Ball Values are
entered
* (run this check after every entry)
*
* * THE BIG ONE - Write a Routine that checks the Date is valid
*
* * Think about what will happen when the Array is full & you put in
an additional Entry
*
*
***********************************************************************************************/

#include <iostream.h>
#include <math.h>
#include <process.h>
#include <fstream.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>




// Function prototypes
void GetInput();
void DisplayValues();

void ChoiceDefault();
void AnyKey();

//Global variables (declared outside main() - so visable (i.e.
accessable) to whole program functions and all!
char datearray[53][10]; // 2-Dimensional array 52 rows, 9 columns.
int balls[53][36];

void main()
{

char date[10];
int choice=0, x=0, y=0;
int insertionPoint=0;




int i =0, j = 0, k = 0;


//initialise global 'datearray' as a string of asterixes and 'balls'
array with 0's
for(j=0; j<53; j++)
{
for(i=0; i<8; i++)
{
if (i == 8)
{
datearray[j] = '\0';//terminate string with null character
}
else
{
datearray[j] = '*';// initialise datarray with asterix
characters
}
for (int k=0;k<8;k++)
balls[k] = 0;

}
}
// open Date.txt file to get data from
ifstream inClientFile("Date.txt", ios::in); // open file for input
if(!inClientFile) // handle "could not open file" error
{
cerr << "File could not be opened" << endl;
exit(1);
}

// read data (date & two numbers) from Date.txt file
i=0; // must reset "i" to zero
while (inClientFile >> date)// read (next) "line" from file

{
for (k=0;k<7;k++)
inClientFile>>balls[k];
for(j=0; j<9; j++)
{
datearray[j] = date[j]; // copy date from 'date' tempory
variable into datearray
}

i++;
}
insertionPoint = i;//remember where to insert next new data


while (true)
{
// Clear screen
system("cls");

//Print Menu choices on screen
cout << "\n\n\t\tMENU:\n\n"<< "\t1 - Enter New Date\n"<< "\t2 - Print
out contents of global array on screen\n" << "\t999 - to exit";

//Get user's choice
cout << "\n\nEnter your choice : ";
cin >> choice;

//if 999 entered the jump out of while loop to finish
if (choice == 999) break;

//act on users menu choice
switch (choice)
{

case 1:
GetInput();
break;

case 2:
DisplayValues();
break;

default:
ChoiceDefault();
break;
}
}
system("cls");



/*
This section was of no use, as it was just repeating the display
before exiting the program


//Write contents of global array to screen
// output contents of date array to file
for(j=0; j<10; j++)
{
for(k=0; k<9; k++)
{
cout << datearray[j][k];
}
cout << "\t" << balls[j][0] << "\t" << balls[j][2];
cout << "\n";
}
*/



//Write data from arrays out to Lottery file
ofstream outClientFile("Date.txt", ios::eek:ut); // open file for output
if(!outClientFile) // handle error if file cannot be opened
{
cerr << "File could not be opened" << endl;
exit(1);
}

// exiting program, so output contents of datearray and balls to
Date.txt file
for(i=0; i<52; i++)
{
outClientFile << datearray;
for (j=0;j<7;j++)
outClientFile << " " << balls[j];
outClientFile << endl;
}
}


// Function Definitions
void GetInput()
{
int inspoint;
int ball1int = 0;
int ball2int = 0;
int ball3int = 0;
int ball4int = 0;
int ball5int = 0;
int ball6int = 0;
int ball7int = 0;
int j;
char date[10];

system("cls");
cout << "\nEnter new date (dd/mm/yy) :";
cin >> date;
cout << "\nEnter First Lottery Number :";
cin >> ball1int;
cout << "\nEnter Second Lottery Number :";
cin >> ball2int;
cout << "\nEnter Third Lottery Number :";
cin >> ball3int;
cout << "\nEnter Fouth Lottery Number :";
cin >> ball4int;
cout << "\nEnter Fifth Lottery Number :";
cin >> ball5int;
cout << "\nEnter Sixth Lottery Number :";
cin >> ball6int;
cout << "\nEnter Bouns Ball :";
cin >> ball7int;




// Find insertion point
for(j=0; j<10; j++)
{
if (datearray[j][0] == '*')
{
inspoint = j;
cout << "inspoint = " << inspoint << endl;
break;
}
}
//Insert new date into datearray and new numbers into balls, global
arrays.
for(j=0; j<10; j++)
{
datearray[inspoint][j] = date[j];
}
balls[inspoint][0] = ball1int;
balls[inspoint][1] = ball2int;
balls[inspoint][2] = ball3int;
balls[inspoint][3] = ball4int;
balls[inspoint][4] = ball5int;
balls[inspoint][5] = ball6int;
balls[inspoint][6] = ball7int;

}

void DisplayValues()
{
int j, k;
//Write contents of global arrays (datearray and balls) to screen
for(j=0; j<10; j++)
{
for(k=0; k<10; k++)
{
cout << datearray[j][k];
}
for(k=0; k<7; k++)
{
cout << "\t" << balls[j][k];
}
cout << "\n";
}
}


void ChoiceDefault()
{
system("cls");
cout <<"\n\n\n\n\tYou entered a number not on menu Menu ";
cout << "\n\n\n\nPress return for Main Menu" << endl;
getchar();
}


void AnyKey()
{
char ch;
cout << "\n\nPress any Key to Continue" << endl;
ch=getch();
cout << endl;
}
 
K

Karl Heinz Buchegger

carl said:
Have this much done but need help with this bit of code:

Please be more specific. What help? What are your problems in specific?
allow user to modify existing draw data. I think I need a counter to
give week numbers so the user can select a week number rather than a
date.

Well. If you currently are unable to calculate the week number from a given
date then yes, storing a week number would solve your problem. But calculating
it from the date is more elegant. How to calculate it? Take paper, pencil
and try to figure out: Which week of the year holds the 25. Mai 2004.
If youcan answer that with only paper and pencil (and the knowledge
of eg. that the 1. January 1980 was a Tusday) you can start writing
a program which does the same thing as you did on paper. Hint: you could
eg count how many days have elapses since 1. January 1980, divide that
number by 7, ...

I need to view data for a selected week given the week number or date.

Should not be a big problem, once you have the number of the week.
need some controls on not allowing the ball number to go >49 or <0 and
no duplicates.

Where is the problem?

if( ball1int < 0 || ball1int > 49 )
cout << "This is an invalid input. 0 to 49 only\n";

if( ball1int == ball2int ||
ball1int == ball3int ||
ball1int == ball4int ||
ball1int == ball5int ||
ball1int == ball6int ||
ball1int == ball7int )
cout << "You entered a duplicate value for ball 1\n";

You probably will want to enclose the whole ball reading sequence
into a loop, which repeats until a valid set of ball numbers is entered.


bool Valid;

do {
Valid = true; // assume the input will be valid.

// ask user for numbers

// check numbers and set flag 'Valid' to false if the assumption
// of valid input doesn't hold
if( ball1int < 0 || ball1int > 49 ) {
cout << "This is an invalid input for ball 1. 0 to 49 only\n";
Valid = false;
}

....

} until( Valid );

You might also want to enhance this to give the user a chance to cancel
the whole process. But this is up to you.
You might also want to divide the whole thing into functions to make
it better readable. But the principle stays the same: Use a loop
to let the user repeat the input until the input is valid.
the date needs some work on so the user can only enter numbers not
letters.

Turn that around:
Let the user enter characters (eg. as a string) and figure out if this
string can be converted into a number.
The point is: You can't hinder the user to enter letters. Your program
gets control on what has entered only after the user has entered it. But
of course you can react gracefully to it.
There are 2 strategies in C++ to deal with that:
either let the stream operations do the detection and clear up the stream
error state and discard all pending characters or read as string and convert
on your own.
See this newsgroup and search for posts entitled: 'how to convert from string
to number' or something similar. This topic comes up at least 4 times a week.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top