473,414 Members | 1,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

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"<<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;
}
Oct 24 '06 #1
7 2040
vninja
40
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.
Oct 24 '06 #2
vninja
40
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)
Oct 24 '06 #3
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...
Oct 24 '06 #4
Banfa
9,065 Expert Mod 8TB
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<float>(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<float>(votes1) / total;

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

either of these 3 will force the calculation into floating point maths and will give the correct result.
Oct 25 '06 #5
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:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include "string.h"
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     string candidate1 = "";
  8.     string candidate2 = "";
  9.     string candidate3 = "";
  10.     int votes1, votes2, votes3, total;
  11.     float percent1, percent2, percent3, percent;
  12.     cout<<"--Vote Analysis Program--\n";
  13.     candidate1 = "Kerry";
  14.     candidate2 = "Bush";
  15.     candidate3 = "Working";
  16.     candidate2[8] = 'h';
  17.     cout<<"Enter votes for "<<candidate1<<": ";
  18.     cin>>votes1;
  19.     cout<<"Enter votes for "<<candidate2<<": ";
  20.     cin>>votes2;
  21.     cout<<"Enter votes for "<<candidate3<<": ";
  22.     cin>>votes3;
  23.     cout<<endl;
  24.     total = votes1 + votes2 + votes3;
  25.     percent1 = static_cast<float> (100 * votes1 / total);
  26.     percent2 = static_cast<float> (100 * votes2 / total);
  27.     percent3 = static_cast<float> (100 * votes3 / total);
  28.     cout<< "Candidate : \t\t"<<candidate1<<endl;
  29.     cout<<"Votes: \t\t\t"<<votes1<<endl;
  30.     cout<<"Percentage: \t\t"<<percent1<<endl<<endl;
  31.     cout<< "Candidate : \t\t"<<candidate2<<endl;
  32.     cout<<"Votes: \t\t\t"<<votes2<<endl;
  33.     cout<<"Percentage: \t\t"<<percent2<<endl<<endl;
  34.     cout<< "Candidate : \t\t"<<candidate3<<endl;
  35.     cout<<"Votes: \t\t\t"<<votes3<<endl;
  36.     cout<<"Percentage: \t\t"<<percent3<<endl<<endl;
  37.     cout<<"Total Votes: \t\t" << static_cast<float>(percent1/100 * total)
  38.      + (percent2/100 * total) + (percent3/100 * total) <<endl<<endl;
  39.     cout<<"Total Percentage: \t"<<(percent1    + percent2+ percent3)
  40.         <<endl;
  41.     cout<<endl;
  42.  
  43.     cin.get();
  44.     cin.get();
  45.     return 0;
  46. }
  47.  
Oct 25 '06 #6
vninja
40
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<float> (votes1) / total*100;
percent2 = static_cast<float> (votes2) / total*100;
percent3 = static_cast<float> (votes3) / total*100;
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;
}
Oct 30 '06 #7
vninja
40
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<<"Percentage: \t\t"<<setpricision(2)<<percent3<<"%"<<endl<<endl;
Oct 30 '06 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: tjshadyluver | last post by:
ok i have gotten this damn project almost done but i am frustrated on this one step. getting 18-35 together and 36-50 and so on. here is my code how can i get them combined in one line instead of...
2
by: N3TB1N | last post by:
Let me try again. I could use some help with this assignment, even though my teacher does not grade assignments.but because I need to know this stuff for a test very soon, but haven't been in...
11
by: spawn | last post by:
but I've been struggling with this for far too long and I'm about to start beating my head against the wall. My assignment seemed simple: create a program that will cacluate the running total of...
1
by: mastern200 | last post by:
I need some homework help with an assignment due wed. I need to make a program where in this program calculates the average of a set of test scores. The program will ask the user how many scores...
5
by: karafire2003 | last post by:
I was wondering if someone can help me with my homework. here's the assignment: a. Write a C program that has a declaration in main() to store the string "What's for lunch?" into an array named...
1
by: itgetsharder | last post by:
Hey, i was wondering if anyone could help me. i have two questions that i cannot complete for a homework assignment: This method should convert its parameter (a string like "3.1415") to the...
8
by: garyrowell | last post by:
I have been at this programme for hours trying to work out what is wrong. Any help would be very much appricated. Here is the breif I received. The program This week you are going to write three...
3
by: alireza6485 | last post by:
I need to write a C# program that asks for a pattern and for each pattern beeps one and goes silence for 1 second. This is my code: for (int i=0;i< pattern; i++) { ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.