Problems with formatting

Hello,
I'm trying to write a function that will output 3 lines of text surrounded by a box.
***************************
| Chuck smith |
| 123 Main Street |
| Anywhere Mi, 49232 |
***************************
This is what it will look like but don't get hung up on the specifics of this example it is just so you know what the goal is.

For my real output it has to have a limit of 60 so the stars will be 60.
If text from any of the lines goes beyond that it is supposed to be cut.
The problem is that I looked at a similar example and nothing is lining up right for me. This is the code that I have made so far. Well one of the attempts in the past 3 hours!The LIMIT is 60 remember.
{
if(str1.length()<=LIMIT)
cout<<"| "<<setw(LIMIT)<<left<<setfill(' ')<<str1<<" |"<<endl;
}
I don't understand why if I have the LIMIT as the width it would output the end bar | on the next line. This is just for the <=LIMIT. When I write the other one for if it is > LIMIT I will use substr() to end it where I want.
I just cannot seem to get this straight for some reason I don't know what else to do I been messing with it for a while.
Thanks

Oh and the lines on the box above are supposed to be in line with the stars I couldn't get it to for some reason so please excuse the out of line bars


Last edited on
As horrible as this code look, it might work for you and is encapsulated in a single function. I can't really explain your problem with that little of code so take this or leave it.

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
void Output(ostream& out, string data)
{
	int i = 0;	
	int dataSize = 0;
	const int limit = 10;
	
	out << "| "; 	
	dataSize += data.size();
    
	if (dataSize < limit)
		out << data << " |" << endl;
	else
	{
		out << data.substr(0, limit) << " |" << endl;
		i = limit; 	
		
		while (i < dataSize)
		{
			out << "| " << data.substr(i, i+limit) << " |" << endl;
			i = i+limit; 	
		}	
	}
}

int main ()
{
string a = "aaa";
string b = "bb";
string c = "cccccddddde";  // 11 chars
string d = "fffffggggg";  // exactly 10 chars

Output(cout, a);
Output(cout, b);
Output(cout, c);
Output(cout, d);
}


Output is:

1
2
3
4
5
| aaa |
| bb |
| cccccddddd |
| e |
| fffffggggg |


It needs serious cleaning up but it's short and sweet. I didn't add the * part because it's trivial.
Last edited on
closed account (3qX21hU5)
This code might be able to help. Used it for one of my ealier exercises and can be transformed to fit your specifics. It uses for and while loops to generate the border.

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
	// The number of blanks surrounding the greeting
	cout << "Please enter the amount of spacing you would like: ";
	int pad = 0;
	cin >> pad;
	

	// The number of row and columns to write
	const int rows = pad * 2 + 3;
	const string::size_type cols = greeting.size() + pad * 2 + 2;

	// Seperate out and input
	cout << endl;

	for (int r = 0; r != rows; ++r)
	{
		string::size_type c = 0;

		while (c != cols)
		{
			// Tests if its time to write the greeting
			if ( r == pad + 1 && c == pad + 1)
			{
				cout << greeting;
				c += greeting.size();
			}
			else
			{
				// Tests if we are on the border
				if (r == 0 || r == rows - 1 || c == 0 || c == cols -1)
					cout << "*";
				else
					cout << " ";
				c++;
			}
		}

		cout << endl;
	}


	std::cout << "Press ENTER to continue...";
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

    return 0;	
}
Last edited on
thx thx




I'm trying to study for my test coming up and I seriously get everything wrong. How the heck do you learn c++ I do all my homework but I can't seem to get the hang of it.

Take these true or false exercises.
The expression
(ch>='A' && ch<='Z')

evaluates to to false if either ch<'A' or ch>='Z'

I said that this was true and the book says false can you tell me why?

Suppose the input is 5. The output of the code:
cin>>num;
if(num>5)
cout<<nnum;
num=0;
else
cout<<"Num is zero"<<endl;



is :Num is zero


they said it was false I said true. The thing that gets me is there is nothing to explain why it is! OK so its false it doesn't help me learn it I need to know why.


THanks!



closed account (9y8C5Di1)
This is what you do...

const unsigned short limit=12;

string a="Hey there bitch i am a servant on this planet, give me something to do";

unsigned short doNextLineIfLimit(string elem){

for(unsigned x=0,y=0;elem[x]!='\0';x++,y++){cout<<elem[x];
if(y==limit){y=0;cout<<endl;
}
}
return x;
}

Then you can define your "**************" and such;


Last edited on
closed account (9y8C5Di1)
"I'm trying to study for my test coming up and I seriously get everything wrong. How the heck do you learn c++ I do all my homework but I can't seem to get the hang of it.

Take these true or false exercises.
The expression
(ch>='A' && ch<='Z')

evaluates to to false if either ch<'A' or ch>='Z'

I said that this was true and the book says false can you tell me why?"


Look at an ASCII Table, these are actually numbers represented as characters.
Yeah I know but I don't get it if ch was 64 ( whatever that character is wouldn't it) wouldn't the expression be false because the left side is false ?
You know because it is a && statement?
Is that wrong?
closed account (3qX21hU5)
I was having the same problems all the text books I got sucked and really didn't define what everything was. So my question is what books are you currently studying with?

For me the books accelerated c++, and c++ primer the fourth edition really really helped. The code I posted above they teach you how to do in the second chapter on accelerated c++ and the primer goes more in detail on the subjects.

It might be worth checking them out or if you taking classes talk to your professor and lett him know your having some problems or find another student that can help ya.
Last edited on
closed account (9y8C5Di1)
"Yeah I know but I don't get it if ch was 64 ( whatever that character is wouldn't it) wouldn't the expression be false because the left side is false ?
You know because it is a && statement?
Is that wrong?"

-jillie89

Let's see.

a = 24,b=30,c=46;

if (a=>b && a<=c)

a is not equal to or higher than b, but a is equal to or less than c.
However, since the code block will only execute if a is equal to or higher than b AND a is less than or equal to c, it will not execute and therefore result to FALSE.

Did this answer your question?

Both sides must be equal in order for the following code to execute.

Like this: (A&&B),

if you have multiple conditional statements like this:

(A&&B) || C)

Well, you know what happens.

I don't know if this answered your question, but anyway, it's something.


I have the book c++ programming from problem analysis to program design
by D.S. Malik
it is the international edition it was cheaper.
What is this primer your talking about can you give me more info Maybe I can grab it from the library.
Thanks
Topic archived. No new replies allowed.