argh.... why is the percent field always 0.00?

This is a discussion on argh.... why is the percent field always 0.00? within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream.h> int main() { int Awbrey1; int Awbrey2; int Awbrey3; int Berlin1; int Berlin2; int Berlin3; cout<<"Number of ...

  1. #1
    Unregistered
    Guest

    argh.... why is the percent field always 0.00?

    Code:
    #include <iostream.h>
    
    int main()
    
    {
    	int Awbrey1;
    	int Awbrey2;
    	int Awbrey3;
    	
    	int Berlin1;
    	int Berlin2;
    	int Berlin3;
    
    	cout<<"Number of votes for Awbrey in New York: ";
    	cin>>Awbrey1;
    	cout<<"Number of votes for Berlin in New York: ";
    	cin>>Berlin1;
    	cout<<"Number of votes for Awbrey in New Jersey: ";
    	cin>>Awbrey2;
    	cout<<"Number of votes for Berlin in New Jersey: ";
    	cin>>Berlin2;
    	cout<<"Number of votes for Awbrey in Connecticut: ";
    	cin>>Awbrey3;
    	cout<<"Number of votes for Berlin in Connecticut: ";
    	cin>>Berlin3;
    	
    	long awbreytotal=(Awbrey1+Awbrey2+Awbrey3);
    	long berlintotal=(Berlin1+Berlin2+Berlin3);
    	long total=(awbreytotal+berlintotal);
    	double awbreypercent=double(((Awbrey1+Awbrey2+Awbrey3)/total)*100);
    	double berlinpercent=double(((Berlin1+Berlin2+Berlin3)/total)*100);
    
    	cout<<endl;
    	const int ColWidth=15;
    	cout.setf(ios::fixed);
    	cout.precision(2);
    
    	cout.setf(ios::left);
    	cout.width(ColWidth); cout<<"Name";
    	cout.setf(ios::right);
    	cout.width(ColWidth); cout<<"Votes";
    	cout.width(ColWidth); cout<<"Percent"<<endl;
    	
    	cout.setf(ios::left);
    	cout.width(ColWidth); cout<<"Awbrey";
    	cout.setf(ios::right);
    	cout.width(ColWidth); cout<<awbreytotal;
    	cout.width(ColWidth); cout<<awbreypercent<<endl;
    	
    	cout.setf(ios::left);
    	cout.width(ColWidth); cout<<"Berlin";
    	cout.setf(ios::right);
    	cout.width(ColWidth); cout<<berlintotal;
    	cout.width(ColWidth); cout<<berlinpercent<<endl;
    	
    	cout.setf(ios::left);
    	cout.width(ColWidth); cout<<"TOTAL VOTES";
    	cout.setf(ios::right);
    	cout.width(ColWidth); cout<<total<<endl;
    	return (0);
    }
    see the topic :9

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Change the lines that calculate the percent to

    double awbreypercent=((((double)Awbrey1+Awbrey2+Awbrey3)/total)*100);
    double berlinpercent=((((double)Berlin1+Berlin2+Berlin3)/total)*100);

    otherwise you're just doing integer division (which will have already been evaluated by the time you cast to double).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 24
    Last Post: 08-05-2010, 06:30 PM
  2. Please critique and suggest improvements for parser
    By Queue in forum C Programming
    Replies: 14
    Last Post: 09-28-2006, 09:28 PM
  3. Line Counting
    By 00Sven in forum C Programming
    Replies: 26
    Last Post: 04-02-2006, 09:59 PM
  4. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 03:41 AM
  5. Profiler for g++
    By SilentStrike in forum Linux Programming
    Replies: 5
    Last Post: 04-30-2002, 12:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21