Access Violation Issue with Bubble Sorting/Duplicate Arrays - c++

This program seems to work fine until it goes to display the data after the sort function, it will display by destination but it bypasses displaying by airline. Then it gives the access violation window and I'm not 100% sure on what it even means. If anybody could explain and help that would be much appreciated.
#include <iostream>
#include <string>
using namespace std;
double flight_number[3], number_passengers[3], max_records, max_pass, min_pass;
string airline_name[3], destination[3], duplicate[3];
char reply, swapping;
int row, index[3];
void setup();
void load_arrays();
void display_airline();
void display_destination();
void sort();
int main()
{
setup();
load_arrays();
display_airline();
display_destination();
system("pause");
return 0;
}
void setup()
{
max_records = 3;
max_pass = 275;
min_pass = 50;
}
void load_arrays()
{
for (row = 0; row < max_records; row++)
{
cout << "Charter Number " << row << endl << endl;
cout << "Please enter..." << endl;
cout << "Airline Name----> ";
cin >> airline_name[row];
cout << endl;
cout << "Flight Number----> ";
cin >> flight_number[row];
cout << endl;
cout << "Destination----> ";
cin >> destination[row];
cout << endl;
cout << "Passenger Load----> ";
cin >> number_passengers[row];
cout << endl;
while (number_passengers[row] < min_pass || number_passengers[row] > max_pass)
{
cout << "You have entered an invalid amount of passengers." << endl;
cout << "There must be between 50 and 275 passengers. > ";
cin >> number_passengers[row];
cout << endl;
}
}
}
void display_airline()
{
system("cls");
for (row = 0; row < max_records; row++)
{
duplicate[row] = airline_name[row];
}
sort();
cout << "Airline" << " Flight" << " Destination" << " Passenger" << endl;
cout << "Name" << " Number" << " Load" << endl;
for (row = 0; row < max_records; row++)
{
cout << airline_name[index[row]] << " " << flight_number[index[row]] << " " << destination[index[row]] << " " << number_passengers[index[row]];
cout << endl;
}
}
void display_destination()
{
system("cls");
for (row = 0; row < max_records; row++)
{
duplicate[row] = destination[row];
}
sort();
cout << "Destination" << " Flight" << " Flight" << " Passenger" << endl;
cout << " Name" << " Number" << " Load" << endl;
for (row = 0; row < max_records; row++)
{
cout << destination[index[row]] << " " << airline_name[index[row]] << " " << flight_number[index[row]] << " " << number_passengers[index[row]];
cout << endl;
}
}
void sort()
{
for (row = 0; row < max_records; row++)
{
index[row] = row;
}
swapping = 'Y';
while (swapping == 'Y')
{
swapping = 'N';
for (row = 0; row < max_records; row++)
{
if (duplicate[row] > duplicate[row + 1])
{
swap (duplicate[row], duplicate[row + 1]);
swap (index[row], index[row + 1]);
swapping = 'Y';
}
}
}
}
also this is the full violation error: Unhandled exception at 0x009984F6 in Program 6.exe: 0xC0000005: Access violation reading location 0x03947188.

for (row = 0; row < max_records; row++)
if (duplicate[row] > duplicate[row + 1]).
What's duplicate[row+1] when row==3? You want the sort to go to max_records-1 since it works on pairs of elements rather than individual elements.

Related

Trying to have c++ program quit by inputting a letter

