Author Topic: My First Semester Project -Vote Casting Program in C++  (Read 1240 times)

0 Members and 1 Guest are viewing this topic.

Offline haseebr21

  • /dev/null
  • *
  • Posts: 18
  • Cookies: 2
  • If you’re not first, you’re last.
    • View Profile
My First Semester Project -Vote Casting Program in C++
« on: February 04, 2015, 11:58:46 am »
This was my first semester project, not good enough but atleast I tried ;)  8)

In this program, user inputs a 13 digit CNIC and his gender, both are stored in a file and user castes vote, after the program terminates, a final result is also stored in a file, if previously entered CNIC is entered again the program rejects it and if user enters previously entered CNIC twice the program beeps and terminates.

Code: (c++) [Select]
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
#include<dos.h>
#include<string.h>
#include<fstream.h>


int main()
{
clrscr();
int i=0,P1=0,P2=0,P3=0,choice,q,total=0,ch,j=0,found=0,c=0,x;
char label,cnic[12],gender,cnic2[15][15],e;




do
{
   clrscr();
   label:
   cout<<"\t \t \t Welcome\n\n\n\n\n"<<"Please Enter The Following Details to Vote?\n";
   cout<<"\nEnter Your CNIC?\n";
   gets(cnic);






/* for (i=0;i<13;i++) // (1)
{
    if (strcmp(cnic2[j],cnic)==0)  // (2)
    {
   cout<<"This Cnic has already casted the vote";
getch();
exit(1);
    }
}
cnic2[j]=cnic[12];
*/




   if(strlen(cnic)!=13)
   {
      cout<<"Please Check Your CNIC Again";
      getch();
      clrscr();
      goto label;
   }
   found = 0;  /* should be found = false; */
   for(i=0; i<j; i++) {
   if (0 == strcmp(cnic, cnic2))
    {
      found = 1;   /* should be found = true; */
     break;
      }
   }
   if (found) {
   cout<<"This Cnic has already casted the vote.";
   c++;
   getch();
   clrscr();
   if(c==2)
   {
   sound(1000);
   delay(1000);
   nosound();
   getch();
   exit(1);
   }




       continue;  /* back to beginning of main loop */


   }




   c=0;
   if (j ==10) {
   cout << "Too much Cnic already";
   break;   /* or continue, up to you ... */
   }
   strcpy(cnic2[j++], cnic);
   cout<<endl;
   do
   {
      cout<<"\n\nWrite As M/F.\n\nGender: ";
      cin>>gender;
      if(gender!='m' && gender!='f' && gender!='M' && gender!='F')
      {
         cout<<"\nGender was wrong.";
         clrscr();
      }
      else if(gender=='q')
      exit(1);
      else
      break;
   }
   while(gender!='m' || gender!='f' || gender!='M' || gender!='F');




      ofstream ui("ui.txt",ios::app);
      if(!ui)
      {
         cerr<<"File Opening Error";
         getch();
         exit(1);
      }
      while(ui)
      {
      ui<<cnic<<" ";
      ui<<gender<<"\n";


      ui.close();
      }


      clrscr();




   cout<<"\n\nChoose any of these candidates:\n\n\n\nP1=1\n\nP2=2\n\nP3=3\n\n";
   cin>>choice;
   clrscr();
      if(choice==0333)
      {
        exit(1);
      }


   switch(choice) {
      case 1:
         cout<<"\n\nParty1 got "<<(P1=P1+1)<<" votes\n";
      break;
      case 2:
         cout<<"\nParty2 got "<<(P2=P2+1)<<" votes\n";
      break;
      case 3:
         cout<<"\nParty3 got "<<(P3=P3+1)<<" votes\n";
      break;
      default:
         cout<<"Wrong choice";
      break;
         }
   total=P1+P2+P3;
   clrscr();
   cout<<"Thank You for voting";
   delay(500);
   for(x=1000;x>=200;x=x-50)
   {
   sound(x);
   delay(100);


   }
   nosound();




      ofstream result("result.txt");
         if(!result)
            {
            cerr<<"File Opening Error";
            getch();
            exit(1);
            }
         while(result)
            {
            result<<"\n\n\nTotal Votes Casted are:\t"<<(total)<<"\n"<<"\n\n\n";
            result<<"\tParty1 Got "<<(P1)<<" votes";
            result<<"\n\tParty2 Got "<<(P2)<<" votes";
            result<<"\n\tParty3 Got "<<(P3)<<" votes"<<"\n\t";
            if(P1>P2 && P1>P3)
            {
               result<<"P1 Has Won";
            }
            else if(P2>P3 && P2>P1)
            {
               result<<"P2 Has Won";


            }
            else if(P3>P2 && P3>P1)
            {
               result<<"P3 Has Won";
            }
            result.close();
            }


            clrscr();
            cout<<"Press Any Key...\n";
}
while(total!=100);


      clrscr();




            getch();






     return(0);




}

« Last Edit: February 04, 2015, 06:21:14 pm by Kulverstukas »
Throw me to the Wolves and I will return leading the pack.

"A successful man is one who can lay a firm foundation with the bricks others have thrown at him." --David Brinkley

"Don't raise your voice, improve your argument."

Doubt me, hate me, you’re the inspiration I need.

There are so many people out there who will tell you that you can’t. What you’ve got to do is to turn around and say – Watch me.

Offline kenjoe41

  • Symphorophiliac Programmer
  • Administrator
  • Baron
  • *
  • Posts: 990
  • Cookies: 224
    • View Profile
