Originally Posted by
liquidcourage1
Ok... assuming I change all the 'void's to 'int'...
then at the end of the code... how do I return the value(total of EACH district) to main and then figure out overall winner?
You can only return once from a function (because a return means that the function is finished). Much in the same way that an operation on a calculator will only give you one result.
[EDIT - Meaning : You can have multiple exit points, but only one of those exit points will actually be called]
What information are you specifically interested in giving back to main()? Your vote() function calculates the winner, and stores how many votes the winner had. at the moment you're returning the number of votes held by the winner.
If there's other data you need to pass back, then you need to alter the way your program works (it won't hurt to break down the program into smaller functions - it's usually very clunky to put all your input, processes and output all together like that)
I'm a little slow to catch what you're saying... I'll highlight what I've changed so far...
Maybe try a simpler example so that you can experiment with functions and see exactly what is going on. The following should work if you copy and paste into your compiler:
Code:
#include <iostream>
using namespace std;
int maths()
{
int answer;
answer = 2+2;
return answer;
}
int main()
{
int myNumber;
myNumber = maths();
cout << "myNumber is: " << myNumber;
cin.get();
}
it should be pretty clear to see what's going on - the function performs an operation (2 + 2), and returns it at the end. then myNumber is assigned the output from the maths() function.
Better still - let the main function provide the numbers to be added together -
Code:
#include <iostream>
using namespace std;
int addition(int a, int b)
{
int answer;
answer = a+b;
return answer;
}
int main()
{
int myNumber;
myNumber = addition(4, 5);
cout << "myNumber is: " << myNumber;
cin.get();
}
These 2 snippets are essentially doing the same operation, but you can see the 2nd is more flexible than the first - I hope this gives you a clearer idea of how functions work, and how they can interact with main() .. (or any other part of a program)
Code:
int main()
{
cout<<"Welcome to the election gizmo\n\n";
int vote();
int vote();
return 0;
}
When you call a function, don't put the data type right in front of it. the program probably thinks you're declaring the functions (Hence why it doesn't actually call them when you run) - They're already declared by the prototypes before main(), so what you want to do is get rid of the int - so your program will call them instead.
You will have 2 calls to vote() - meaning you'll get 2 values returned, you need to make somewhere to store those variables.
either do something like this:
Code:
int result1, result2;
result1 = vote();
result2 = vote();
or maybe this..
Code:
int result1 = vote();
int result2 = vote();
Both have the same effect.