URGENT C++ text setter program

the problem is as follows my teacher wants me to:
write a program that reads lines of text into a string using the getline(cin,line) function and then type sets these lines of text so that they are column aligned on the left and right margins as shown in the second example above.

So it looks like this:

"Beware____the____Jabberwock,____my___son! without the underscores just wanted to put those in so you know what i mean by the spaces.

HELP ME PLEASE!! ive been working on it for liek 3 days i cannot get it for the life of me!!!
If i didn't have Dysgraphia i would tell you how to do this in like 100 lines of code.
Just a guess, but could you just find the width of the column that you need to spread the text over as well as the width of each individual word, then divide to find out where each word should go? Then you could just insert blank spaces in between.

If not why not? I'm pretty inexperienced with C++ so I'd be curious to know what goes wrong.
yea i got it to work guys... it was crazy here was the answer if you are curious...

#include <iostream>
#include <cstring>
using namespace std;

const int targetLength=43;
int lineLength;
int numofSpaces;
int padNeeded;
int i, j;
char page[100][50];

int main () {
int x;
char input[80];
char output[80];

while(true) {
cin.getline(input, 80);
if(input[0]=='&') break;

lineLength = strlen(input);
numofSpaces = 0;
for (int i=0; i<lineLength; i++) {
if (input[i] == ' ') numofSpaces++;
}

output[0]=0;
for (int i=0, j=0; input[j]!=0; )
{
output[i] = input[j++];
if (output[i++] == ' ')
{
if (numofSpaces > 0) padNeeded = (targetLength-lineLength)/numofSpaces;
for (int k=0; k<padNeeded; k++) {
output[i++]= ' ';
}
numofSpaces= numofSpaces-1;
lineLength=lineLength+padNeeded;
}
output[i]=0;
}
cout<<output<<'\n';

}
cout << "\n\n\n";


}
Topic archived. No new replies allowed.