Re: My First Semester Project -Vote Casting Program in C++
« Reply #1 on: February 04, 2015, 12:31:30 pm »
First, for a C++ project, you are using alot of C-style headers. Drop the .h extension, the headers should work just fine. Most of what is in stdio.h should be already in iostream. My compiler has no iostream.h file. You sure need a fine lining between C and C++.
Secondly, your indetation here suck, hence it is alittle difficult to follow your code but lets see what good we can make of it.
For zeus' sake drop the use of goto. Just not a good programming style lately.
You better think of a better datastructure to store your cnic data.
You are clearing the screen like 100 times, not something amazing.
I also think you should read up on the good use of semaphores in loops.
I personally never thought of any use of the sound thingamajig.

Alot to improve on this code, and i presume you haven't covered classes yet but a class could have beautified things in the main function a bit.

Ooh, you also missed afew braces, fix that and more will be added. Also arrange you coe in a meaningfull manner. Here everything is like just remembered and thrown in there.
If you can't explain it to a 6 year old, you don't understand it yourself.
http://upload.alpha.evilzone.org/index.php?page=img&img=GwkGGneGR7Pl222zVGmNTjerkhkYNGtBuiYXkpyNv4ScOAWQu0-Y8[<NgGw/hsq]>EvbQrOrousk[/img]

Offline haseebr21

  • /dev/null
  • *
  • Posts: 18
  • Cookies: 2
  • If you’re not first, you’re last.
    • View Profile
Re: My First Semester Project -Vote Casting Program in C++
« Reply #2 on: February 04, 2015, 12:43:07 pm »
First, for a C++ project, you are using alot of C-style headers. Drop the .h extension, the headers should work just fine. Most of what is in stdio.h should be already in iostream. My compiler has no iostream.h file. You sure need a fine lining between C and C++.


well thank you for the tips :) I'll keep those in mind.

As for .h ext I was using turboc compiler. (restriction by university)
Throw me to the Wolves and I will return leading the pack.

"A successful man is one who can lay a firm foundation with the bricks others have thrown at him." --David Brinkley

"Don't raise your voice, improve your argument."

Doubt me, hate me, you’re the inspiration I need.

There are so many people out there who will tell you that you can’t. What you’ve got to do is to turn around and say – Watch me.

Offline L0aD1nG

  • Peasant
  • *
  • Posts: 83
  • Cookies: 6
  • NeverFear1isHere
    • View Profile
Re: My First Semester Project -Vote Casting Program in C++
« Reply #3 on: February 04, 2015, 03:05:28 pm »
Secondly, your indetation here suck, hence it is alittle difficult to follow your code
This ^

Dude whats wrong with the <Enter> spam while you are coding.
Stop putting 1000 empty lines for each line you write, one empty line is just fine I guess(or mostly two if "needed").

Offline haseebr21

  • /dev/null
  • *
  • Posts: 18
  • Cookies: 2
  • If you’re not first, you’re last.
    • View Profile
Re: My First Semester Project -Vote Casting Program in C++
« Reply #4 on: February 04, 2015, 03:22:44 pm »
This ^

Dude whats wrong with the <Enter> spam while you are coding.
Stop putting 1000 empty lines for each line you write, one empty line is just fine I guess(or mostly two if "needed").




I'll try not to repeat old mistakes :) as for this code, i had alot of pressure as there wasn't any time left to submit it, but then again, lame excuses are not tolerable.
Throw me to the Wolves and I will return leading the pack.

"A successful man is one who can lay a firm foundation with the bricks others have thrown at him." --David Brinkley

"Don't raise your voice, improve your argument."

Doubt me, hate me, you’re the inspiration I need.

There are so many people out there who will tell you that you can’t. What you’ve got to do is to turn around and say – Watch me.

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: My First Semester Project -Vote Casting Program in C++
« Reply #5 on: February 04, 2015, 06:23:29 pm »
Oh my god - you need to work on your coding style more than anything, bro. Indentation sucks, extra spaces suck, formatting sucks as well.

« Last Edit: February 04, 2015, 06:24:11 pm by Kulverstukas »

Offline haseebr21

  • /dev/null
  • *
  • Posts: 18
  • Cookies: 2
  • If you’re not first, you’re last.
    • View Profile
Re: My First Semester Project -Vote Casting Program in C++
« Reply #6 on: February 04, 2015, 07:17:28 pm »
Oh my god - you need to work on your coding style more than anything, bro. Indentation sucks, extra spaces suck, formatting sucks as well.




I will :)
Throw me to the Wolves and I will return leading the pack.

"A successful man is one who can lay a firm foundation with the bricks others have thrown at him." --David Brinkley

"Don't raise your voice, improve your argument."

Doubt me, hate me, you’re the inspiration I need.

There are so many people out there who will tell you that you can’t. What you’ve got to do is to turn around and say – Watch me.

Offline ArkPhaze

  • Peasant
  • *
  • Posts: 136
  • Cookies: 20
  • null terminated
    • View Profile
Re: My First Semester Project -Vote Casting Program in C++
« Reply #7 on: March 22, 2015, 11:48:06 pm »
I'm surprised that nobody mentioned anything about the use of the gets() function:
Code: [Select]
gets(cnic);
Never use gets(), ever. Sweet jesus buffer-overflow christ..
« Last Edit: March 22, 2015, 11:48:49 pm by ArkPhaze »
sig=: ArkPhaze

[ J/ASM/.NET/C/C++ - Software Engineer ]

Offline L0aD1nG

  • Peasant
  • *
  • Posts: 83
  • Cookies: 6
  • NeverFear1isHere
    • View Profile
Re: My First Semester Project -Vote Casting Program in C++
« Reply #8 on: March 23, 2015, 05:02:47 pm »
I'm surprised that nobody mentioned anything about the use of the gets() function:
Code: [Select]
gets(cnic);
Never use gets(), ever. Sweet jesus buffer-overflow christ..

Correct.
Code: [Select]
fgets(cnic, 12, stdin);
Would be better.