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
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.
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.
// 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
constint 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;
}
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.
"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?
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.
"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