Need help aligning my output

This is my output. For some reason Gear-Housing and Vacuum-Gripper are not aligning their values 5 and 25. What am i doing wrong? I copy pasted my code at the end.

Valve-------------------------10
Bearing------------------------5
Bushing----------------------15
Coupling---------------------21
Flange------------------------7
Gear--------------------------5
Gear-Housing-----------------------5
Vacuum-Gripper--------------------25
Cable------------------------18
Rod--------------------------12

*This is my code*

#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

struct bin {
string descript;
int amount;
};
double AddParts();
double RemoveParts();

int main() {
int i; //count
int choice;
ifstream file;
file.open("bins.txt");

const int size = 10;
bin array[size] = {};
for (i = 0; i < size; i++) {
file >> array[i].descript >> array[i].amount;
cout << setw(10) << left << array[i].descript << setw(20) << right << fixed << array[i].amount << endl;
}
Last edited on
I think, you should make setw(20) instead of setw(10).
Hello sambix,

Until I work up something to test this code, you might give this a try:

1
2
3
4
5
6
7
std::cout << std::setfill('-');

for (i = 0; i < size; i++)
{
	file >> array[i].descript >> array[i].amount;
	cout << setw(30) << left << array[i].descript << ' ' << setw(3) << right << fixed << array[i].amount << endl;
}


The setw 30 is what you want your longest line to be. And the setw 3 will allow for a three digit number. I added the space in between for appearance.

Hope that helps,

Andy
Hello sambix,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

Once I set up the program to run this is what I came up with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <cmath>
#include <string>
#include <fstream>
#include <iomanip>
#include <chrono>  // <---Added.
#include <thread>  // <---Added.


//using namespace std;  // <--- Best not to use.

struct bin
{
	std::string descript;
	int amount;
};

void ReadFile(bin array[], int& index);
double AddParts();
double RemoveParts();

int main()
{
	//int i; //count  // <--- Better to define this in the for loop.
	int index{};  // <--- Count  of records read. Could also use a different name.
	int choice{};
	//ifstream file;
	//file.open("bins.txt");

	const int MAXSIZE = 10;  // <--- I like MAXSIZE. The capital letters let you know it is a constant.

	bin array[MAXSIZE]{};  // <--- The = is not needed, but OK.

	ReadFile(array, index);

	for (int i = 0; i < index; i++)
	{
		std::cout << std::setfill('-');
		//file >> array[i].descript >> array[i].amount;
		std::cout << std::setw(30) << std::left << array[i].descript
			<< std::setfill(' ')  // <--- Sets fill charater back to a space.
			<< ' ' // <--- Prints a space. More for appearance.
			<< std::setw(3) << std::right << array[i].amount << std::endl;
	}

	return 0;
}

void ReadFile(bin array[], int& index)
{
	std::string iFileName{ "Data.txt" };

	std::ifstream inFile;

	inFile.open(iFileName);

	if (inFile.is_open())
	{
		std::cout << "\n File " << iFileName << " is open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(2));  // <--- Needs header files chrono" and "thread".
	}
	else
	{
		std::cout << "\n File " << iFileName << " did not open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread".
		exit(1);
	}

	while (inFile>> array[index].descript)
		inFile >> array[index++].amount;

	std::cout << std::endl;
}


I find it is better to read the file and fill the array first. Then the array can be used when you need it. What you have will also work. Using the while loop and the variable "index", or you could use another name, will allow you to read a file of any size and "index" will keep track of the number of records stored in the array. Although the array may need to be of a larger size.

Personally I like a function like "ReadFile" so the file only has to be read once. I set up this when I was first learning about reading files. With some time and experience you should be able to figure out how to shorten this code.

Until I could test what I first showed you I started with the right idea then found out that it needed some more.

The output of the above code produces this which is what I believe you want.

Valve-------------------------  10
Bearing-----------------------   5
Bushing-----------------------  15
Coupling----------------------  21
Flange------------------------   7
Gear--------------------------   5
Gear-Housing------------------   5
Vacuum-Gripper----------------  25
Cable-------------------------  18
Rod---------------------------  12


The program allows for a three digit number with an extra space between the "-" and the start of the number. if you only have one and two digit numbers you may not want the extra space. Or shorten setw(3) to 2.

Hope that helps,

Andy

Edit:

Forgot to mention "std::fixed" is not needed as it works with std::setprecision and is used with doubles or floats. It has no effect on ints.
Last edited on
Topic archived. No new replies allowed.