c++ strings

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2005
Posts: 9
Reputation: Scuuba is an unknown quantity at this point 
Solved Threads: 0
Scuuba Scuuba is offline Offline
Newbie Poster

c++ strings

 
0
  #1
May 16th, 2005
ok, so I'm supposed to write a program which allows the user to input 5 candidate names and the number of votes they receive. The program should the spit out the names, number of votes, percentage of votes received, the total votes, and the winner of the election. This is the code:

  1. #include <iostream>
  2. #include <cstring>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. int totalVote(int votes[]);
  9. int win_index(int votes[], int);
  10.  
  11.  
  12. int main()
  13. {
  14.  
  15. string candidate[5];
  16. int votes[5];
  17.  
  18.  
  19. int total, j, win;
  20.  
  21.  
  22.  
  23. cout<<"Please enter the candidate and the number of votes they received."<<endl;
  24.  
  25.  
  26. for (j=0; j<5; j++)
  27. {
  28. cin>> candidate[j]>> votes[j];
  29. }
  30.  
  31.  
  32.  
  33. cout<<setw(20)<<left<<"Candidate"<<setw(20)<<"Votes Received"<<setw(20)<<"% of Total Votes"<<endl;
  34. cout<<"---------------------------------------------------------"<<endl<<endl;
  35.  
  36.  
  37. total= totalVote(votes);
  38.  
  39. for (j=0; j<5; j++)
  40. {
  41. cout<<setw(20)<<left<<candidate[j]<<setw(20)<<votes[j]<<setw(20)<<(static_cast<float>(votes[j])/total)*100<<endl;
  42. }
  43.  
  44. cout<<"Total Votes: "<<total<<endl;
  45.  
  46. win = win_index (votes, 5);
  47. cout<<"The winner is: "<<candidate[win]<<endl;
  48.  
  49. return 0;
  50.  
  51.  
  52. }
  53.  
  54.  
  55. int totalVote(int votes[])
  56. {
  57. int i, total;
  58.  
  59. total=0;
  60.  
  61. for (i=0; i<5; i++)
  62. {
  63. total=total+votes[i];
  64. }
  65.  
  66. return total;
  67.  
  68. }
  69.  
  70.  
  71.  
  72. int win_index(int votes[], int)
  73. {
  74.  
  75. int i, j, win;
  76.  
  77. i=0;
  78.  
  79. for (j=0; j<5; j++)
  80. {
  81. if (votes[j]<i)
  82. win= i;
  83. else
  84. win= votes[j];
  85. }
  86.  
  87. return win;
  88. }

The compiler is telling me that: error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >.

Does anyody know what this means?
Also, I don't think the last function is quite right... any suggestions?

Thanks
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,433
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 249
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: c++ strings

 
0
  #2
May 16th, 2005
#include <cstring>
I think you meant this one.
  1. #include <string>
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 9
Reputation: Scuuba is an unknown quantity at this point 
Solved Threads: 0
Scuuba Scuuba is offline Offline
Newbie Poster

Re: c++ strings

 
0
  #3
May 16th, 2005
yep, that did it, however when I run it, it says the memory can not be read. Do you know what this means?
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: c++ strings

 
0
  #4
May 16th, 2005
What's the point of the int parameter in the win_index function? You should give it a name and use it, or remove it entirely..

Also, it would be far more efficient to add up the total votes as you are receiving them in then to iterate through them later...

Also, win_index function doesn't make any sense to begin with - the winner is always the last candidate unless one of your candidates has negative votes...
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 9
Reputation: Scuuba is an unknown quantity at this point 
Solved Threads: 0
Scuuba Scuuba is offline Offline
Newbie Poster

Re: c++ strings

 
0
  #5
May 16th, 2005
The int parameter has scince been removed. My professor gave me a shell to work with and it had the function with 2 parameters in it.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC