how add class

hello I want to ask ... the program below is the presidential election program and the program is ready. I want to ask how to add classes to the program? please help. I'm already dizzy
this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
void header()
{
 cout<<"\t\t--presidential election program -- "<<endl<<endl;
}
int inservote (int number, int pres1, int pres2,int inval)
{
    if (number==1)
    {
        pres1+=1;
        return pres1;
    }
    else if (number==2)
    {
        pres2+=1;
        return pres2;
    }
    else
    {
        inval+=1;
        return 0;
    }
}

void winBoard(int pres1,int pres2,int amount,int inval)
{
    if (pres1>pres2)
    {
        cout<<"congratulation president 1 win by  "<<pres1<<" vote."<<endl;
    }
    else if (pres2>pres1)
    {
        cout<<"congratulation president 2 win by   "<<pres2<<" suara."<<endl;
    }
    else
    {
        cout<<"draw"<<endl;
    }
    cout<<endl;
    cout<<" total "<<amount<<"  chooser."<<endl;
    cout<<"president 1 by : "<<pres1<<"  vote."<<endl;
    cout<<"president 2 by : "<<pres2<<"  vote."<<endl;
    cout<<"invalid : "<<inval<<"  vote."<<endl;
    cout<<endl;
    cout<<endl;
    cout<<"selection complete "<<endl;

}
int main()
{

    header();
    int amount_chooser ,counter = 0,choice, president1=0,president2=0,invalid=0;
    cout<<"input the amount chooser : "<<endl;
    cin>>amount_chooser;
    system("CLS");

    while (counter < amount_chooser)
    {
    header();
    cout<<"selecktor to: "<<counter+1<<"   of  "<<amount_chooser<<" chooser."<<endl;
    cout<<endl;
    cout<<"press 1 for president 1 or press 2 for president 2   : "<<endl;
    cin>>choice;

    if (choice==1)
        president1=inservote(choice,president1,president2,invalid);
    else if (choice==2)
        president2=inservote(choice,president1,president2,invalid);
    else
        invalid=inservote(choice,president1,president2,invalid);
    counter++;
    system("CLS");
    }
   header();
    cout<<endl;

    winBoard(president1,president2,amount_chooser,invalid);
    getch();

    return 0;
}
Last edited on
Please use code tags when posting code. Highlight the code and click the <> button to the right of the edit window.

You can simplify your code by passing some parameters by reference to insevote():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void
inservote(int number, int &pres1, int &pres2, int &inval)
{
    if (number == 1) {
        pres1 += 1;
    } else if (number == 2) {
        pres2 += 1;
    } else {
        inval += 1;
    }
}
...
        cout << "press 1 for president 1 or press 2 for president 2 : " << endl;
        cin >> choice;

        inservote(choice, president1, president2, invalid);


What would you do it there were 3 candidates? Or 5? Or 17? You need a store the candidates in a vector or something similar.

I'd create a class for a candidate and a class for the election.
how's that? I don't understand ... can you help me?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <vector>
#include <algorithm>
#include <iostream>
#include <string>

class Candidate
{
    std::string m_name;
    int m_votes = 0;
    
public:
    std::string get_name() const { return m_name; }
    void set_name( const std::string & name)
    { m_name = name; }
    
    void do_vote() { ++ m_votes; }
    int get_votes() const { return m_votes; }
};

class Election
{
    std::vector<Candidate> m_candidates;
    int m_amount= 0;
    int m_invalid = 0;
    
public:

    void add_candidate( const std::string & name)
    {
        Candidate c;
        c.set_name( name );
        m_candidates.push_back( c );
    }
    void print_candidates() const
    {
        for( auto & candidate : m_candidates )
        {
            std::cout << "name: " << candidate.get_name()
                      << " votes: " << candidate.get_votes()
                      << std::endl;
        }
    }
    void vote( const std::string & name)
    {
        auto p = std::find_if( 
            m_candidates.begin(),
            m_candidates.end(),
            [&]( Candidate & c){ return c.get_name() == name; }
        );
        if( p == m_candidates.end() ) ++ m_invalid;
        else p->do_vote();
        ++ m_amount;
    }
    void print_resolution()
    {
        std::sort( 
            m_candidates.begin(),
            m_candidates.end(), 
            [] ( Candidate & a, Candidate & b) { return a.get_votes() > b.get_votes(); }
        );
        
        using namespace std;
        
        cout << endl << endl;
        if ( m_candidates[0].get_votes() > m_candidates[1].get_votes() )
        {
            std::cout<<"congratulation, "<<  m_candidates[0].get_name() << " wins by "
                     <<  m_candidates[0].get_votes() << " votes." << endl;
        }
        else
        {
            cout<<"draw"<<endl;
        }
            
        cout<<endl<<endl;
        for( auto & c : m_candidates )
        {
            cout<<c.get_name()<<" got "<<c.get_votes()<<" vote."<<endl;
        }
        cout << endl;
        cout<<"total   : "<<m_amount<<"  chooser."<<endl;
        cout<<"invalid : "<<m_invalid<<"  vote."<<endl;
        cout<<endl;
        cout<<endl;
        cout<<"selection complete "<<endl;

    }
};

