Homework Help!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mastern200
    New Member
    • Oct 2006
    • 16

    Homework Help!

    For Homework, i have to debug a program (I am a noob!) it is a voting program where you put in the amount of votes a certain candidate gets and evaluates by showing how many votes, what percentage of votes, and total. Problem is that it rounds everything and doesnt show what it is supposed to show. I got this far... Please help!



    #include <iostream>
    #include "string.h"
    using namespace std;

    int main()
    {
    string candidate1 = "";
    string candidate2 = "";
    string candidate3 = "";
    int votes1, votes2, votes3, total;
    float percent1, percent2, percent3, percent;
    cout<<"--Vote Analysis Program--\n";
    candidate1 = "Kerry";
    candidate2 = "Bush";
    candidate3 = "Working";
    candidate2[8] = 'h';
    cout<<"Enter votes for "<<candidate1<< ": ";
    cin>>votes1;
    cout<<"Enter votes for "<<candidate2<< ": ";
    cin>>votes2;
    cout<<"Enter votes for "<<candidate3<< ": ";
    cin>>votes3;
    cout<<endl;
    total = votes1 + votes2 + votes3;
    percent1 = 100 * votes1 / total;
    percent2 = 100 * votes2 / total;
    percent3 = 100 * votes3 / total;
    cout<< "Candidate : \t\t"<<candidat e1<<endl;
    cout<<"Votes: \t\t\t"<<votes1 <<endl;
    cout<<"Percenta ge: \t\t"<<percent1 <<endl<<endl;
    cout<< "Candidate : \t\t"<<candidat e2<<endl;
    cout<<"Votes: \t\t\t"<<votes2 <<endl;
    cout<<"Percenta ge: \t\t"<<percent2 <<endl<<endl;
    cout<< "Candidate : \t\t"<<candidat e3<<endl;
    cout<<"Votes: \t\t\t"<<votes3 <<endl;
    cout<<"Percenta ge: \t\t"<<percent3 <<endl<<endl;
    cout<<"Total Votes: \t\t"<<static_c ast<float>(perc ent1/100 * total)
    + (percent2/100 * total) + (percent3/100 * total) <<endl<<endl;
    cout<<"Total Percentage: \t"<<(percent 1 + percent2+ percent3)
    <<endl;
    cout<<endl;

    cin.get();
    cin.get();
    return 0;
    }
  • vninja
    New Member
    • Oct 2006
    • 40

    #2
    sounds like an data type problem to me. i'd suggest that you change the
    "INT" with "DOUBLE"
    because int will just give integers and truncate (not round) any decimal number
    double will allow any real number to be used with many decimal places.

    Comment

    • vninja
      New Member
      • Oct 2006
      • 40

      #3
      if you need to use an "INT" data type its ok to use for the votes but change the total to a "DOUBLE" or "FLOAT" and then static cast the votes in the percentage calculation to a "DOUBLE/FLOAT" so that you will get a percent and not a truncated percent.
      (unfortunatly i just had to reload windows on my comp so i'm still looking for my C++ builder so i can't say for 100% but i'm 95% certain that this will fix the challenge)

      Comment

      • mastern200
        New Member
        • Oct 2006
        • 16

        #4
        what the teacher told me is that the votes and the total must be ints. i tried replacing the percents with both doubles and floats and the static cast with both doubles and float (every combination i think) and it still didnt work...

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          The problem is definately to do with the variable types, in this code line

          percent1 = 100 * votes1 / total;

          votes1 and total are both ints, 100 is an integer constant so the calculation is performed in integer maths. To get the correct result you need to force the calculation into floating point maths, if you tried this

          percent1 = static_cast<flo at>(100 * votes1 / total);

          you wont have corrected the problem because the calculation is still in integer maths you are just casting the result to a float you need to do 1 of the folowing

          percent1 = 100. * votes1 / total;

          percent1 = 100 * static_cast<flo at>(votes1) / total;

          percent1 = 100 * votes1 / static_cast<flo at>(total);

          either of these 3 will force the calculation into floating point maths and will give the correct result.

          Comment

          • mastern200
            New Member
            • Oct 2006
            • 16

            #6
            OK thank you, but it gives me this:

            --Vote Analysis Program--
            Enter votes for Kerry: 1
            Enter votes for Bush: 1
            Enter votes for Working: 1

            Candidate : Kerry
            Votes: 1
            Percentage: 33

            Candidate : Bush
            Votes: 1
            Percentage: 33

            Candidate : Working
            Votes: 1
            Percentage: 33

            Total Votes: 2.97

            Total Percentage: 99

            When it needs to be like this:
            //--Vote Analysis Program--
            //Enter votes for Kerry: 1
            //Enter votes for Bush: 1
            //Enter votes for Working: 1
            //
            //Candidate : Kerry
            //Votes: 1
            //Percentage: 33.3333

            //Candidate : Bush
            //Votes: 1
            //Percentage: 33.3333

            //Candidate : Working
            //Votes: 1
            //Percentage: 33.3333

            //Total Votes: 3

            //Total Percentage: 100

            //Press any key to continue


            This is the code right now:
            Code:
            #include <iostream>
            #include "string.h"
            using namespace std;
            
            int main()
            {
                string candidate1 = "";
                string candidate2 = "";
                string candidate3 = "";
            	int votes1, votes2, votes3, total;
            	float percent1, percent2, percent3, percent;
            	cout<<"--Vote Analysis Program--\n";
            	candidate1 = "Kerry";
            	candidate2 = "Bush";
            	candidate3 = "Working";
            	candidate2[8] = 'h';
            	cout<<"Enter votes for "<<candidate1<<": ";
            	cin>>votes1;
            	cout<<"Enter votes for "<<candidate2<<": ";
            	cin>>votes2;
            	cout<<"Enter votes for "<<candidate3<<": ";
            	cin>>votes3;
            	cout<<endl;
            	total = votes1 + votes2 + votes3;
            	percent1 = static_cast<float> (100 * votes1 / total);
            	percent2 = static_cast<float> (100 * votes2 / total);
            	percent3 = static_cast<float> (100 * votes3 / total);
            	cout<< "Candidate : \t\t"<<candidate1<<endl;
            	cout<<"Votes: \t\t\t"<<votes1<<endl;
            	cout<<"Percentage: \t\t"<<percent1<<endl<<endl;
            	cout<< "Candidate : \t\t"<<candidate2<<endl;
            	cout<<"Votes: \t\t\t"<<votes2<<endl;
            	cout<<"Percentage: \t\t"<<percent2<<endl<<endl;
            	cout<< "Candidate : \t\t"<<candidate3<<endl;
            	cout<<"Votes: \t\t\t"<<votes3<<endl;
            	cout<<"Percentage: \t\t"<<percent3<<endl<<endl;
            	cout<<"Total Votes: \t\t" << static_cast<float>(percent1/100 * total)
                 + (percent2/100 * total) + (percent3/100 * total) <<endl<<endl;
            	cout<<"Total Percentage: \t"<<(percent1	+ percent2+ percent3)
            		<<endl;
            	cout<<endl;
            	
            	cin.get();
            	cin.get();
            	return 0;
            }

            Comment

            • vninja
              New Member
              • Oct 2006
              • 40

              #7
              like banfa said: your not static casting the right way. (you can't div int by int and then static cast but you have to div static cast by int of int by static cast.)

              try this code:



              #include <iostream>
              #include "string.h"
              using namespace std;

              int main()
              {
              string candidate1 = "";
              string candidate2 = "";
              string candidate3 = "";
              int votes1, votes2, votes3, total;
              float percent1, percent2, percent3, percent;
              cout<<"--Vote Analysis Program--\n";
              candidate1 = "Kerry";
              candidate2 = "Bush";
              candidate3 = "Working";
              candidate2[8] = 'h';
              cout<<"Enter votes for "<<candidate1<< ": ";
              cin>>votes1;
              cout<<"Enter votes for "<<candidate2<< ": ";
              cin>>votes2;
              cout<<"Enter votes for "<<candidate3<< ": ";
              cin>>votes3;
              cout<<endl;
              total = votes1 + votes2 + votes3;
              percent1 = static_cast<flo at> (votes1) / total*100;
              percent2 = static_cast<flo at> (votes2) / total*100;
              percent3 = static_cast<flo at> (votes3) / total*100;
              cout<< "Candidate : \t\t"<<candidat e1<<endl;
              cout<<"Votes: \t\t\t"<<votes1 <<endl;
              cout<<"Percenta ge: \t\t"<<percent1 <<endl<<endl;
              cout<< "Candidate : \t\t"<<candidat e2<<endl;
              cout<<"Votes: \t\t\t"<<votes2 <<endl;
              cout<<"Percenta ge: \t\t"<<percent2 <<endl<<endl;
              cout<< "Candidate : \t\t"<<candidat e3<<endl;
              cout<<"Votes: \t\t\t"<<votes3 <<endl;
              cout<<"Percenta ge: \t\t"<<percent3 <<endl<<endl;
              cout<<"Total Votes: \t\t" << static_cast<flo at>(percent1/100 * total)
              + (percent2/100 * total) + (percent3/100 * total) <<endl<<endl;
              cout<<"Total Percentage: \t"<<(percent 1+ percent2+ percent3)
              <<endl;
              cout<<endl;

              cin.get();
              cin.get();
              return 0;
              }

              Comment

              • vninja
                New Member
                • Oct 2006
                • 40

                #8
                here is my output

                --Vote Analysis Program--
                Enter votes for Kerry: 498
                Enter votes for Bush: 356
                Enter votes for horking: 256

                Candidate : Kerry
                Votes: 498
                Percentage: 44.8649

                Candidate : Bush
                Votes: 356
                Percentage: 32.0721

                Candidate : horking
                Votes: 256
                Percentage: 23.0631

                Total Votes: 1110

                Total Percentage: 100


                if you need the percentage in a two decimal form i'd suggest using
                cout<<"Percenta ge: \t\t"<<setprici sion(2)<<percen t3<<"%"<<endl<< endl;

                Comment

                Working...