Thread: Chapter 5- Question 7

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    4

    Post Chapter 5- Question 7

    Hello C++ programming community!!

    I was wondering if someone can give me hints and guidelines on my code to make it better-in terms of industry standards. I know there are many answers to this question, but I want to get my mind in always improving the current program I have. Shown below is the code.
    Thanks.

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    
    int main()
    {
        int entry;
        int cand1 = 0;
        int cand2 = 0;
        int cand3 = 0;
        do
        {
            cout << "Please enter the candidate chosen for the election"<<endl ;
            cout << "Enter 1 for Trump." << endl;
            cout << "Enter 2 for Hiliary" << endl;
            cout << "Enter 3 for Burg" << endl;
            cout << "Enter 0 to exit" << endl;
            cin >> entry;
            if( entry == 1 )
            {
                cand1++;
            }
            else if( entry == 2)
            {
                cand2++;
            }
            else if ( entry == 3)
            {
                cand3++;
            }
            else
                cout << "Enter a valid entry"<<endl;
        }
        while(entry != 0);
        
        cout <<setw(4)<<"Trump: ";
        for (int entry = 0; (entry < cand1); entry ++)
        {
            cout << "  *";
        }
        cout<<endl;
        cout << setw(4)<< "Hilia: ";
        for (int entry2 = 0; (entry2 < cand2); entry2++)
        {
            cout <<"  *";
        }
        cout <<endl;
        cout << setw(4)<< "Burgi: ";
        for ( int entry3 = 0; (entry3 < cand3); entry3++)
        {
            cout << "  *";
        }
        
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,745
    I guess the first thing is to start making use of arrays.
    Whenever you start creating variables with numeric suffixes, you need to think arrays.

    Code:
    const int numCandidates = 3;
    string candidates[numCandidates ] = {
      "Trump",
      "Hiliary",
      "Burg"
    };
    int votes[numCandidates] = { 0 };
    From there, you can do
    Code:
    if ( entry >= 0 && entry < numCandidates ) {
      votes[entry]++;
    }
    Your output histogram then becomes a nested loop, instead of more copy/paste code.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Jumping in C++ Chapter 5 Question 2
    By etricity in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2014, 02:18 AM
  2. Jumping into C++, Chapter 5 Question 1.
    By etricity in forum C++ Programming
    Replies: 7
    Last Post: 04-02-2014, 06:55 AM
  3. Jumping into C++ chapter 8 question 5
    By twigz in forum C++ Programming
    Replies: 5
    Last Post: 03-07-2014, 04:43 PM
  4. Question on Chapter 7 sample in the book
    By CMaker3 in forum C++ Programming
    Replies: 1
    Last Post: 07-31-2013, 05:39 AM
  5. K&R Chapter 1 - basic question
    By LienolCrazel in forum C Programming
    Replies: 1
    Last Post: 05-05-2013, 08:52 AM

Tags for this Thread