Thread: Help with arrays

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    75

    Question 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;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > cin >> name[index][16]
    Just use
    cin >> name[index]

    Ditto with the cout as well
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    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;
    }

  4. #4
    C++ Beginner
    Join Date
    Jun 2005
    Posts
    39
    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.
    I'm a beginner C++ programmer, but I have studied HTML and Java. So if you need to help me I should catch on fast =)

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    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;
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Check your assignments
    Code:
    	for (index = 0; index < 5; index++)
    		percent[index] /* NOT actualpercent */ = (votes[index] / sum) * 100;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    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?

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >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.
    You're only born perfect.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    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;
    }

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.
    You're only born perfect.

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    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.

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    Thanks. I got it now!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM