• Advertisement
Sign in to follow this  

C++ frustration

This topic is 2646 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


//
// Author: Lauren Settle
//
//Program: Local Election
//
// This program prompts the user to input the last names of 5 canidates in a local election and the number of votes they recieved.
// The program then outputs the canidates name, the number of votes recieved, the percent of total votes, and output the winner.
//

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;


void printHeading();

void initialize (int totalVotes[], int percentVotes[], const int numberOfCanidates);

void getCanidatesName (string canidatesName[], const int numberOfCanidates);

void sortCanidatesName (string canidatesName[], const int numberOfCanidates);

int binSearch (string canidatesName[], const int numberOfCanidates, string name);

void processVotes (string canidatesName[], int noOfVotes[], const int numberOfCanidates);

void percentVote (int noOfVotes[], int totalVotes[], int percentVotes[], const int numberOfCanidates);

void printResults (string canidatesName, int noOfVotes[], int totalVotes[], int percentVotes[], const int numberOfCanidates);

int main()
{
const int numberOfCanidates = 5;
string canidatesName[numberOfCanidates];
int noOfVotes[numberOfCanidates];
int percentVotes[numberOfCanidates];
int totalVotes;

getCanidatesName (canidatesName, numberOfCanidates);
sortCanidatesName (canidatesName, numberOfCanidates);
initialize (totalVotes, percentVotes, numberOfCanidates);
processVotes (canidatesName, noOfVotes, numberOfCanidates);
percentVote (noOfVotes, totalVotes, percentVotes, numberOfCanidates);
printHeading ();
printResults (canidatesName, noOfVotes, totalVotes, percentVotes, numberOfCanidates);

/* Scaffolding code for testing purposes */
cin.ignore(256, '\n');
cout << "Press ENTER to continue..." << endl;
cin.get();
/* End Scaffolding */
return 0;
}

void getCanidatesName (string canidatesName[], const int numberOfCanidates)
{
int i;

for (i = 0; i < numberOfCanidates; i++)
cin >> canidatesName[i];
}
void sortCanidatesName (string canidatesName[], const int numberOfCanidates)
{
int i, j;
int min;

for (i = 0; i < numberOfCanidates - 1; i++)
{
min = i;

for (j = i + 1; j < numberOfCanidates; j++)
if (canidatesName[j] < canidatesName[min])
min = j;

canidatesName[i].swap(canidatesName[min]);
}
}
void initialize (int totalVotes[], int percentVotes[], const int numberOfCanidates)
{
int i;

for (i = 0; i < numberOfCanidates; i++)
totalVotes[i] = 0;

for (i = 0; i < numberOfCanidates; i++)
percentVotes[i] = 0;
}
int binSearch (string canidatesName[], const int numberOfCanidates, string name)
{
int first, last, mid;
bool found;
first = 0;
last = numberOfCanidates - 1;
found = false;

while (!found && first <= last)
{
mid = (first + last) / 2;

if (canidatesName[mid] == name)
found = true;
else if (canidatesName[mid] > name)
last = mid - 1;
else
first = mid + 1;
}
if (found)
return mid;
else
return -1;
}
void processVotes (string canidatesName[], int noOfVotes[], const int numberOfCanidates)
{
string candName;
int no_OfVotes;
int loc;

cin >> candName >> no_OfVotes;

while (cin)
{
loc = binSearch (canidatesName, numberOfCanidates, candName);

if (loc != -1)
noOfVotes[loc] = noOfVotes[loc] + no_OfVotes;
cin >> candName >> no_OfVotes;
}
}
void percentVote (int noOfVotes[], int totalVotes[], int percentVotes[], const int numberOfCanidates)
{
int i;

for (i = 0; i < numberOfCanidates; i++)
totalVotes[i] = totalVotes[i] + noOfVotes[i];

for (i = 0; i < numberOfCanidates; i++)
percentVotes[i] = noOfVotes[i] / totalVotes[i];

}
void printHeading()
{
cout << " -----------Election Results------------ " << endl;
cout << " Canidates Number Percent " << endl;
cout << " Name of Votes of Votes " << endl;
cout << " -------------------------------------------------- " << endl;
}
void printResults (string canidatesName, int noOfVotes[], int totalVotes[], int percentVotes[], const int numberOfCanidates)
{
int i;
int largestVotes = 0;
int winLoc = 0;

for (i = 0; i < numberOfCanidates; i++)
{
if (largestVotes < noOfVotes[i])
{
largestVotes = noOfVotes[i];
winLoc = i;
}
cout << left;
cout << setw(9) << canidatesName[i] << " ";
cout << right;
cout << setw(8) << noOfVotes[i] << " ";
cout << setw(6) << percentVotes[i] << endl;
}
cout << endl << endl << "The Winner Is: " << canidatesName[winLoc] << endl << endl;
}





