Welcome to Dream.In.Code
Getting C++ Help is Easy!

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!




Election program

 
Reply to this topicStart new topic

Election program, nid help

angel0lz
post 22 Jul, 2008 - 05:46 AM
Post #1


New D.I.C Head

*
Joined: 19 Jul, 2008
Posts: 8

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


CODE
#include<stdio.h>
#include<conio.h>
#define YES ('Y'-'y')



main()
{

    int year, name;
    char ans, vote, name1,name2,name3,name4,name5,name6,name7,name8,name9,name10,name11,name12,name1
3;
    
    clrscr();
    
    
    printf("Enter voter's name:");
    scanf("%s", &name);
    try_again:
    clrscr();
    printf("Enter your year of birth:");
    scanf("%d", &year);
        if( (year<=0) || (year>2008) || (year<1880) || (year>=1993) )
        {
        printf("You entered questionable year.\a");
        printf("\n\nPress any key to try again. . .");
        ans=getche();
        goto try_again;
        
        }

        else if (year<=1992)
        {
            printf("You are in the right age to vote!\n\n");
            printf("\n\t\t\t\tList of Candidates\n");
            printf("President:\n\t[A]Name1\n\t");
            scanf("%c", &name1);
            printf("[B]Name2\n");
            scanf("%c", &name2);
            printf("Vice-President:\n\t[c]Name3\n\t");
            scanf("%c", &name3);
            printf("[D]Name4\n");
            scanf("%c", &name4);
            printf("Senators:\n\t");
            printf("[E]Name5\n\t");
            scanf("%c", &name5);
            printf("[F]Name6\n\t");
            scanf("%c", &name6);
            printf("\n[G]Name7\n\t");
            scanf("%c", &name7);
            printf("[H]Name8\n\t");
            scanf("%c", &name8);
            printf("[I]Name9\n");
            scanf("%c", &name9);
            printf("Governor:\n\t[J]Name10\n\t");
            scanf("%c", &name10);
            printf("[K]Name11\n");
            scanf("%c", &name11);
            printf("Mayor:\n\t[L]Name12\n\t");
            scanf("%c", &name12);
            printf("[M]Name13\n");
            scanf("%c", &name13);
        }


    getch();        
}





User is offlineProfile CardPM

Go to the top of the page


NickDMax
post 22 Jul, 2008 - 01:23 PM
Post #2


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,856



Thanked 46 times

Dream Kudos: 550
My Contributions


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);
}

User is offlineProfile CardPM

Go to the top of the page

angel0lz
post 24 Jul, 2008 - 07:54 AM
Post #3


New D.I.C Head

*
Joined: 19 Jul, 2008
Posts: 8

plss help me improve my program
CODE

#include<stdio.h>
#include<conio.h>
#include<math.h>

#define MAX 18

#define EDAY 10
#define EMONTH 5
#define EYEAR 2010


int age,yyage,mmage,ddage;



main() {
    
    
    int mm,dd,yy;
    

    clrscr();
    
    
    try_again:

    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");
        
        goto try_again;    }

    else if(yyage>MAX)     {
        election();    }

    
    else if(yyage==MAX)    {
        birthmonth();    }
    /*}while(yyage<18 || yyage!=18);
    return 0;*/
    getch();    }



birthmonth()    {
if(mmage<EMONTH){
election();    }
if(mmage<1)     {
goto try_again;    }
if(mmage==0)    {
birthdate()    }
}    

birthdate() {
if(ddage<EDAY) {
election(); }
else if(ddage<1) {
printf("Terminate"); }
else if(ddage==0) {
election(); }
    
}    


election() {
char pres,vpres,sen1,sen2,sen3;


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);
                                    
            
            
    
    getch();
}    

User is offlineProfile CardPM

Go to the top of the page

angel0lz
post 6 Sep, 2008 - 10:36 PM
Post #4


New D.I.C Head

*
Joined: 19 Jul, 2008
Posts: 8

QUOTE(NickDMax @ 22 Jul, 2008 - 02:23 PM) *

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
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 02:41AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month