help..voting machine

This is a discussion on help..voting machine within the C++ Programming forums, part of the General Programming Boards category; can anybosy please write me a code that have following specs *using array *user registration *username and password check that ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    4

    help..voting machine

    can anybosy please write me a code that have following specs
    *using array
    *user registration
    *username and password check that is login
    *allow the user to vote for a particular person(once)
    *publish the results

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    Probably, but you should do your best to write it yourself, and then approach us for help if you run into any problems.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    i tried my best......can u pls help me how to make the login only once..that is if he tries to login more than one time he should not be able to login.....

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    So, what did you try? Post the code.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    Code:
    #include<iostream>
    #include<fstream>
    #include<conio.h>
    
    using namespace std;
    
    void Auth();
    void Members();
    void Userchange();
    void Passchange();
    
    string inuser;
    string inpass;
    string user;
    string pass;
    int num = 0;
    string com;
    
    main()
    {
          system("cls");
          cout<<"Welcome\n---------------------------\n\n";
          while(num==0)
          {
          system("cls");
          cout<<"Command: ";
          cin>>com;
          if(com=="login")
          {
          Auth();
          }
          
          else if(com==com)
          {
               cout<<"Unknown command\n";
               }
          }
    }
    void Auth()
    {
         ifstream Passfile("password.txt", ios::in);
         Passfile>>inpass;
         ifstream Userfile("username.txt", ios::in);
         Userfile>>inuser;
         system("cls");
         cout<<"USERNAME: ";
         cin>>user;
         cout<<"PASSWORD: ";
         cin>>pass;
         Userfile.close();
         Passfile.close();
         if(user==inuser&&pass==inpass)
         {
         cout<<"\nHit enter to continue to members area";
         getch();
         Members();
         }
         else
         {
             cout<<"nope";
             getch();
             main();
             }
    }
    one have only this much...after this i have to make sure sure that the user should be able to login only once as this is a voting machine.....

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    Since your code appears to be C++ rather than C, I am moving this thread to the C++ programming forum.

    Let's deal with your current code first. You should:
    • Change those global variables into local variables.
    • Indent your code properly.
    • Never call the global main function recursively (use a loop).
    • Note that <conio.h> and getch are non-standard.


    As to your problem: I would expect that you need to store several username/password combinations in the file. You then read them from the file into parallel std::vector<std::string> containers. With these at hand, you can then search the container with the usernames to see if the username entered by the user matches any of those that were already entered. If not, you then create a new entry, otherwise you update the corresponding vote if the password matches.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    actually i am a beginner in c++ and c...so i am in learning stage...so not able to unserstand what you are coveying.....is there any siimple way

  8. #8
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107
    Kunku,

    I took Laserlight's comments and implemented them. Especially the scope (local/global) of the variables, and the indention. I don't recall if I've ever tried to call main recursively, I think that an early professor did a song an dance about it early in college. He made reference to material we learned in Java, but it had to do with a concept later referred to as a single entry and single exit...it showed up when we started discussing automata theory. Also, take note of the calls to the "system," I am using a Linux emulator so it's "clear" rather than "cls," and I wrote a "pause" program to wait for a user's key press and left it in the working directory. It is included below:

    Code:
    #include <iostream>
    #include <fstream>
    
    void Auth();
    void Members();
    void Userchange();
    void Passchange();
    
    void WaitForKeypress();
    
    int main(void)
    {
       int num = 0;
       std::string com;
    
       while(true)
       {
          system("clear");
          std::cout << "Welcome\n---------------------------\n\n" << std::endl;
    
          std::cout << "Command: ";
          std::cin >> com;
    
          if( 0 == std::strcmp(com.c_str(),"login") )
          {
             Auth();
          }
          else
          {
             std::cout << "Unknown command\n" << std::endl;
             
             WaitForKeypress();
             std::cin.clear( );
          }
       }
    
       return 0;
    }
    
    void Auth()
    {
       std::string inuser;  // read from system file
       std::string inpass;
    
       std::string user;    // read from user input
       std::string pass;
       
       std::ifstream Passfile("password.txt", std::ios::in);
       std::ifstream Userfile("username.txt", std::ios::in);
    
       if( !Passfile.good() || !Userfile.good() )
       {
           // close the offending file...
           if( !Passfile.good() )
           {
               Passfile.close();
           }
    
           if( !Userfile.good() )
           {
               Userfile.close();
           }
    
           // discontinue program execution...
           std::cerr << "User and/or Password file error...exiting." << std::endl;
           std::exit( 1 );
       }
    
       Passfile >> inpass;
       Userfile >> inuser;
    
       // appearantly there is only one user/password pair in the files, thus they
       // may be closed now, rather than after prompting the user for their
       // credentials
       Userfile.close();
       Passfile.close();
    
    
       system("clear");
    
       std::cout << "USERNAME: ";
       std::cin >> user;
    
       std::cout << "PASSWORD: ";
       std::cin >> pass;
    
       if( user == inuser && pass == inpass )
       {
          WaitForKeypress();
          Members();
       }
       else
       {
          std::cout << "\n Login failed." << std::endl;
    
          std::exit( 1 );
       }
       return;
    }
    
    void Members()
    {
       std::cout << std::endl << "void Members()" << std::endl;
       WaitForKeypress();
    
       return;
    }
    
    void Userchange()
    {
       std::cout << std::endl << "void Userchange()" << std::endl;
       WaitForKeypress();
    
       return;
    }
    
    void Passchange()
    {
       std::cout << std::endl << "void Passchange()" << std::endl;
       WaitForKeypress();
    
       return;
    }
    
    void WaitForKeypress()
    {
        system("./pause");
    
        return;
    }
    Laserlight, I was rather ignorant of the name space behaviors which are being imposed in the current versions of the C++ standard...(I believe 0x is the next), so I intentionally used the qualified names of library variables/constants/functions.

    When I was implementing the "pause" code which follows, I noticed cin's state had been reset upon executing the external program "pause" -- which is clearly because the "pause" program "inherited" the state from the newly invoked shell executing it. The "pause" program would then exit, and the client "vote" would still have an "eof" in the input queue, and loop indefinitely. The test that produced the aforementioned behaviors, corrected with the "cin.clear()" call in the code above, was an end-of-file (Ctrl-D) at a user prompt.

    Code:
    //
    // wait for a keypress from the user, and exit
    
    #include <iostream>
    
    int main( void )
    {
        char input;
    
        if( std::cin.eof() )
        {
            return 254;
        }
    
        if( ! std::cin.good() )
        {
            return 253;
        }
    
        std::cout << std::endl << "Press Enter to continue...";
    
        do
        {
           input = std::cin.get();
        }
        while( std::cin.good() && '\n' != input );
    
        return 0;
    }
    What state is the input stream in initially, do you happen to know? I'll go look for the answer myself, but I figured I would volunteer a question while I was attempting to demonstrate good form for the new learner. I tried several variations on the cin.clear() calls, and found that passing no parameters was the only effective recourse. I'll implement the std::vector suggestion for the next post/reply....

    Best Regards,

    New Ink -- Henry
    Last edited by new_ink2001; 10-11-2010 at 01:35 AM. Reason: grammar
    Kept the text books....
    Went interdisciplinary after college....
    Still looking for a real job since 2005....

    During the interim, I may be reached at ELance, vWorker, FreeLancer, oDesk and WyzAnt.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Turing machine
    By Dante Wingates in forum General Discussions
    Replies: 5
    Last Post: 07-26-2010, 01:47 AM
  2. Will my memory leak? --school project
    By steals10304 in forum C++ Programming
    Replies: 10
    Last Post: 02-24-2010, 02:04 PM
  3. Porting from 32 bit machine to 64 bit machine!
    By anoopks in forum C Programming
    Replies: 10
    Last Post: 02-25-2005, 07:02 PM
  4. More voting machine source code.
    By anonytmouse in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 04-07-2004, 04:41 PM
  5. Voting Machine Source Code
    By anonytmouse in forum Tech Board
    Replies: 1
    Last Post: 11-03-2003, 04:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21