I made a program that generates a multiplication table for the number that is inputted by the user:
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
char userSelection;
int numForTable;
int col;
int row;
do
{
cout << "Please enter a number for your multiplication table: " << endl;
cin >> numForTable;
while (numForTable < 1 || numForTable > 10)
{
cout << "Please enter a number between 1 & 10." << endl;
cin >> numForTable;
}
cout << "\n"
<< "MULTIPLICATION TABLE: " << numForTable << "'s" << endl
<< "\n"
<< " " << 1;
for (col = 2; col <= numForTable; ++col)
cout << " " << col;
cout << endl;
cout << " ----|";
for (col = 2; col <= numForTable; ++col)
cout << "----|";
cout << endl;
for (col = 1; col <= numForTable; ++col)
{
cout << setw(2) << col << "|";
for (row = 1; row <= numForTable; ++row)
cout << setw(4) << col * row << "|";
cout << endl;
cout << " -|----";
for (row = 2; row <= numForTable - 1; ++row)
cout << "|----";
cout << "|----|";
cout << endl;
}
}
while (userSelection != 'q');
return 0;
}
It continuously asks the user to input a number until the program is closed, but I'm trying to make it so the program closes when the user inputs any alphabet letter followed by a message that says something like "Have a nice day"
I totally used your program to write the table, and only make the mechanism to switch between writing the table or exiting the program. Since your program only writes the table for integer between 1-10, which is 2-9, I used the ASCII code of the char for it. Here's the code.
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
char userSelection;
int numForTable;
int col;
int row;
while(cin>>userSelection)
{
int numForTable = (int)userSelection - 48;
if(numForTable<2 or numForTable > 9) //because the limit of the table is between 1-10, that is 2-9
{
cout<<"Have a nice day"<<endl;
return 0;
}
else
{
cout << "\n"
<< "MULTIPLICATION TABLE: " << numForTable << "'s" << endl
<< "\n"
<< " " << 1;
for (col = 2; col <= numForTable; ++col)
cout << " " << col;
cout << endl;
cout << " ----|";
for (col = 2; col <= numForTable; ++col)
cout << "----|";
cout << endl;
for (col = 1; col <= numForTable; ++col)
{
cout << setw(2) << col << "|";
for (row = 1; row <= numForTable; ++row)
cout << setw(4) << col * row << "|";
cout << endl;
cout << " -|----";
for (row = 2; row <= numForTable - 1; ++row)
cout << "|----";
cout << "|----|";
cout << endl;
}
}
}
return 0;
}
First, it changes the char input to the ASCII code, then it checks whether the number ranges from 2 to 9, then execute the program according to it. Please do correct me if i have any mistake on it.
You can type and input a character or string in 'numForTable' to quit the program but it will throw an error because of a type mismatch.
To handle this, you can use try and catch in c++
//start of do while loop
cout << "Please enter a number for your multiplication table: " << endl;
try{
cin >> numForTable;
if(!numForTable){ //if numForTable is not int, it will throw the the catch will be executed
throw 0;
}
}catch(...){
cout<< "Have a nice day";
return 0;
}
while (numForTable < 1 || numForTable > 10)
{
cout << "Please enter a number between 1 & 10." << endl;
// I do the same here
try{
cin >> numForTable;
if(!numForTable){
throw 0;
}
}catch(...){
cout<< "Have a nice day";
return 0;
}
}
I tried this and it worked
Also I changed the do while to -- do...while(true)

Nested For - Loops to create multiplication table C++