int main()
{
    const int NO_OF_CANDIDATES = 3;
    Election election;
    
    std::cout << "Enter the names of all candidates:\n";
    for( int i = 0;  i < NO_OF_CANDIDATES; ++i )
    {
        std::string name;
        std::getline( std::cin, name );
        election.add_candidate( name );
    }
    
    std::cout << "Vote the candidates.\n";
    while( true )
    {
        std::cout << "\nEnter your candidate: ";
        std::string name;
        std::getline(std::cin, name);
        if (name == "none") break;
        election.vote(name);
    }
    
    election.print_resolution();
}
Last edited on
whats wrong?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
||=== Build: Debug in tes class (compiler: GNU GCC Compiler) ===|
C:\Users\slowski\Downloads\tes class\main.cpp|9|warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11|
C:\Users\slowski\Downloads\tes class\main.cpp|23|warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11|
C:\Users\slowski\Downloads\tes class\main.cpp|24|warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11|
C:\Users\slowski\Downloads\tes class\main.cpp||In member function 'void Election::print_candidates() const':|
C:\Users\slowski\Downloads\tes class\main.cpp|36|warning: 'auto' changes meaning in C++11; please remove it [-Wc++0x-compat]|
C:\Users\slowski\Downloads\tes class\main.cpp|36|warning: range-based 'for' loops only available with -std=c++11 or -std=gnu++11|
C:\Users\slowski\Downloads\tes class\main.cpp|38|error: expected primary-expression before '<<' token|
C:\Users\slowski\Downloads\tes class\main.cpp|38|error: request for member 'get_name' in 'candidate', which is of non-class type 'int'|
C:\Users\slowski\Downloads\tes class\main.cpp|39|error: expected primary-expression before '<<' token|
C:\Users\slowski\Downloads\tes class\main.cpp|39|error: request for member 'get_votes' in 'candidate', which is of non-class type 'int'|
C:\Users\slowski\Downloads\tes class\main.cpp|40|error: expected primary-expression before '<<' token|
C:\Users\slowski\Downloads\tes class\main.cpp||In member function 'void Election::vote(const string&)':|
C:\Users\slowski\Downloads\tes class\main.cpp|45|warning: 'auto' changes meaning in C++11; please remove it [-Wc++0x-compat]|
C:\Users\slowski\Downloads\tes class\main.cpp|45|error: 'p' does not name a type|
C:\Users\slowski\Downloads\tes class\main.cpp|49|error: expected primary-expression before ')' token|
C:\Users\slowski\Downloads\tes class\main.cpp|50|error: 'p' was not declared in this scope|
C:\Users\slowski\Downloads\tes class\main.cpp||In member function 'void Election::print_resolution()':|
C:\Users\slowski\Downloads\tes class\main.cpp|59|warning: lambda expressions only available with -std=c++11 or -std=gnu++11|
C:\Users\slowski\Downloads\tes class\main.cpp|60|error: no matching function for call to 'sort(std::vector<Candidate>::iterator, std::vector<Candidate>::iterator, Election::print_resolution()::<lambda(Candidate&, Candidate&)>)'|
C:\Program Files\CodeBlocks\MinGW\lib\gcc\mingw32\5.1.0\include\c++\bits\stl_algo.h|4689|note: candidate: template<class _RAIter> void std::sort(_RAIter, _RAIter)|
C:\Program Files\CodeBlocks\MinGW\lib\gcc\mingw32\5.1.0\include\c++\bits\stl_algo.h|4689|note:   template argument deduction/substitution failed:|
C:\Users\slowski\Downloads\tes class\main.cpp|60|note:   candidate expects 2 arguments, 3 provided|
C:\Program Files\CodeBlocks\MinGW\lib\gcc\mingw32\5.1.0\include\c++\bits\stl_algo.h|4718|note: candidate: template<class _RAIter, class _Compare> void std::sort(_RAIter, _RAIter, _Compare)|
C:\Program Files\CodeBlocks\MinGW\lib\gcc\mingw32\5.1.0\include\c++\bits\stl_algo.h|4718|note:   template argument deduction/substitution failed:|
C:\Users\slowski\Downloads\tes class\main.cpp|60|required from here|
C:\Users\slowski\Downloads\tes class\main.cpp|60|error: template argument for 'template<class _RAIter, class _Compare> void std::sort(_RAIter, _RAIter, _Compare)' uses local type 'Election::print_resolution()::<lambda(Candidate&, Candidate&)>'|
C:\Users\slowski\Downloads\tes class\main.cpp|60|error:   trying to instantiate 'template<class _RAIter, class _Compare> void std::sort(_RAIter, _RAIter, _Compare)'|
C:\Users\slowski\Downloads\tes class\main.cpp|76|warning: 'auto' changes meaning in C++11; please remove it [-Wc++0x-compat]|
C:\Users\slowski\Downloads\tes class\main.cpp|76|warning: range-based 'for' loops only available with -std=c++11 or -std=gnu++11|
C:\Users\slowski\Downloads\tes class\main.cpp|78|error: request for member 'get_name' in 'c', which is of non-class type 'int'|
C:\Users\slowski\Downloads\tes class\main.cpp|78|error: request for member 'get_votes' in 'c', which is of non-class type 'int'|
||=== Build failed: 13 error(s), 10 warning(s) (0 minute(s), 1 second(s)) ===|
Your compiler is old and doesn't compile as at least C++11 by default.
Compile with -std=c++11 as a compiler flag.

This is probably an option in your IDE; check your compiler settings.
Edit: In CodeBlocks go to Settings --> Compiler --> check "Have g++ follow the C++11 ISO C++ language standard".
Last edited on
Okee..but error

1
2
3
4
5
6
7
||=== Build: Debug in tes class (compiler: GNU GCC Compiler) ===|
C:\Users\slowski\Downloads\tes class\main.cpp||In member function 'void Election::print_candidates() const':|
C:\Users\slowski\Downloads\tes class\main.cpp|38|error: expected primary-expression before '<<' token|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 37 second(s)) ===|


nuderobmonkey's code compiles for me. I don't see anything particularly wrong with line 38 or the print_candidates function.

Are you compiling it verbatim or did you change something?
Last edited on
Topic archived. No new replies allowed.