I'm working on a program where I need to input a file with 5 candidates names and votes, and output the name, votes, and the percentage of total votes that candidate received, as well as the overall winner. Right now I've been able to create some arrays and output the basic information, but I have two problems:

1). My output is showing

-858993460
Johnson 5000
Miller 4000
Duffy 6000
Robinson 2500
Ashtony 1800
Press any key to continue . . .

But, I don't know where the -858993460 is coming from...!?!

2). Where should I go from here?


Here is my code:

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

void main()
{
    int numCan = 0;
    string candidates[10];
    int votes[10];

    ifstream inFile;
   
    inFile.open("candidates.txt");

	while(inFile && numCan < 10)
    {
        cout << candidates[numCan] << " " << votes[numCan] << endl;
		numCan++;
        inFile >> candidates[numCan] >> votes[numCan];        
    }

    inFile >> candidates[numCan] >> votes[numCan];

    inFile.close();
}

Recommended Answers

All 9 Replies

But, I don't know where the -858993460 is coming from...!?!

2). Where should I go from here?

Read input, then print it?

Read input, then print it?

So, this is what I came up with:

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

void main()
{
    int i;
	int numCan = 0;
    string candidates[6];
    int votes[6];

    ifstream inFile;
   
    inFile.open("candidates.txt");

	for (i = 1; i <=5; i++)
    {
		inFile >> candidates[numCan] >> votes[totvote];
        cout << candidates[numCan] << " " << votes[totvote] << endl;      
    }
	numCan++; 

    inFile.close();
}

Would "numCam" and "totvote" be considered columns?

Would "numCam" and "totvote" be considered columns?

Maybe if both existed and they were used in a way that had meaning, like i (kinda).

Maybe if both existed and they were used in a way that had meaning, like i (kinda).

I guess my question would be...how do I add my votes? I'm assuming there is a fairly easy way to add the column of information...

here is the input file:
Johnson 5000
Miller 4000
Duffy 6000
Robinson 2500
Ashtony 1800

I guess my question would be...how do I add my votes? I'm assuming there is a fairly easy way to add the column of information...

First, get the input right. Then yes, adding is pretty simple.

First, get the input right. Then yes, adding is pretty simple.

My input is right....and I've adjusted for a matrix, and I'm trying to add the second column, but I'm not getting the right numbers....what's wrong?

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;


void main()
{
    int i, sum;
	int numCan = 0;
	int totvote = 0;
    string candidates[6];
    int votes[6];
	int matrix[6][2];
	int rows, cols;

    ifstream inFile;
   
    inFile.open("candidates.txt");

	for (i = 1; i <=5; i++)
    {
		inFile >> candidates[numCan] >> votes[totvote];
        cout << candidates[numCan] << " " << votes[totvote] << endl;      
    }
	numCan++; 

	for(rows=0;rows<6;rows++)
		for (cols=0;cols<3;cols++)
			inFile >> matrix[6][2];


	for (cols = 0; cols < 3; cols++)
	{
		sum = 0;

		for (rows=0;rows<6;rows++)
			sum = sum + matrix[6][2];

		cout << "Sum of Votes Column " << sum << endl;

	}

	cout << matrix[6][2] << endl;

	inFile.close();
}

My input is right....

Really? This isn't reading all of the data into element zero:

int numCan = 0;
   int totvote = 0;
   // ...
   for ( i = 1; i <=5; i++ )
   {
      inFile >> candidates[numCan] >> votes[totvote];
      cout << candidates[numCan] << " " << votes[totvote] << endl;
   }
   numCan++;

And then incrementing numCan to point to data you haven't read?

And then trying to reread the file into a non-existent array element?

for ( rows=0;rows<6;rows++ )
      for ( cols=0;cols<3;cols++ )
         inFile >> matrix[6][2];

With seemingly unused indices from the loop?

And then expecting more magic to happen with the non-existent array element matrix[6][2]?

for ( cols = 0; cols < 3; cols++ )
   {
      sum = 0;

      for ( rows=0;rows<6;rows++ )
         sum = sum + matrix[6][2];

      cout << "Sum of Votes Column " << sum << endl;

   }

   cout << matrix[6][2] << endl;

but I'm not getting the right numbers....what's wrong?

I'd say back up a bit. You might have been closer with your first post.

I don't know if you expect the compiler to read your mind with regard to some relationship between matrix and candidates and votes. You don't even need an array for this, except if you choose to read all data first and later print out the data and calculate the sum -- but you can print and sum on input.

[eidt]Possibly like this kinda thing:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
   string candidates[6];
   int votes[6];
   int sum = 0;
   ifstream inFile("candidates.txt");
   for ( int i = 0; i < 6; ++i )
   {
      if ( inFile >> candidates[i] >> votes[i] )
      {
         // sum
         // print
      }
      else
      {
         break;
      }
   }
   cout << "Total " << sum << endl;
}

Really? This isn't reading all of the data into element zero:

int numCan = 0;
   int totvote = 0;
   // ...
   for ( i = 1; i <=5; i++ )
   {
      inFile >> candidates[numCan] >> votes[totvote];
      cout << candidates[numCan] << " " << votes[totvote] << endl;
   }
   numCan++;

And then incrementing numCan to point to data you haven't read?

And then trying to reread the file into a non-existent array element?

for ( rows=0;rows<6;rows++ )
      for ( cols=0;cols<3;cols++ )
         inFile >> matrix[6][2];

With seemingly unused indices from the loop?

And then expecting more magic to happen with the non-existent array element matrix[6][2]?

for ( cols = 0; cols < 3; cols++ )
   {
      sum = 0;

      for ( rows=0;rows<6;rows++ )
         sum = sum + matrix[6][2];

      cout << "Sum of Votes Column " << sum << endl;

   }

   cout << matrix[6][2] << endl;

I'd say back up a bit. You might have been closer with your first post.

I don't know if you expect the compiler to read your mind with regard to some relationship between matrix and candidates and votes. You don't even need an array for this, except if you choose to read all data first and later print out the data and calculate the sum -- but you can print and sum on input.

[eidt]Possibly like this kinda thing:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
   string candidates[6];
   int votes[6];
   int sum = 0;
   ifstream inFile("candidates.txt");
   for ( int i = 0; i < 6; ++i )
   {
      if ( inFile >> candidates[i] >> votes[i] )
      {
         // sum
         // print
      }
      else
      {
         break;
      }
   }
   cout << "Total " << sum << endl;
}

Oh...Ok! So, something like this to add the sum, and then the total would be all values of votes....Thanks! Next question, is how would I insert a formula that would take each value of votes and determine it's percentage of the total?


#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;

int main()
{
   string candidates[6];
   int votes[6];
   int sum = 0;
   ifstream inFile("candidates.txt");

   cout << "Candidate......Votes" << endl;

   for ( int i = 0; i < 6; ++i )
   {
	  
      if ( inFile >> candidates[i] >> votes[i] )
      {
		  cout << candidates[i] << setw(10) << votes [i] << endl;
		  sum = sum + votes[i];
      }
      else
      {
         break;
      }
   }
   cout << "Total " << sum << endl;
}

(((double) votes)/sum)*100 for each. You need the cast because otherwise you'll get rounded to zero (I went a little crazy on parentheses, but you get the idea)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.