I've been trying to overcome this problem for a few hours now and I seem to have one approach to the situation. It seems that the use of selection statements worked in creating the table necessary. Although there are formatting issues.
I'd like to know if there was a way to create the same table
using only nested for-loops as mentioned by our professor.
Are the selection statements necessary or can we implement a system of nested for loops to acquire the same results?
The image below is the required table:
But the image below is what I have:
Below is my code:
for (int i = 0; i <= numChoice; ++i)
{
if (i == 0)
{
for (int k = 1; k <= numChoice; ++k)
{
cout << " " << k;
}
cout << "\n";
}
else
{
cout << i << " | ";
for (int j = 1; j <= numChoice; ++j)
{
if (j*i <= 9)
{
cout << " " << j*i << "|";
}
else if (j*i > 9 && j*i <= 100)
{
cout << " " << j*i << "|";
}
else if (j*i > 99 && j*i <= 999)
{
cout << " " << j*i << "|";
}
}
cout << "\n";
for (int k = 0; k <= numChoice; ++k)
{
if (k == 0)
{
cout << "-|";
}
else
{
cout << "----|";
}
}
cout << "\n";
}
}
The following code uses no if else constructs. The formatting can be got by using setw, used for setting the width of integers.Following code produces perfect output.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i,j;
cout<<" "<<1;//5 space chars
for(i = 2;i <= 10;++i)
cout<<" "<<i;//4 space chars
cout<<endl;
cout<<" ----|";
for(i = 2;i <= 10;++i)
cout<<"----|";
cout<<endl;
for(i = 1;i <= 10;++i)
{
cout<<setw(2)<<i<<"|";
for(j = 1;j <= 10;++j)
cout<<setw(4)<<j*i<<"|";
cout<<endl;
cout<<" -|----";
for(j = 2;j <= 9;++j)
cout<<"|----";
cout<<"|----|";
cout<<endl;
}
return 0;
}
#FranticCode. I'm also in the same class as you and was having problems with this homework assignment as well. I still don't understand it, but I figured out how to manipulate Sumeet's code to give us correct format. The ONLY thing I am having a problem with now is adding an empty space AFTER the first multiplication table and before the menu redisplay. I'll share what I have and maybe you can figure it out. Still going to ask the professor to review chapter 5 because I would like to learn it rather than just submit the homework.
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
char userSelection;
int numForTable;
int col;
int row;
do
{
cout << "MENU" << endl
<< "a) Generate Multiplication Table" << endl
<< "q) Quit the program" << endl
<< "Please make a selection: ";
cin >> userSelection;
if (userSelection == 'a')
{
cout << "Please enter a number for your multiplication table: " << endl;
cin >> numForTable;
while (numForTable < 1 || numForTable > 10)
{
cout << "Please enter a number between 1 & 10." << endl;
cin >> numForTable;
}
cout << "\n"
<< "MULTIPLICATION TABLE: " << numForTable << "'s" << endl
<< "\n"
<< " " << 1;
for (col = 2; col <= numForTable; ++col)
cout << " " << col;
cout << endl;
cout << " ----|";
for (col = 2; col <= numForTable; ++col)
cout << "----|";
cout << endl;
for (col = 1; col <= numForTable; ++col)
{
cout << setw(2) << col << "|";
for (row = 1; row <= numForTable; ++row)
cout << setw(4) << col * row << "|";
cout << endl;
cout << " -|----";
for (row = 2; row <= numForTable - 1; ++row)
cout << "|----";
cout << "|----|";
cout << endl;
}
}
else if (userSelection != 'q')
{
cout << "Invalid Selection\n" << endl;
}
else if (userSelection == 'q')
{
cout << " You have chosen to quit the program. Thank you for using!" << endl;
}
}
while (userSelection != 'q');
//system("PAUSE");
return 0;
}
got curious to see if i could add the lines as easy as i claimed, it took a bit of fiddling, but here's the result (updated code below to also have lines).
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int counter;
int counter2;
int amount;
cout << " |-----------------------------------------------------------|" << endl; // first line of table.
for(counter=1;counter<11;counter++){ // the 2 for lines create our 2 dimensional table
for(counter2=1;counter2<11;counter2++){
cout << " | " << setw(3) << counter*counter2; // setw(3) is a function of <iomanip>,
//setting minimum width to 3 for numbers.
}
cout << " |" << endl; // this here is being added to the end of each line and starts a new line.
cout << " |-----------------------------------------------------------|" << endl; // this is being inserted between each line, and starts a new line.
}
return 0;
}
Use the following construct:
for (int i=0; i<=numChoice; i++) // display first row of numbers
cout <<"\t" << i << "\t";
cout << "\n";
for (int i=0; i <=numChoice; i++) {
cout << i << "\t";
for (int j=0; j <=numChoice; j++)
cout << i*j << "\t";
cout << "\n";
}

Array of Structures - Functions not pulling through data

