EvilZone

Programming and Scripting => Beginner's Corner => : haseebr21 February 04, 2015, 11:58:46 AM

: My First Semester Project -Vote Casting Program in C++
: haseebr21 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.

: (c++)
#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);




}

: Re: My First Semester Project -Vote Casting Program in C++
: kenjoe41 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.
: Re: My First Semester Project -Vote Casting Program in C++
: haseebr21 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)
: Re: My First Semester Project -Vote Casting Program in C++
: L0aD1nG 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").
: Re: My First Semester Project -Vote Casting Program in C++
: haseebr21 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.
: Re: My First Semester Project -Vote Casting Program in C++
: Kulverstukas 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.

(http://imgs.xkcd.com/comics/goto.png)
: Re: My First Semester Project -Vote Casting Program in C++
: haseebr21 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.

(http://imgs.xkcd.com/comics/goto.png)


I will :)
: Re: My First Semester Project -Vote Casting Program in C++
: ArkPhaze March 22, 2015, 11:48:06 PM
I'm surprised that nobody mentioned anything about the use of the gets() function:
:
gets(cnic);
Never use gets(), ever. Sweet jesus buffer-overflow christ..
: Re: My First Semester Project -Vote Casting Program in C++
: L0aD1nG March 23, 2015, 05:02:47 PM
I'm surprised that nobody mentioned anything about the use of the gets() function:
:
gets(cnic);
Never use gets(), ever. Sweet jesus buffer-overflow christ..

Correct.
:
fgets(cnic, 12, stdin);
Would be better.