If any of you are on that can help, please respond. time is of the essance!!!
Announcement
Collapse
No announcement yet.
help with a C program (very basic).
Collapse
X
-
help with a C program (very basic).
"Mal nommer les choses, c'est accroître le malheur du monde" - Camus (thanks Davout)
"I thought you must be dead ..." he said simply. "So did I for a while," said Ford, "and then I decided I was a lemon for a couple of weeks. A kept myself amused all that time jumping in and out of a gin and tonic."Tags: None
-
#include
#define NUM_COLUMNS 80
main()
{
int maxChars = 9999;
char newLine[maxChars];
char data [maxChars];
int c=0, i=0, y=0, x=0, z=0, numChars=0, done=0, numSpaces=0 ;
int beginOfLine, temp;
while ((c = getchar()) != EOF)
{
if (c == '\t')
c = ' ';
data [x] = c;
++numChars;
++x;
}
if (numChars > NUM_COLUMNS)
for (i=NUM_COLUMNS-1; i <= numChars; i += NUM_COLUMNS)
{
numSpaces=0;
y = i;
while(data[y] != ' ')
y--;
data [y] = '\n';
//************************************************** *******everything works above here
/* numSpaces = NUM_COLUMNS - y;
beginOfLine = i-NUM_COLUMNS+1;
for (z = beginOfLine; z < beginOfLine + NUM_COLUMNS+1; z++)
{
c=0;
newLine[c]=data[z];
while(numSpaces >=0)
{
/* c=0;
newLine[c]=data[z]; */
if (newLine[c]==' ')
{
temp=data[z+1];
newLine[c+1]=' ';
newLine[c+2]=temp;
c+=3;
z++;
numSpaces--;
}
else
{
c++;
z++;
}
}
printf("\n%s\n", newLine); */
}
}
printf("\n%s\n", data);
}
Then (and this is the part that doesn't work) i'm tring to justify each line to 80 chars by doubling the white spaces.
And that is where i have my problem..."Mal nommer les choses, c'est accroître le malheur du monde" - Camus (thanks Davout)
"I thought you must be dead ..." he said simply. "So did I for a while," said Ford, "and then I decided I was a lemon for a couple of weeks. A kept myself amused all that time jumping in and out of a gin and tonic."
Comment
-
one error in that code...the last close comment should be one bracker lower. Also, if you need a sample file, and a working version of the program, i can send it to you."Mal nommer les choses, c'est accroître le malheur du monde" - Camus (thanks Davout)
"I thought you must be dead ..." he said simply. "So did I for a while," said Ford, "and then I decided I was a lemon for a couple of weeks. A kept myself amused all that time jumping in and out of a gin and tonic."
Comment
-
another mistake i made. I need to add the spaces before i'm adding the /n chars..."Mal nommer les choses, c'est accroître le malheur du monde" - Camus (thanks Davout)
"I thought you must be dead ..." he said simply. "So did I for a while," said Ford, "and then I decided I was a lemon for a couple of weeks. A kept myself amused all that time jumping in and out of a gin and tonic."
Comment
-
I had a hard time to get it even compiling, due to the mistake in the comments and the commented out {}'s. Yes, send me a sample file to "sirralph at gmx dot com". I'm away for 30 minutes now and will look into it after I return. I hope that's ok with you, I'm at work.
Comment
-
nevermind, it should work the way it is..."Mal nommer les choses, c'est accroître le malheur du monde" - Camus (thanks Davout)
"I thought you must be dead ..." he said simply. "So did I for a while," said Ford, "and then I decided I was a lemon for a couple of weeks. A kept myself amused all that time jumping in and out of a gin and tonic."
Comment
-
As you wish. Btw: This can't work:
int maxChars = 9999;
char newLine[maxChars];
char data [maxChars];
You can't declare arrays in C with variable sizes. If you want to do this, you have to allocate it dynamically:
In plain C:
int maxChars = 9999;
char* newLine = (char*)malloc(maxChars);
char* data = (char*)malloc(maxChars);
...
free(data);
free(newLine);
or in C++:
int maxChars = 9999;
char* newLine = new char[maxChars];
char* data = new char[maxChars];
...
delete data;
delete newLine;
Comment
-
oh no, i know it doesn't work, i was just talking about the logic of it. i'll send you the samples..."Mal nommer les choses, c'est accroître le malheur du monde" - Camus (thanks Davout)
"I thought you must be dead ..." he said simply. "So did I for a while," said Ford, "and then I decided I was a lemon for a couple of weeks. A kept myself amused all that time jumping in and out of a gin and tonic."
Comment
-
i appreciate your help!!!"Mal nommer les choses, c'est accroître le malheur du monde" - Camus (thanks Davout)
"I thought you must be dead ..." he said simply. "So did I for a while," said Ford, "and then I decided I was a lemon for a couple of weeks. A kept myself amused all that time jumping in and out of a gin and tonic."
Comment
-
that is program she let us use to see how it was supposed to work. I don't have the source for it...I'll send you what i have"Mal nommer les choses, c'est accroître le malheur du monde" - Camus (thanks Davout)
"I thought you must be dead ..." he said simply. "So did I for a while," said Ford, "and then I decided I was a lemon for a couple of weeks. A kept myself amused all that time jumping in and out of a gin and tonic."
Comment
-
Well, I found 2 bugs so far, both the same thing:
You have in the second half of the program two loops with a position variable c. First mistake, in both loops c is set to 0 in every run of the loop. That is most likely incorrect. Place the two "c = 0;" lines before the for and while lines.
Second, in the first of these loops, c is never counted up, thus always replacing the 0th character of newLine. I think, the corrected statement in the first loop should be "newLine[c++] = data[z];".
It still doesn't work entirely, but at least better. May be this helps you to hunt the rest of the errors down. I must work a bit for my money and will look into this thread again later.
Comment
-
lol...thanks again"Mal nommer les choses, c'est accroître le malheur du monde" - Camus (thanks Davout)
"I thought you must be dead ..." he said simply. "So did I for a while," said Ford, "and then I decided I was a lemon for a couple of weeks. A kept myself amused all that time jumping in and out of a gin and tonic."
Comment
Comment