I am studying BscCS so I am familiar with programming concepts, however I always seem to get stuck on structs and arrays of structs.
I am required to convert parallel arrays into an array of structs. I have done this, but for some reason the information is not getting sent to the functions properly and the program keeps crashing.
Could you help identify where I am going wrong of if there is something I am missing? I don't need exact code for answers, just some guidance. Here is the code:
#define SIZE 3
struct Employee {
string firstName[SIZE];
string lastName[SIZE];
int id[SIZE];
int hoursWorked[SIZE];
int payRate[SIZE];
int stat[SIZE];
};
//Functions
int menu();
void printReport(Employee & employees);
void search(Employee & employees);
void calculatePay(Employee & employees);
void orderByLastName(Employee & employees);
void orderByid(Employee & employees);
void printActive(Employee & employees);
void printInactive(Employee & employees);
//Display Main Menu
int menu()
{
int choice;
cout << "1. Print out Employee Report. " << endl;
cout << "2. Search Employee Records. " << endl;
cout << "3. Display the Report in Sorted order on Last Name or ID. << endl;
cout << "4. Calculate Pay. " << endl;
cout << "5. Display Active Employees." << endl;
cout << "6. Display Inactive Employees." << endl;
cout << "7. Quit" << endl;
cout << "Enter your choice. ";
cin >> choice;
return choice;
}
//Display the employee data in a formatted order
void printReport(Employee & employees)
{
cout << setw(10) << "First Name" <<
setw(10) << "Last Name" <<
setw(10) << "ID" <<
setw(10) << "Hours" <<
setw(10) << "Rate" <<
endl;
ios::fixed;
for (int index = 0; index < SIZE; index++)
cout << setw(10) << employees.firstName[index] <<
setw(10) << employees.lastName[index] <<
setw(10) << employees.id[index] <<
setw(10) << employees.hoursWorked[index] <<
setw(10) << employees.payRate[index] << endl;
}
//Search for employee ID
//Show "Not Found" if unable to locate
void search(Employee & employees)
{
bool found = false;
int idNumber;
int pos = -1;
cout << "Enter id number ";
cin >> idNumber;
for (int index = 0; index < SIZE && !found; index++)
{
if (employees.id[index] == idNumber)
{
found = true;
pos = index;
}
}
if (!found)
cout << "Not Found. " << endl;
else
cout << setw(10) << employees.firstName[pos] <<
setw(10) << employees.lastName[pos] <<
setw(10) << employees.id[pos] <<
setw(10) << employees.hoursWorked[pos] <<
setw(10) << employees.payRate[pos] << endl;
}
//Calculate total weekly pay
void calculatePay(Employee & employees)
{
cout << setw(10) << "First Name" <<
setw(10) << "Last Name" <<
setw(10) << "ID" <<
setw(10) << "Hours" <<
setw(10) << "Rate" <<
setw(10) << "Total Pay" <<
endl;
ios::fixed;
for (int index = 0; index < SIZE; index++)
cout << setw(10) << employees.firstName[index] <<
setw(10) << employees.lastName[index] <<
setw(10) << employees.id[index] <<
setw(10) << employees.hoursWorked[index] <<
setw(10) << employees.payRate[index] <<
setw(10) << employees.hoursWorked[index] * employees.payRate[index] << endl;
}
//Sort employee data by last name
void orderByLastName(Employee & employees)
{
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE - 1; j++)
{
if (employees.lastName[j] > employees.lastName[j + 1])
{
string temp = employees.lastName[j];
employees.lastName[j] = employees.lastName[j + 1];
employees.lastName[j + 1] = temp;
temp = employees.firstName[j];
employees.firstName[j] = employees.firstName[j + 1];
employees.firstName[j + 1] = temp;
int tempid = employees.id[j];
employees.id[j] = employees.id[j + 1];
employees.id[j + 1] = tempid;
int temphours = employees.hoursWorked[j];
employees.hoursWorked[j] = employees.hoursWorked[j + 1];
employees.hoursWorked[j + 1] = temphours;
int temppayrate = employees.payRate[j];
employees.payRate[j] = employees.payRate[j + 1];
employees.payRate[j + 1] = temppayrate;
}
}
}
}
//Sort employee data by ID
void orderByid(Employee & employees)
{
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE - 1; j++)
{
if (employees.id[j] > employees.id[j + 1])
{
string temp = employees.lastName[j];
employees.lastName[j] = employees.lastName[j + 1];
employees.lastName[j + 1] = temp;
temp = employees.firstName[j];
employees.firstName[j] = employees.firstName[j + 1];
employees.firstName[j + 1] = temp;
int tempid = employees.id[j];
employees.id[j] = employees.id[j + 1];
employees.id[j + 1] = tempid;
int temphours = employees.hoursWorked[j];
employees.hoursWorked[j] = employees.hoursWorked[j + 1];
employees.hoursWorked[j + 1] = temphours;
int temppayrate = employees.payRate[j];
employees.payRate[j] = employees.payRate[j + 1];
employees.payRate[j + 1] = temppayrate;
}
}
}
}
void printActive(Employee & employees)
{
bool found = false;
int pos = -1;
for (int index = 0; index < SIZE && !found; index++)
{
if (employees.stat[index] == 1)
{
found = true;
pos = index;
}
if (!found) {
cout << "No active employees." << endl;
}
else {
cout << setw(10) << employees.firstName[pos] <<
setw(10) << employees.lastName[pos] <<
setw(10) << employees.id[pos] <<
setw(10) << employees.hoursWorked[pos] <<
setw(10) << employees.payRate[pos] << endl;
}
}
}
void printInactive(Employee & employees)
{
bool found = false;
int pos = -1;
for (int index = 0; index < SIZE && !found; index++)
{
if (employees.stat[index] == 0)
{
found = true;
pos = index;
}
if (!found) {
cout << "No inactive employees." << endl;
}
else {
cout << setw(10) << employees.firstName[pos] <<
setw(10) << employees.lastName[pos] <<
setw(10) << employees.id[pos] <<
setw(10) << employees.hoursWorked[pos] <<
setw(10) << employees.payRate[pos] << endl;
}
}
}
int main()
{
struct Employee employees[SIZE];
//Read first and last name from user
for (int index = 0; index < SIZE; index++)
{
cout << "Enter first name : ";
cin >> employees[index].firstName[index];
cout << "Enter last name : ";
cin >> employees[index].lastName[index];
//Read ID, hours, and pay rate from user
while (employees[index].id[index] < 0)
{
cout << "Enter id : ";
cin >> employees[index].id[index];
if (employees[index].id[index] < 0)
{
cout << "Invalid Id number. " << endl;
cout << "Enter id : ";
cin >> employees[index].id[index];
}
}
while (employees[index].hoursWorked[index] < 0)
{
cout << "Enter hours worked : ";
cin >> employees[index].hoursWorked[index];
if (employees[index].hoursWorked[index] < 0)
{
cout << "Invalid hours. " << endl;
cout << "Enter hours worked : ";
cin >> employees[index].hoursWorked[index];
}
}
while (employees[index].payRate[index] < 0)
{
cout << "Enter Pay Rate : ";
cin >> employees[index].payRate[index];
if (employees[index].payRate[index] < 0)
{
cout << "Invalid pay rate. " << endl;
cout << "Enter Pay Rate : ";
cin >> employees[index].payRate[index];
}
}
while (employees[index].stat[index] < 0)
{
cout << "Enter your status. (0 - Inactive, 1 - Active) : ";
cin >> employees[index].stat[index];
if (employees[index].stat[index] < 0)
{
cout << "Enter your status : ";
cin >> employees[index].stat[index];
}
cout << endl;
}
}
//Loop to display menu options until user quits program
while (true)
{
//Call menu with options
int ch = menu();
//Different options to choose
switch (ch)
{
case 1:
printReport(employees[SIZE]);
break;
case 2:
search(employees[SIZE]);
break;
case 3:
int sortType;
cout << "1.Sort by Last Name." << endl;
cout << "2.Sort by ID." << endl;
cout << "Enter your choice. ";
cin >> sortType;
if (sortType == 1)
orderByLastName(employees[SIZE]);
else if (sortType ==2)
orderByid(employees[SIZE]);
break;
case 4:
calculatePay(employees[SIZE]);
break;
case 5: printActive(employees[SIZE]);
case 6: printInactive(employees[SIZE]);
break;
case 7:
exit(0);
}
}
system("pause");
return 0;
}
Thank you in advance!
I ran your program, and I received more than 20 errors before the compiler involuntarily stopped trying to compile any longer. All of them are connected to the lines of code using cout, cin, string, and setw. You need to use these keywords with the std namespace to fix this, and you need to include the iostream header file (#include <iostream>). Either you can write std:: in front of all of these keywords (i.e. std::cout) or you can declare the namespace in the preprocessor (using namespace std;). Also, for std::setw, you need to include the iomanip header file (#include <iomanip>). After that, you're missing a quotation in the menu function and a bracket at the end of the main function. (There's also a couple of problems with initializing the size of the struct members in the declaration, but I'll leave that up to you.) Hope this helped!

Two Dimensional Array outputs one continuous row

Hello I am new to C++ and am having trouble understanding why this two dimensional array is only producing one row and many columns. It reads the correct information but does not output it with the correct columns and rows.
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <windows.h>
using namespace std;
char pat [9][9]= {'$','$','$','$','$','$','$','$','$',
'$','$','$','$','$','$','$','$','$',
'$','$','$','$','$','$','$','$','$',
'$','$','$','$','$','$','$','$','$',
'$','$','$','$','$','$','$','$','$',
'$','$','$','$','$','$','$','$','$',
'$','$','$','$','$','$','$','$','$',
'$','$','$','$','$','$','$','$','$',
'$','$','$','$','$','$','$','$','$'}; // 9x9 matrix
int main ()
{
int pattern,
dimensions;
do
{
cout << "1) Display Pattern 1" << endl; //menu selections
cout << "2) Display Pattern 2" << endl;
cout << "3) Display Pattern 3" << endl;
cout << "4) Display Pattern 4" << endl;
cout << "5) Exit Program" << endl << endl;
cout << "Please select an option. ";
cin >> pattern;
if (pattern == 1)
{
system("cls");
do
{
cout << "Note: Choose a number between 1 and 10." << endl;
cout << "Choose a number ";
cin >> dimensions;
cout << endl;
if (dimensions > 1 && dimensions < 10)
/* based on the user's input for dimension
it will output a square i.e. 2x2, 3x3, 4x4 etc */
{
cout << "True!" << endl;
for (int rows = 0; rows < dimensions; rows++)
{
for (int cols = 0; cols < dimensions; cols++)
cout << pat[cols][rows];
}
}
else
{
cout << "Error! Number is not between this set!" << endl;
Sleep(3000);
cout << endl;
}
}
while (pattern == 1);
}
else if (pattern == 2)
{
system("cls");
do
{
cout << "Note: Choose a number between 1 and 10." << endl;
cout << "Choose a number ";
cin >> dimensions;
cout << endl;
if (dimensions > 1 && dimensions < 10)
{
cout << "True!";
}
else
{
cout << "Error! Number is not between this set!" << endl;
Sleep(3000);
cout << endl;
}
}
while (pattern == 2);
}
else if (pattern == 3)
{
system("cls");
do
{
cout << "Note: Choose a number between 1 and 10." << endl;
cout << "Choose a number ";
cin >> dimensions;
cout << endl;
if (dimensions > 1 && dimensions < 10)
{
cout << "True!";
}
else
{
cout << "Error! Number is not between this set!" << endl;
Sleep(3000);
cout << endl;
}
}
while (pattern == 3);
}
else if (pattern == 4)
{
system("cls");
do
{
cout << "Note: Choose a number between 1 and 10." << endl;
cout << "Choose a number ";
cin >> dimensions;
cout << endl;
if (dimensions > 1 && dimensions < 10)
{
cout << "True!";
}
else
{
cout << "Error! Number is not between this set!" << endl;
Sleep(3000);
cout << endl;
}
}
while (pattern == 4);
}
else if (pattern == 5)
{
return 0;
}
else
{
cout << "Please input a valid entry." << endl << endl;
Sleep(3000);
cout << endl;
}
}
while (pattern != 5);
}
When you're printing the array:
for (int rows = 0; rows < dimensions; rows++)
{
for (int cols = 0; cols < dimensions; cols++)
cout << pat[cols][rows]
}
You never print a new line, so it's all on one row. You want something like this:
for (int rows = 0; rows < dimensions; rows++)
{
for (int cols = 0; cols < dimensions; cols++)
cout << pat[cols][rows]
cout << endl;
}
If you want to initialize a multidimensional array, you have to do it like so:
int multiarray[3][3] =
{
{1,2,3},
{4,5,6},
{7,8,9}
};
Using nested braces to separate the dimensions.
After this to output it the way you want you do it as such:
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
std::cout << multiarray[i][j];
}
std::cout << std::endl;
}

