Like Tree1Likes
  • 1 Post By Salem

I can't find what's wrong with my code. A little help please...

This is a discussion on I can't find what's wrong with my code. A little help please... within the C++ Programming forums, part of the General Programming Boards category; here is my code Code: #include <iostream> #include <stdlib.h> using namespace std; class prospective { public: prospective(); ~prospective(); int get_Points() ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    17

    I can't find what's wrong with my code. A little help please...

    here is my code
    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    class prospective
    {
     public:
        prospective();
        ~prospective();
        int get_Points()  {return itsPoints;}
        void set_Points(int number){itsPoints += number;}
        int get_P_Number()  {return itsP_Number;}
        void set_P_Number(int number){itsP_Number=number;}
        char* get_P_Name() {return itsP_Name;}
     private:
        char itsP_Name[15];
        int itsP_Number, itsPoints;
    };
    prospective::prospective()
    {
    
        cout<<"Enter the name of the candidate"<<endl;
        cin>>itsP_Name;
        cin.clear();
        cout<<"Enter the code number of the candidate"<<endl;
        cin>>itsP_Number;
        itsPoints=0;
    }
    
    
    prospective::~prospective()
    {
    }
    
    
    class voter
    {
     public:
        voter();
        ~voter();
        int get_V_Number() const  {return itsV_Number;}
        void set_V_Number(int number){itsV_Number=number;}
     private:
        char itsV_Name[15];
        int itsV_Number;
    };
    
    voter::voter()
    {
        cout<<"Enter the name of the voter"<<endl;
        cin>>itsV_Name;
        cout<<"Enter the code number of the voter"<<endl;
        cin>>itsV_Number;
    
    }
    
    voter::~voter()
    {
    }
    
    
    
    int vote(prospective *array,int x,int z)
    {
        int i,num,counter=x,*revotes,revote_counter_checker;
        bool flag=false;
    
        if(z!=0){
            revotes=(int*)malloc(sizeof(int)*x);
            if (revotes==NULL){
                free(revotes);
                cout<<"Error allocating memory"<<endl;
                return 1;
            }
            for(i=0;i<x;i++){
                revotes[i]=-1;
            }
            cout<<"Enter the code number of the candidate or"<<endl;
            cout<<"if you want to stop voting press -1"<<endl;
            cin>>num;
            do{
                while(flag==false){
                    if(num==-1){
                        cout<<"Moving to the next voter"<<endl;
                        flag=true;
                    }
                i=0;
                while(flag==false && i<x){
                    if(revotes[i]==num){
                        cout<<"I'm sorry but you have alread voted"<<endl;
                        cout<<"for this candidate"<<endl;
                        flag=true;
                    } else{
                        i++;
                    }
                }
                i=0;
                while (flag==false && i<x){
                    if (array[i].get_P_Number()==num){
                        array[i].set_Points(counter);
                        revotes[i]=num;
                        counter--;
                        flag=true;
                    } else {
                        i++;
                    }
                }
                if (counter==0){
                    cout<<"You have voted all the candidates"<<endl;
                    flag=true;
                }
                if(flag==false){
                    cout<<"Enter the code number of the candidate or"<<endl;
                    cout<<"if you want to stop voting press -1"<<endl;
                    cin>>num;
                }
                }
            } while(num!=-1);
            revote_counter_checker=1;
        }else{
            cout<<"You have already voted."<<endl;
            cout<<"You can't do that again."<<endl;
            revote_counter_checker=0;
        }
        return revote_counter_checker;
    }
    
    int main()
    {   int i,j,N_can,N_vot,num_for_ver,counter_for_g_flag=0;
        bool flag,g_flag;
        prospective *can_array;
        voter *vot_array;
    
    
    
        cout<<"Enter the number of the candidates"<<endl;
        cin>>N_can;
    
        cout<<"Enter the number of the voters"<<endl;
        cin>>N_vot;
    
        can_array=(prospective*)malloc(N_can*sizeof(prospective));
        if (can_array==NULL){
            free(can_array);
            cout<<"Error allocating memory"<<endl;
            return 1;
        }
        vot_array=(voter*)malloc(N_vot*sizeof(voter));
        if (vot_array==NULL){
            free(vot_array);
            cout<<"Error allocating memory"<<endl;
            return 1;
        }
        for(i=0; i<N_can; i++){
            can_array[i]=prospective();
        }
         for(i=0; i<N_vot; i++){
            vot_array[i]=voter();
        }
    
    
        while(g_flag==false){
            cout<<"Enter the code number of the voter for varification"<<endl;
            cin>>num_for_ver;
            flag=false;
            j=0;
            while(j<N_vot && flag==false){
                if(vot_array[j].get_V_Number()==num_for_ver){
                    counter_for_g_flag += vote(can_array,N_can,vot_array[j].get_V_Number());
                    vot_array[j].set_V_Number(0);
                    flag=true;
                } else{
                    j++;
                }
            }
            if(flag==false){
                cout<<"There isn't any voter with this code"<<endl;
                cout<<"or the voter with this code has already voted"<<endl;
            }
            if(counter_for_g_flag==N_vot){
                g_flag=true;
            }
        }
        cout<<endl;
        cout<<"The list of the candidates and their points"<<endl;
        for (i=0;i<N_can;i++){
            cout<<can_array[i].get_P_Name()<<"\t"<<can_array[i].get_Points()<<endl;
        }
        free(can_array);
        free(vot_array);
        return 0;
    }
    the problem is that when i run it after a few seconds it crushes
    BUT if i delete the do while here
    Code:
    do{
                while(flag==false){
                    if(num==-1){
                        cout<<"Moving to the next voter"<<endl;
                        flag=true;
                    }
                i=0;
                while(flag==false && i<x){
                    if(revotes[i]==num){
                        cout<<"I'm sorry but you have alread voted"<<endl;
                        cout<<"for this candidate"<<endl;
                        flag=true;
                    } else{
                        i++;
                    }
                }
                i=0;
                while (flag==false && i<x){
                    if (array[i].get_P_Number()==num){
                        array[i].set_Points(counter);
                        revotes[i]=num;
                        counter--;
                        flag=true;
                    } else {
                        i++;
                    }
                }
                if (counter==0){
                    cout<<"You have voted all the candidates"<<endl;
                    flag=true;
                }
                if(flag==false){
                    cout<<"Enter the code number of the candidate or"<<endl;
                    cout<<"if you want to stop voting press -1"<<endl;
                    cin>>num;
                }
                }
            } while(num!=-1);
    the program runs fine till the end
    the only problem is that it does what i want to do only one time and not until num=-1
    can someone please help me?


    ps i know that in c++ it's best to use new and not malloc but my professor said that we have to use malloc in this project

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,764
    So we can have even half a chance at making it crash in the same way you're seeing, can you provide a text file containing an example terminal session.
    Ideally, we need to be able to do
    prog.exe < input.txt

    If it's crashing, use the debugger and start looking around for things like variables with unexplainable values.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Some hints how you could structure your program so that finding mistakes is easier:

    Name your stuff for what it is. You have a lot of "i", "flag" and other nondescriptive variables. I have a tough time seeing if "i < x" is correct when I have no clue what i or x are. Is one of them the user input? If so, name it userinput. Is one of it the voter array index? Name it voterindex.

    Your set_Points method is very unintuitive. If I set points to x, I would expect points to be x afterwards. If you need to add points frequently, you can write a second method with another name (add_Points maybe) that actually adds points.

    Your logic of voting is one huge block that is hard to follow. Split it into smaller functions and name them after what they logically do.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    17
    Quote Originally Posted by Salem View Post
    So we can have even half a chance at making it crash in the same way you're seeing, can you provide a text file containing an example terminal session.
    Ideally, we need to be able to do
    prog.exe < input.txt

    If it's crashing, use the debugger and start looking around for things like variables with unexplainable values.
    can you tell me how i can give you the text file from my session?


    Quote Originally Posted by nvoigt View Post
    Some hints how you could structure your program so that finding mistakes is easier:

    Name your stuff for what it is. You have a lot of "i", "flag" and other nondescriptive variables. I have a tough time seeing if "i < x" is correct when I have no clue what i or x are. Is one of them the user input? If so, name it userinput. Is one of it the voter array index? Name it voterindex.

    Your set_Points method is very unintuitive. If I set points to x, I would expect points to be x afterwards. If you need to add points frequently, you can write a second method with another name (add_Points maybe) that actually adds points.

    Your logic of voting is one huge block that is hard to follow. Split it into smaller functions and name them after what they logically do.
    you are absolutely right
    even my self i have trouble reading it
    i will try to rewrite my code but this time i'll try to follow your suggestions
    thanks for the tips

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,764
    Well my terminal session looks like this - I don't know if it's right or not, but it stops working at the end.
    Code:
    $ ./a.out 
    Enter the number of the candidates
    3
    Enter the number of the voters
    5
    Enter the name of the candidate
    Obama
    Enter the code number of the candidate
    1
    Enter the name of the candidate
    Romney
    Enter the code number of the candidate
    2
    Enter the name of the candidate
    Paul
    Enter the code number of the candidate
    3
    Enter the name of the voter
    Fred
    Enter the code number of the voter
    10
    Enter the name of the voter
    Barney
    Enter the code number of the voter
    11
    Enter the name of the voter
    Wilma
    Enter the code number of the voter
    12
    Enter the name of the voter
    Betty
    Enter the code number of the voter
    13
    Enter the name of the voter
    Pebbles
    Enter the code number of the voter
    14
    Enter the code number of the voter for varification
    11
    Enter the code number of the candidate or
    if you want to stop voting press -1
    3
    -1
    So I have a text file like this, to save typing
    Code:
    3
    5
    Obama
    1
    Romney
    2
    Paul
    3
    Fred
    10
    Barney
    11
    Wilma
    12
    Betty
    13
    Pebbles
    14
    11
    3
    -1
    So you can just run the program with
    prog.exe < input.txt
    and see all the questions answered in quick succession as if you typed everything in.

    > can you tell me how i can give you the text file from my session?
    Well that depends on your system.
    Most (if not all) terminal programs on a windowed environment have some kind of logging, even if it is just as dumb as "select all text" and "copy", at which point you can paste it into your editor and save to a file. Windows users should set the console to have a decent amount of scroll-back.
    Users on a Unix/Linux type system should have the script utility, which logs everything to a file, regardless of terminal capabilities.
    stahta01 likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    17
    Quote Originally Posted by Salem View Post
    Well my terminal session looks like this - I don't know if it's right or not, but it stops working at the end.
    Code:
    $ ./a.out 
    Enter the number of the candidates
    3
    Enter the number of the voters
    5
    Enter the name of the candidate
    Obama
    Enter the code number of the candidate
    1
    Enter the name of the candidate
    Romney
    Enter the code number of the candidate
    2
    Enter the name of the candidate
    Paul
    Enter the code number of the candidate
    3
    Enter the name of the voter
    Fred
    Enter the code number of the voter
    10
    Enter the name of the voter
    Barney
    Enter the code number of the voter
    11
    Enter the name of the voter
    Wilma
    Enter the code number of the voter
    12
    Enter the name of the voter
    Betty
    Enter the code number of the voter
    13
    Enter the name of the voter
    Pebbles
    Enter the code number of the voter
    14
    Enter the code number of the voter for varification
    11
    Enter the code number of the candidate or
    if you want to stop voting press -1
    3
    -1
    that's my problem (where i highlighted)
    normally after 3 it should print
    "Enter the code number of the candidate or
    if you want to stop voting press -1
    but instead of that it just prints a new line where i have to type a value (you typed -1) and then it just stops working
    have you got any idea what's going on;
    (if you have trouble understanding my code wait till tomorrow
    i will remake my code to look more understandable)

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Yeah, lots of complexity in this one...
    You need to stop thinking as a C dev and start thinking as a C++ dev.
    Get rid of malloc and free. Use std::vector.
    Get rid of char arrays. Use std::string.
    Stop putting all variables at the beginning of a function! Declare them near first use (such as in loops), and give them proper names.
    Never ever use cin to read into a char array. Use std::string instead!

    Fix all that stuff, indent properly, then check your code again to see if it behaves, and if not, post your new code.
    Don't bother hunting down the bug in this spagetti code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Please do not add a destructor to a class that does not need one.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  2. i can't find what's wrong with this code
    By blindleaf in forum C Programming
    Replies: 3
    Last Post: 11-12-2002, 03:29 PM
  3. Can someone find out what i'm doing wrong?
    By QuincyEQ in forum C Programming
    Replies: 2
    Last Post: 08-15-2002, 12:22 PM
  4. Help i cant find whats wrong with my code
    By 0927 in forum C++ Programming
    Replies: 1
    Last Post: 12-21-2001, 09:14 PM
  5. Help me find out what's wrong
    By biosx in forum C Programming
    Replies: 4
    Last Post: 08-15-2001, 01:37 PM

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