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

  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, 05:30 PM
  2. Please critique and suggest improvements for parser
    By Queue in forum C Programming
    Replies: 14
    Last Post: 09-28-2006, 08:28 PM
  3. Line Counting
    By 00Sven in forum C Programming
    Replies: 26
    Last Post: 04-02-2006, 08: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, 02:41 AM
  5. Profiler for g++
    By SilentStrike in forum Linux Programming
    Replies: 5
    Last Post: 04-29-2002, 11:01 PM