Lining columns up

Solved!
This is what I wrote:
cout << setw(4) << "Students";
cout << setw(20) << "Hours Worked";
cout << setw(20) << "of Total Hours";
cout << endl;
for (int i = 0; i < students; i++)
{
cout << setw(20);
cout << names[i];
cout << setw(10) << hours[i];
cout << setw(10) << percent[i];
cout << endl;
}
But if the first name is a few characters sorter or bigger than second name, they become misaligned. How would I keep each column aligned equally?
Try something like this:
#include<iostream>
#include <iomanip>
#include<string>
using namespace std;
int main()
{
int students = 5;
string names[5] = {"a","bccc","c","d","ecsdfsdfasdasasf"};
int hours[5] = {1,2,3,4,5};
int percent[5] = {10,20,30,40,54};
string column("Students");
int maxStringSize = 0;
int sizeOfStudentColumn = column.length();
for(int i = 0; i < 5; ++i)
{
if(maxStringSize < names[i].length())
maxStringSize = names[i].length();
}
if(sizeOfStudentColumn > maxStringSize)
maxStringSize = sizeOfStudentColumn;
cout<<"max size: "<<maxStringSize<<endl;
cout << setw(4) << "Students";
cout << setw(maxStringSize + 5) << "Hours Worked";
cout << setw(20) << "of Total Hours";
cout << endl;
for (int i = 0; i < students; i++)
{
// cout << setw(20);
cout << names[i];
int diff = maxStringSize - names[i].length();
cout << setw(diff + 5 ) << hours[i];
cout << setw(20) << percent[i];
cout << endl;
}
}