| | |
c++ strings
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2005
Posts: 9
Reputation:
Solved Threads: 0
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:
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
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstring> #include <iomanip> using namespace std; int totalVote(int votes[]); int win_index(int votes[], int); int main() { string candidate[5]; int votes[5]; int total, j, win; cout<<"Please enter the candidate and the number of votes they received."<<endl; for (j=0; j<5; j++) { cin>> candidate[j]>> votes[j]; } cout<<setw(20)<<left<<"Candidate"<<setw(20)<<"Votes Received"<<setw(20)<<"% of Total Votes"<<endl; cout<<"---------------------------------------------------------"<<endl<<endl; total= totalVote(votes); for (j=0; j<5; j++) { cout<<setw(20)<<left<<candidate[j]<<setw(20)<<votes[j]<<setw(20)<<(static_cast<float>(votes[j])/total)*100<<endl; } cout<<"Total Votes: "<<total<<endl; win = win_index (votes, 5); cout<<"The winner is: "<<candidate[win]<<endl; return 0; } int totalVote(int votes[]) { int i, total; total=0; for (i=0; i<5; i++) { total=total+votes[i]; } return total; } int win_index(int votes[], int) { int i, j, win; i=0; for (j=0; j<5; j++) { if (votes[j]<i) win= i; else win= votes[j]; } return win; }
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
#include <cstring> C++ Syntax (Toggle Plain Text)
#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
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...
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...
![]() |
Similar Threads
- Comparing Strings in C# (C#)
- Please help me compare strings (Java)
- reversing two strings (C)
- C++ handling of strings in a boolean expression (C++)
- Compare strings... (C++)
- comparing two strings with linear search.. (Java)
- JSP and Oracle (JSP)
Other Threads in the C++ Forum
- Previous Thread: Fork and exec to run external needed app
- Next Thread: help with updating a text file using C++
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






