0

Having trouble copying a vector<string> to ostream && program runs pretty slow

Hi all, Program Description: I've written a program that take an int (n) input, initializes a vector of (n) elements (candidate), then runs two functions on each element to assign a name (string) and a vote count (integer). Then the program sums the total number of votes and assigns each element of the vector a percentage (float) of votes. The program also calculates the max votes of any one (candidate). The program initializes a vector<string> to store the names of candidates with the max number of votes (in case of a draw). Then the program prints the results out and it should print out a string with the contents of the winning candidate vector as well. Questions for y'all: 1. However, the program is not outputting the string of the winning candidates. 2. Additionally, the entire program runs pretty slowly despite few entries. Link to code: https://code.sololearn.com/ct1dz49SCJfF/#cpp Any insights into these two issues would be helpful.

20th Jun 2017, 10:57 PM
Joseph P French
Joseph P French - avatar
7 Respostas
+ 2
Ya I have been testing it, but haven't really been able to look at all the code yet. Busy with household projects. There is definitely more wrong with it than just the input stream issue. Which, might not actually be an issue, but still a better practice. I would switch your cin statements to... string str; // place this var up towards the top of main where it is scope for all the cin statements. getline(cin, str); then convert and assign str to your integer variables where needed like: var = atoi(str.c_str()); This should get rid of the '\n' issue if there is one. I'll look the code over in a bit when I can if no one else has beat me too it yet. 😉
21st Jun 2017, 12:24 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
You need to flush your input stream. Your first cin is an int type which leaves the remaining newline character in the stream. Same with the other cin int types.
20th Jun 2017, 11:19 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Ok so first you can change these lines: vector<string> vecMaxVotes; //winning vote names printResults(candList, vecMaxVotes, maxVotes); //print results To just this: printResults(candList, maxVotes); // No need to pass an empty vector when it should be declared in the function itself. Then you can change your printResults function from this: //print the results void printResults(vector<candidate> obj, vector<string> stringObj, int intObj) { std::cout << std::setw(11) << "Name"; std::cout << std::setw(11) << "Votes"; std::cout << std::setw(11) << "Percentage" << endl; for(int i = 0; i < 5; i++) { obj[i].print(); } cout << endl; //calculate winner for (unsigned int i = 0; i < obj.size(); i++) { if (obj[i].getVotes() == intObj) { stringObj.push_back(obj[i].getName()); } } //print winner printWinner(stringObj); } To this: //print the results void printResults(vector<candidate> obj, int intObj) { vector<string> stringObj; // Reduce your parameters by moving this inside the function. std::cout << std::setw(11) << "Name"; std::cout << std::setw(11) << "Votes"; std::cout << std::setw(11) << "Percentage" << endl; int v_size = obj.size(); // Store the size of the vector in a variable for optimization. for(int i = 0; i < v_size; i++) { // Your issue is here instead of 5 you should get the size of the vector. obj[i].print(); } cout << endl; //calculate winner for (unsigned int i = 0; i < v_size; i++) { // change it here too which you had correct lol if (obj[i].getVotes() == intObj) { stringObj.push_back(obj[i].getName()); } } //print winner printWinner(stringObj); } See working code here: https://goo.gl/J28yvL
21st Jun 2017, 1:58 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Yup NP. Looks like your cin clear function is working too.
21st Jun 2017, 2:05 AM
ChaoticDawg
ChaoticDawg - avatar
0
Hi @chaoticdog, thanks for the response (not sure why the downvote?). I implemented a function to flush the input stream (I think? TBO first time I've ever heard about that). Still not getting the print out of the winner string I'm looking for: printWinner(stringObj); //Line 142 Any additional advice is much appreciated.
21st Jun 2017, 12:15 AM
Joseph P French
Joseph P French - avatar
0
Thank you, eagerly awaiting. I wonder if I should implement an if statement to check for a tie before handling one as I do (w/ a vector of strings). It would simplify the printout (which I got to work, but changed a lot of code and now it doesn't). However, in the event there was one, I'd have to handle it anyways...
21st Jun 2017, 12:47 AM
Joseph P French
Joseph P French - avatar
0
@chaoticdog, I guess that '5' was a leftover from an earlier iteration before I wrote it to accept 'n' elements. Works perfectly now! Thanks a million!
21st Jun 2017, 2:04 AM
Joseph P French
Joseph P French - avatar