Join 131,555 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,241 people online right now. Registration is fast and FREE... Join Now!
this code is not done yet.. im having probs about the birthday.. the program asks the user his/her bday then the program will run if he/she is 18 years above before the exact date of the election which is May 2010.(in our probem) and also im having a hard time making the user pick his/her candidates and after that the program will tell that you have voted for blah blah then tell the user if he/she wants to make final corrections or finish voting..
the algo for this is: 1.ask the user's name 2.ask user's bday 3.if age<18, terminate (not allowed to vote) 4. if age>=18 continue running the program 5.The user votes from the candidates listed 6.program displays the user's candidates on w/c he/she has voted end
Well first of all... goto.. we have seen a rash of it recently. Although there is technically nothing wrong with using goto -- you will find that it complicates your code and makes it hard to maintain. Try to use control structures (while-loop, do-while loop, for-loop, if-statment, if-else, switch etc.) rather than goto.
here is an example program which may help:
cpp
#include <stdio.h> #include <time.h>
int isLeapYear(int ); int daysInMonth(int , int );
int main() { int dd,mm,yy; int age; int today_dd, today_mm, today_yy; struct tm *currentDateTime; time_t theTime;
//Find the current date time(&theTime); currentDateTime = localtime(&theTime); today_dd = currentDateTime->tm_mday; today_mm = currentDateTime->tm_mon+1; today_yy = currentDateTime->tm_year+1900; printf("Today's date: %d/%d/%d\n", today_dd, today_mm, today_yy);
do { printf("please input your birth date in the format dd/mm/yyyy : "); scanf("%d/%d/%d", &dd, &mm, &yy); age = -1; //since I don't do any of the invald checks... if (yy < today_yy && yy > 1900) { if (mm > 0 && mm <=12) { if (dd >= 1 && dd <= daysInMonth(mm, yy)) { if (today_mm < mm) { age = today_yy - yy - 1; } else if (today_dd < dd) { age = today_yy - yy - 1; } else { age = today_yy - yy; } } // else invalid day } // else invalid month } //else invalid year
if (age < 18 && age > 0) { printf("Too young to vote! \n"); } } while(age < 18); printf("date: %d/%d/%d, you age is: %d\n", dd, mm, yy, age); return 0; }
int daysInMonth(int month, int year) { // jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int retValue; if (isLeapYear(year) && month == 2) { retValue = 29; } else { retValue = days[month - 1]; } return retValue; }
int isLeapYear(int year) { //Formula via wikipedia return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0); }
printf("Input your birth date in this format mm/dd/yyyy: "); scanf("%d/%d/%d", &mm, &dd, &yy); clrscr(); yyage=EYEAR-yy; mmage=EMONTH-mm; ddage=EDAY-dd;
if((yyage<MAX) || (yyage>130)) { printf("You are not allowed to vote, you are not in the right age\n\n");
gotoxy(30,5); printf("\"LIST of Candidates\"\n\n\t\t\t\tPRESIDENT:\n\n\t\t\t[A] Manny Pacquiao\n\t\t\t[B] Boy Abunda\n"); pres=getch();
switch(pres) { case 'a': case 'A': printf("\nYou have voted for Manny Pacquiao"); break;
case 'b': case 'B': printf("\nYou have voted for Boy Abunda"); }
gotoxy(30,5); printf("\"LIST of Candidates\"\n\n\t\t\t\tVice-PRESIDENT:\n\n\t\t\t[A] Angel Locsin\n\t\t\t[B] Katrina Halili\n"); vpres=getch();
switch(vpres) { case 'a': case 'A': printf("\nYou have voted for Angel Locsin"); break; case 'b': case 'B': printf("\nYou have voted for Katrina Halili"); break; }
gotoxy(30,5); printf("\"LIST of Candidates\"\n\n\t\t\t\tSenators:\n\n\t\t\t[A] Lacson\n\t\t\t[B] De Castro\n\t\t\t[C] Roxas\n\t\t\t[D] Villar\n"); scanf("%c,%c,%c", &sen1,&sen2,&sen3);
printf("You have voted for %c,%c,%c", sen1, sen2, sen3);
Well first of all... goto.. we have seen a rash of it recently. Although there is technically nothing wrong with using goto -- you will find that it complicates your code and makes it hard to maintain. Try to use control structures (while-loop, do-while loop, for-loop, if-statment, if-else, switch etc.) rather than goto.
here is an example program which may help:
cpp
#include <stdio.h> #include <time.h>
int isLeapYear(int ); int daysInMonth(int , int );
int main() { int dd,mm,yy; int age; int today_dd, today_mm, today_yy; struct tm *currentDateTime; time_t theTime;
//Find the current date time(&theTime); currentDateTime = localtime(&theTime); today_dd = currentDateTime->tm_mday; today_mm = currentDateTime->tm_mon+1; today_yy = currentDateTime->tm_year+1900; printf("Today's date: %d/%d/%d\n", today_dd, today_mm, today_yy);
do { printf("please input your birth date in the format dd/mm/yyyy : "); scanf("%d/%d/%d", &dd, &mm, &yy); age = -1; //since I don't do any of the invald checks... if (yy < today_yy && yy > 1900) { if (mm > 0 && mm <=12) { if (dd >= 1 && dd <= daysInMonth(mm, yy)) { if (today_mm < mm) { age = today_yy - yy - 1; } else if (today_dd < dd) { age = today_yy - yy - 1; } else { age = today_yy - yy; } } // else invalid day } // else invalid month } //else invalid year
if (age < 18 && age > 0) { printf("Too young to vote! \n"); } } while(age < 18); printf("date: %d/%d/%d, you age is: %d\n", dd, mm, yy, age); return 0; }
int daysInMonth(int month, int year) { // jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int retValue; if (isLeapYear(year) && month == 2) { retValue = 29; } else { retValue = days[month - 1]; } return retValue; }
int isLeapYear(int year) { //Formula via wikipedia return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0); }
this code has a bug i tried inputting 2/29/1991.. it should give like error message because month is till 12(months) please help me debugg this one.. it should give errors if the user inputted wrong nos. please help meeee