PDA

View Full Version : A little programming help



Tranquility
2005-Mar-13, 12:55 AM
I hate to ask this again, but, well, I've been working on this program from 2 am to 4:50 am my time and I still can't figure out why it isn't working :D . Maybe the timing and coffee is getting to my head and turning it into noodle soup.

Anyway the question is to read a number of lines from a user. We assume that 85 characters can be entered per line. Now after reading in the lines, I'm supposed to justify them, printing out 65 characters for each line. So if the 65th character is a character followed by a space, you go to the next line and keep printing out the string. If the last character is a letter followed by another letter, I put a dash and go to the next line. If the last character is a space, I have to put spaces between the words.
I'm fine with the first two conditions, but I just can't get my head around the last aspect of the program. Here's the code so far:


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

void main ()
{
int lines;
char buf[86];
char buf1[65];
//you can have 85 characters in 8.5 inches

cout << "How many lines do you want to enter?" << endl;
cin >> lines;

char **array = new char* [lines];

cout << "Enter the text:" << endl;

for (int i = 0; i <= lines; i++)
{
cin.getline(buf, sizeof(buf));
array[i] = new char [strlen(buf)+1];
strcpy(array[i],buf);
}

cout << "Printing the lines in justified format:" << endl;

for (i = 1; i <= lines; i++)
{
for (int j = 0; j < 85; j++)
{
if (array[i][j] == '\0')
break;

strcpy (buf1, array[i]);
cout << array[i][j];

if (array[i][65] == ' ')
{
while (array[i][65] == ' ')
{
char *p = strtok (buf1," ");

if (array[i][j] == ' ')
{
p = strtok (NULL, " ");
cout << p << " ";
}
}
}

if (j == 65)
{
if (array[i][64] != ' ' && array[i][65] == ' ')
cout << "-" << endl;

if (array[i][64] != ' ' && array[i][65] != ' ')
cout << endl;
}

}

cout << endl;
}

}

Any help with this?

Tranquility
2005-Mar-13, 01:12 AM
I amended the code a little bit.


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

void main ()
{
int lines;
char buf[86];
char buf1[65];
//you can have 85 characters in 8.5 inches

cout << "How many lines do you want to enter?" << endl;
cin >> lines;

char **array = new char* [lines];

cout << "Enter the text:" << endl;

for (int i = 0; i <= lines; i++)
{
cin.getline(buf, sizeof(buf));
array[i] = new char [strlen(buf)+1];
strcpy(array[i],buf);
}

cout << "Printing the lines in justified format:" << endl;

for (i = 1; i <= lines; i++)
{
for (int j = 0; j < 85; j++)
{
if (array[i][j] == '\0')
break;

strcpy (buf1, array[i]);
cout << array[i][j];

if (array[i][65] == ' ' && array[i][j] == ' ')
{
cout << " ";
}

if (j == 65)
{
if (array[i][64] != ' ' && array[i][65] == ' ')
cout << "-" << endl;

if (array[i][64] != ' ' && array[i][65] != ' ')
cout << endl;
}

}

cout << endl;
}

}

It isnt justifying a line if it has less than 65 characters. The reason is probably because \0 is stored at the end of the string, so the 65th character is never a space if the string is smaller than 65 characters.

wedgebert
2005-Mar-13, 02:17 AM
My C++ is a bit rusty (I'm a VB.NET programmer by trade), I'll try to help.

I don't think I understand the problem you're having though. As far as I can tell, the program does the following:



Gets number of lines to be entered (lines)

Retreive input (lines + 1) number of times, store each line of input inside of array.

For each line entered:
{
For each Character in current line
{
If Character is Null, exit loop
Copy Current Line into buf1 (possible buffer overflow here, CurLine can be 87 characters, but buf1 is only 66 characters long) buf1 is also never used again.

Output current character

If current character is a space and character 66 is a space, output another space (do you really want to have two spaces for every space if the last character is also a space?)

If current character is number 66
{
If previous character is not a space and this character is a space, ouput a hypen and a new line.
If the previous character is not a space and this character is not space output a new line.
}
}
Output new line.
}

I guess the question is, what you mean by justified? This program seems to add an extra line break if the 65th character is not a space, if it IS a space, then it doubles all spaces in the line.

Candy
2005-Mar-14, 06:22 PM
Did you add break points to determine exactly where the program is malfunctioning? I'm very fresh to VB.NET 2003, so forgive me if I am giving you misleading advise.

[edited a word]

Majin Vegeta
2005-Mar-14, 06:38 PM
Could someone teach me how to do coding privately or something? I am a quick learner.

Musashi
2005-Mar-14, 06:46 PM
Best bet is a book. Microsoft has this "step-by-step" series that is pretty good for beginners.

jfribrg
2005-Mar-14, 07:02 PM
Could someone teach me how to do coding privately or something? I am a quick learner.

Doesn't your professor have office hours?

Candy
2005-Mar-14, 07:04 PM
Could someone teach me how to do coding privately or something? I am a quick learner.
PM me your address, and I'll send you an extra copy I have with all the software (for free). :D

Majin Vegeta
2005-Mar-14, 07:31 PM
No thanks Candy, I don't give out personal details.

I can learn somewhere else I guess... :lol: