473,796 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Homework Help!

16 New Member
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;
}
Oct 24 '06 #1
7 2059
vninja
40 New Member
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 New Member
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
mastern200
16 New Member
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 Recognized Expert Moderator Expert
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.
Oct 25 '06 #5
mastern200
16 New Member
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 New Member
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;
}
Oct 30 '06 #7
vninja
40 New Member
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;
Oct 30 '06 #8

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

Similar topics

7
1772
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 writing it out a million times. please help me. i have to write a program that you type in the age and it stores it in a text file then i have to catagorize them and when i hit the read button it shows how many times 18-35 was typed in, then how...
2
2282
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 class for awhile and don't know what I'm doing. I have included my (probably wrong) answers for the first few questions. It would be great if someone could tell me what is missing or what I need to work on at least just for the first few. I...
11
1714
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 user inputs until it hits 100. At 100 it should stop. That's not the problem, in fact, that part works. It's the adding that isn't working. How can my program add 2 + 7 and come up with 14? I'm posting my code (so that you may all laugh). ...
1
1581
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 there will be. The user will be prompted to enter score 1, score 2, etc. After the scores are entered the computer will print the total number of points, the average score, and the number of scores. The program will ask the user if he/she wants...
5
2999
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 message. There should be a function call to restaurant() that accepts the message as an argument named menu and then displays the message using the pointer notation *(menu + i). b. Modify this restaurant() function to alter the address in message....
1
1565
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 corresponding value of type double. If the string supplied is not a valid number, it should return 0.0 as its result. Note that you can use the method Double.parseDouble() to do the hard work for you. ...
8
29171
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 classes: Card.java, Deck.java and DeckTester.java. The specification for each class is given below. Card.Java This is a simple class that represents a playing card. Card has two attributes: • rank which is a String that represents the value...
3
4237
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++) { Console.Beep(); ***************** //I have no idea what to put for 1 sec of silence }
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9535
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10467
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10021
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7558
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6802
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4130
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.