C:\Users\User\Documents\csc\chp9.num.7.cpp: In function `int main()':
C:\Users\User\Documents\csc\chp9.num.7.cpp:43: error: invalid conversion from `int' to `int*'
C:\Users\User\Documents\csc\chp9.num.7.cpp:43: error: initializing argument 1 of `void initialize(int*, int*, int)'

C:\Users\User\Documents\csc\chp9.num.7.cpp:45: error: invalid conversion from `int' to `int*'
C:\Users\User\Documents\csc\chp9.num.7.cpp:45: error: initializing argument 2 of `void percentVote(int*, int*, int*, int)'
C:\Users\User\Documents\csc\chp9.num.7.cpp:47: error: conversion from `std::string*' to non-scalar type `std::string' requested

These are the errors I keep getting. I have no clue what they mean....let alone how to correct them.

[edit: Source tags are your friend -SiCrane]

[Edited by - SiCrane on October 23, 2010 8:31:40 AM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by borogirl09
C:\Users\User\Documents\csc\chp9.num.7.cpp: In function `int main()':
C:\Users\User\Documents\csc\chp9.num.7.cpp:43: error: invalid conversion from `int' to `int*'
C:\Users\User\Documents\csc\chp9.num.7.cpp:43: error: initializing argument 1 of `void initialize(int*, int*, int)'


You're trying to pass an integer in as the first argument to initialize, when it expects a pointer to an integer.

Quote:
Original post by borogirl09
C:\Users\User\Documents\csc\chp9.num.7.cpp:45: error: invalid conversion from `int' to `int*'
C:\Users\User\Documents\csc\chp9.num.7.cpp:45: error: initializing argument 2 of `void percentVote(int*, int*, int*, int)'


Same here, except with the second parameter of percentVote.

Quote:
Original post by borogirl09
C:\Users\User\Documents\csc\chp9.num.7.cpp:47: error: conversion from `std::string*' to non-scalar type `std::string' requested


You're trying to convert a pointer to an std::string on this line to an std::string.


More info: You're using int [] as a parameter in a lot of places, which C++ treats the same as int * when passing arguments. When you pass in an integer value, such as totalVote, when it expects an array or a pointer, the compiler will complain. You need to decide if you need an array or a scalar value and adjust your code accordingly. Based on your codebase, it seems like you should change your totalVotes variable to consistently be an array.

Share this post


Link to post
Share on other sites
In addition to Rycross's response --

Based on your reaction to the errors, it would be worth your time to find a good resource to understand how pointers and arrays work (GD lists a bunch in its "Resources" section). It's not a concept one can understand just through debugging, and the investment of time will pay back very quickly.

Share this post


Link to post
Share on other sites
the variable totalVotes is defined as a single integer in main, but it is then used as if it were an array of ints during the calls to initialize() and percentVote(). The value being passed in is probably supposed to be noOfVotes.

In the function printResults(), the first variable is defined to be a single string, but within the function it is treated like an array. The function header needs to express the arrayness. Also in this function, totalVotes is treated as if it were an array instead of a single value.

[Edited by - AngleWyrm on October 23, 2010 6:03:24 AM]

Share this post


Link to post
Share on other sites
Just some advice on how to improve your general approach to programming whilst learning:

When it comes to writing code using something you haven't used before (such as pointers), test it does what you think it should do and try to understand all aspects of it. Research.

Don't write huge blocks of code before hitting the compile button: Reason being is that if a compile fails you may find you've been taking the wrong approach to something and may have to do a lot of it again.

Get familiar with the debugger: The output from the debugger in this case points out the lines with the problem. You need to learn what it's saying.
Let's break this one down:

C:\Users\User\Documents\csc\chp9.num.7.cpp: In function `int main()':




There's a problem in the main function (more info to follow)

C:\Users\User\Documents\csc\chp9.num.7.cpp:43: error: invalid conversion from `int' to `int*'
C:\Users\User\Documents\csc\chp9.num.7.cpp:43: error: initializing argument 1 of `void initialize(int*, int*, int)'




The compiler can't convert from an int to an int pointer on line 43.
The problem is the first argument of the initialize function.
In other words, you are trying to pass an int to a function that expects an int pointer.

Don't get frustrated, it doesn't help. If you don't understand something take a step back and approach it as logically as you can.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement