-
Help with arrays
I'm learning to program with arrays and thought I was getting the concept until I executed my program. If someone could point me in the direction of my mistakes I'd really appreciate it.
Code:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
char name[5][16];
double votes[5];
double percent[5];
int sum;
int index;
double actualpercent;
cout << fixed << showpoint << setprecision(2);
cout << "Enter candidate's name and the votes received by the candidate." << endl;
for (index = 0; index < 5; index++)
cin >> name[index][16] >> votes[index];
cout << endl;
sum = 0;
for (index = 0; index < 5; index++)
sum = sum + votes[index];
actualpercent = 0;
for (index = 0; index < 5; index++)
actualpercent = (votes[index] / sum) * 100;
cout << "Candidate" << setw(5) << "Votes Received" << setw(5) << "% of Total Votes" << endl;
for (index = 0; index < 5; index++)
cout << name[index][16] << setw(10) << votes[index] << setw(10) << percent[index];
cout << "Total" << setw(10) << sum << endl;
return 0;
}
-
> cin >> name[index][16]
Just use
cin >> name[index]
Ditto with the cout as well
-
OK.... but my data is still coming out very weird when I execute. Ideas???
Code:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
char name[5][16];
double votes[5];
double percent[5];
int sum;
int index;
double actualpercent;
cout << fixed << showpoint << setprecision(2);
cout << "Enter candidate's name and the votes received by the candidate." << endl;
for (index = 0; index < 5; index++)
cin >> name[index] >> votes[index];
cout << endl;
sum = 0;
for (index = 0; index < 5; index++)
sum = sum + votes[index];
actualpercent = 0;
for (index = 0; index < 5; index++)
actualpercent = (votes[index] / sum) * 100;
cout << "Candidate" << setw(5) << "Votes Received" << setw(5) << "% of Total Votes" << endl;
for (index = 0; index < 5; index++)
cout << name[index] << setw(10) << votes[index] << setw(10) << percent[index];
cout << "Total" << setw(10) << sum << endl;
return 0;
}
-
What are you trying to do with this code and why are you using a double dimension array to contain a string? I would help but I don't really see what is the output for this code is suppose to be.
-
Sorry... yeah, I changed the array to a string array (that was my bad). What the program is supposed to do is the user inputs candidate's name and how many votes they received (there are 5 candidates). Then the program is supposed to output (in a table format) the name, votes received, and the percentage of the total votes received. That's basically the jist of it. I've done a little more revising and this is what it looks like.
Code:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
string name[5];
double votes[5];
double percent[5];
int sum;
int index;
double actualpercent;
cout << fixed << showpoint << setprecision(2);
cout << "Enter candidate's name and the votes received by the candidate." << endl;
for (index = 0; index < 5; index++)
cin >> name[index] >> votes[index];
cout << endl;
sum = 0;
for (index = 0; index < 5; index++)
sum = sum + votes[index];
actualpercent = 0;
for (index = 0; index < 5; index++)
actualpercent = (votes[index] / sum) * 100;
cout << "Candidate Votes Received % of Total Votes" << endl;
for (index = 0; index < 5; index++)
cout << left << name[index] << setw(10) << votes[index] << right;
cout << setw(10) << percent[index];
cout << "Total" << setw(10) << sum << endl;
return 0;
}
-
Check your assignments
Code:
for (index = 0; index < 5; index++)
percent[index] /* NOT actualpercent */ = (votes[index] / sum) * 100;
-
thanks. I guess the biggest problem I'm having right now with the execution is that it's not coming out in table format. I want the names to output in a list, then the votes received to output in another list next to that, and the same for the percent of votes received. Does that make sense?
-
>I want the names to output in a list, then the votes received to output in another list next to that, and the same for the percent of votes received. Does that make sense?
Essentially, each list is a column but output is line by line, not column by column. To output lines such that data appears to be in columns you use formatted output. To do this look up i/o format flags and manipulators. For example, width() can be used to declare a string of char (consider it a box or a field) of a given length (width), and left (or right) can be used to indicate whether the string/int/whatever, placed in the box is left (or right) justified. There are a number of other flags/manipulators as well. Many of these items are defined in iostream or fstream, but for some you need to use the iomanip header file. I like to use cppreferences.com to look them up.
-
I've finally gotten my program to output in the column format I wanted. But I'm having some trouble getting the spacing between everything right. Because the names the user inputs can be different lengths, when I try to set a length between that and the remaining two columns, the last two columns don't line up. Advise???
Code:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
string name[5];
double votes[5];
double percent[5];
int sum;
int index;
cout << fixed << showpoint << setprecision(2);
cout << "Enter candidate's name and the votes received by the candidate." << endl;
for (index = 0; index < 5; index++)
cin >> name[index] >> votes[index];
cout << endl;
sum = 0;
for (index = 0; index < 5; index++)
sum = sum + votes[index];
for (index = 0; index < 5; index++)
percent[index] = (votes[index] / sum) * 100;
cout << "Candidate Votes Received % of Total Votes" << endl;
for (index = 0; index < 5; index++)
{
cout << left << name[index];
cout << right << setw(20) << votes[index];
cout << right << setw(15) << percent[index] << endl;
}
cout << "Total" << setw(20) << sum << endl;
return 0;
}
-
Use setw() when calling cout << on name as well as with votes and percent. Each call to setw() or width() or whatever syntax you use creates a "box" of certain size. The stuff inside the box may or may not fill the box completely so you use left or right or center to determine how you want the material displayed within the box.
-
Think of each column as a box. The first box will contain the name. Make a box wide enough to hold the longest possible name plus some blank spaces. Then, if you left justify the names in the first box, you will have spaces after every name. The second box/column will have a left border that is the same as the right border of the first box. If you left justify the content in the second box, it will all line up along the left edge of the second box.
-