Fixing the box

This box is killing me
I can't get this box formatted right. Under print formated addresss if it is less then it is supposed to truncate if it is greater then the limit which it does but if it is like one over it moves it out of line.What is going on?
thanks
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185

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


// Global constants
ifstream fin;

const int LIMIT=60;
const int LIMIT1=56;



// Purpose: To print formatted address
void PrintFormattedAddress(string str1,string str2,string str3)
{	
	cout << setw(LIMIT) << setfill('*') << ('*') << endl;

// Outputs string 1

	if(str1.length()<LIMIT)
	cout << "| " << setw(LIMIT1) << setfill(' ')  << str1 <<" |" << endl;

	else if (str1.length()==LIMIT1)
		cout <<"| "<< setw(LIMIT1) << setfill(' ') <<str1 <<" |"<<endl;

	else if(str1.length()>LIMIT1)
	cout << "| "  <<left<< setw(LIMIT1) << setfill(' ') << str1.substr(0,LIMIT1) << " |" << endl;


//Output string 2

	if (str2.length()>=LIMIT)
    cout <<"| " << setw(LIMIT1) << setfill(' ') << str2.substr(0,LIMIT1) << " |" << endl;

	else if(str2.length()<LIMIT)
	cout <<"| "<< setw(LIMIT1) << setfill(' ') << str2 << " |" << endl;


//Outputs string 3

	if(str3.length()<LIMIT)
	cout <<"| "<< left << setw(LIMIT1)<< setfill(' ') << str3 <<" |" << endl;

	else if(str3.length()>=LIMIT)
	cout << "| "<< setw(LIMIT1) << setfill(' ') << str3.substr(0,LIMIT1) << " |" << endl;
	
	//For the bottom stars
	cout << setw(LIMIT) << setfill('*') << ('*') << endl;
}

//Purpose to display programmers name
void Programmer()
{
cout << "Student: Jordan Smith" << endl;
cout << '\n' << endl;
}

//Purpose: To print address with no formatting 
void PrintAddress( string str1, string str2, string str3)

{

	cout << "Address" << endl;
	cout << "1: " << str1 << endl;
	cout << "2: " << str2 << endl;
	cout << "3: " << str3 << endl;
}


//Purpose to echo input
void Echo(string str1,string str2, string str3)
{

	cout <<"Echo"<< endl;
	cout << "1: "<< str1 << endl;
	cout << "2: "<< str2 << endl;
	cout << "3: "<< str3 << endl;

	cout<<endl;

}


//Purpose: To obtain print selection
int PrintSelection()
{
int choice=0;
int nullchoice=-1;

fin >> choice;

	if((choice>0)&&(choice<=3))
	return choice;

	else if((choice<0)&&(choice>3))
	choice= nullchoice;

	return choice;


}


//Purpose to display print selection
void Displayprintselection( int Printchoice)
{
	if(Printchoice!=-1)
	cout << "Your print selection is : "<< Printchoice <<"\n" << endl;
	
	else if( Printchoice=-1)
	cout << "You have selected an invalid printing option." << endl;
}

//Purpose to get three vailid lines of address
void GetAddress(string& str1, string& str2, string& str3)
{
	
	fin.get();
	while(str1.size()==0)
	getline(fin,str1);

	while(str2.size()==0)
	getline(fin,str2);

	while(str3.size()==0)
	getline(fin,str3);
	
}

void main()
{

//Variables	
string str1="";
string str2="";
string str3="";
int Printchoice=0;
fin.open("inputpractice.txt");
int n=0;

	//Algorithm
	Programmer();
	
	//Calling functions 

	while(!fin.eof())
	{
		str1 = "";
		str2 = "";
		str3 = "";
		Printchoice=PrintSelection();
		GetAddress(str1,str2,str3);
		Echo(str1,str2,str3);
		Displayprintselection(Printchoice);

//Control of selection
	switch(Printchoice)
	{
	
	case 1: PrintAddress(str1,str2,str3);
			cout << "\n" << endl;
		    break;

	case 2: PrintFormattedAddress(str1,str2,str3);
		    cout << "\n" << endl;
		    break;

	case 3: PrintAddress(str1,str2,str3);
		    cout << '\n' << endl;
			PrintFormattedAddress(str1,str2,str3);
			cout << "\n" << endl;
			break;

	default: cout <<" "<< endl;
	}
	}
system("pause");



}
Last edited on
Your code looks more complicated than it needs to be.
Have you considered something like this?
1
2
3
// Outputs string 1
    str1.resize(LIMIT1, ' ');
    cout << "| " << str1 << " |" << endl;
hey what is re size? I didn't know that this was a function the only tool I had was substring.

I have another questions you know how I have the switch structure at the end ? Today we learned about enum and my teacher put a switch inside a function by using the enum is it impossible to do this without having a enum ?


Like this below. Don't get hung up on the specifics because this will not compile I think but just in general can you do this with out enum.


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
void Controlofselection(int& Printchoice, string& str1,string& str2,string& str3)
{
while(!fin.eof())
	{
		

//Control of selection
	switch(Printchoice)
	{
	
	case 1: PrintAddress(str1,str2,str3);
			cout << "\n" << endl;
		    break;

	case 2: PrintFormattedAddress(str1,str2,str3);
		    cout << "\n" << endl;
		    break;

	case 3: PrintAddress(str1,str2,str3);
		    cout << '\n' << endl;
			PrintFormattedAddress(str1,str2,str3);
			cout << "\n" << endl;
			break;

	default: "\n";
	}
	

	}
}








Take a look at the reference section on this site, it's very good,

http://www.cplusplus.com/reference/string/string/
http://www.cplusplus.com/reference/string/string/resize/

Regarding the use of enum, that is just a way of improving readability of the program. It also serves to document what are the acceptable values. But yes, you can certainly do a switch/cases without the use of enum. (Internally enum values are simply integers).
Last edited on
OK if the switch above was a function how would I pass all the variables that I needed because if you look I have a lot of functions within the switch function.Would I just do it like I did but just do all of the functions or variables that are needed to make this function work?
Do you get what I'm saying?
THANKS

So why even use enum? Does it really improve readabilty and what exactly do you mean by readability?
I'm not quite sure I get the question. If you meant the switch in the previous post, well, that's already just about a function in it's own right. You could do something like this I suppose, and have switch/case inside the new function outputAddress():
1
2
3
4
    while(!fin.eof())
    {
        outputAddress(Printchoice, str1, str2, str3);
    }

If you're concerned about passing so many parameters around, you could group some of them together, for example the address lines str1, str2, str3 are related, so could be put into a structure or class, and then passed around as a single parameter.
1
2
3
4
5
struct Address {
    std::string line1;
    std::string line2;
    std::string line3;
} address;

In this case, you'd need to pass address instead of str1, str2 and str3.

So why even use enum? Does it really improve readabilty and what exactly do you mean by readability?

It's sometimes hard to explain ideas because necessarily the programs done as exercises are not hugely complicated, but real-world programs can be very long, and if you are working on some part in the middle, it's a great help to be clear about what it is you are dealing with.

Let's say for example, you have a variable like this: int dayNumber;
Now if you just glance at that, you don't know whether it's the day of the week, or of the month, quarter, or year. Let's say it has a more meaningful name, int dayOfWeek = 4;. Can you tell whether dayOfWeek here represents "Tuesday" or "Wednesday" or "Thursday"? You'd have to read the documentation, (which may be missing or out of date), or the program comments (ditto), or then start to study the program code itself.
But if an enum was used, all becomes clear:
1
2
    enum weekday { sun, mon, tue, wed, thu, fri, sat };
    weekday dayOfWeek = thu;

Now this is a trivial example, in the real world the types of data, and the permitted values may be much less familiar, and the usefulness of the idea becomes more valuable.

Bear in mind it will often be necessary to work on code which was written by someone else, and even if you write it all yourself, six months down the line even one's own code can appear baffling if it's not clearly written. Comments in the code can help, but these can and do get out of step with what the program really does. So good clear, self-documenting code does help. By self-documenting, I mean the enum, together with sensible names for functions and variables etc. should make it clear what the program is doing.

Topic archived. No new replies allowed.