PDA

View Full Version : What could be wrong with this program?



dE_logics
November 10th, 2009, 03:57 PM
#include <stdio.h>
main()
{


//Notice, whenever I say 'jockey' or 'from', I actually mean input[jockey] and input[from] unless stated explicitly that they are indexes.
/*
Certain lines from the input will be selected for comparison with the rest of the lines...so the program will be separated in 2 parts -

1) Mechanism to select the line
2) Mechanism to compare the selected lines with all the lines below it.

The variables jockey and from will determine which lines are selected...the comparison will run between this and the other lines.

1) Never reset the value of jockey. Search for a \n after the the current value of jockey and proceed to step 2. If '\0' exists instead of '\n', then come out of the loop.
2) The value of from will always be the previous value of jockey + 2...that's cause jockey always points to the end of the new line.
4) Implementing search (algorithm later).
*/


//Input will be stored to this array.
char input[1000];
///make jockey unsigned.
short jockey = -1;
short from = 0;
//Full form -- Search_index, this will work as an increment in value of jockey, the incremented value of jockey will be an element of the next line...since jockey point to a character before '\n', shr_indx's minimum value will be 2.
short shr_indx = 2;
//This variable will store 2 indexes (in a column) of input which are suspected to be duplicates. These indexes will be a part of the duplicate lines.
unsigned char dupes[2][10];
//indexes of dupes
char l = 0;
char m = 0;
//Initiating random value of input.
input[0] = 'g';
//Inputting
printf("\nPaste the list of all contents in /portage/distfiles; this can be generated by running ls while in /portage/disfiles\nEnsure to type a '!' after pasting.\n");
//utilizing variable 'from' temporarily
while(input[from] != '!')
{
///Remove the ifs
if(from > 0)
from++;
input[from] = getchar();
if(from == 0)
from++;
}
input[from] = '\0';
from = 0;
printf("DID I EVER GET TO THIS STEP???");//DEBUG
//The line selection mechanism starts
while (input[jockey + 1] != '\0')
{
from = jockey + 2;
///Remove this if.
if(jockey == -1)
jockey = 0;
while(input[jockey] != '\n' && input[jockey] != '\0')
jockey++;
jockey--;
//Searching mechanism starts
/*



The letters from 'from' will be compared with jockey + shr_indx...jockey + shr_indx will point the characters of the line to which the comparison is made, initial value is always 2 no matter what the value of jockey is; these will be the following conditions -
1) If it's identical and equal to '-' proceed to the next step; if they are equal but do not have '-' proceed to step 3; if they are not identical proceed to step 2
1.1) Increment value of srh_index and from and see if both from and jockey + srh_indx points to a number...if so move to next step, otherwise goto step 2.
1.1.1) Export value of srh_indx and from to corresponding columns of dupes and proceed to step 2
2) Increment value of srh_indx to point the next line and goto step 1, if next line does not exist...the array ends, then exit loop but before that set the value of shr_indx to 2.
3) Increment value of srh_indx and from by 1 and proceed to step 1.



*/
//Resetting value of shr_index
shr_indx = 2;
while(input[jockey + shr_indx] != '\0')
{
if(input[from] == input[jockey + shr_indx])
{
if(input[from] == '-')
{
if(input[jockey + shr_indx + 1]>=49 && input[jockey + shr_indx + 1] <= 47 && input[jockey + shr_indx + 1] == input[from + 1])
{
dupes[l][m] = input[from];
m++;
dupes[l][m] = shr_indx;
m = 0;
l++;
while(input[jockey + shr_indx] != '\n' && input[shr_indx] != '\0')
{
shr_indx++;
}
if(input[jockey + shr_indx] != '\0')
shr_indx++;
}
else
{
while(input[jockey + shr_indx] != '\n' && input[shr_indx] != '\0')
{
shr_indx++;
}
if(input[jockey + shr_indx] != '\0')
shr_indx++;
}
}
else
{
from++;
shr_indx++;
}
}
else
{
while(input[jockey + shr_indx] != '\n' && input[shr_indx] != '\0')
{
shr_indx++;
}
if(input[jockey + shr_indx] != '\0')
shr_indx++;
}
}
}
}

Just ignore the comments.

The program get's stuck in this part -


while(input[from] != '!')
{
///Remove the ifs
if(from > 0)
from++;
input[from] = getchar();
if(from == 0)
from++;
}
input[from] = '\0';
from = 0;
printf("DID I EVER GET TO THIS STEP???");//DEBUG

according to my diagnosis, it never comes out of the loop...if I make something print out of this loop, it never get printed...for this piece, it's "DID I EVER GET TO THIS STEP???"...it never get executed.

However if I trim the lower parts of the program and make it -


#include <stdio.h>
main()
{


//Notice, whenever I say 'jockey' or 'from', I actually mean input[jockey] and input[from] unless stated explicitly that they are indexes.
/*
Certain lines from the input will be selected for comparison with the rest of the lines...so the program will be separated in 2 parts -

1) Mechanism to select the line
2) Mechanism to compare the selected lines with all the lines below it.

The variables jockey and from will determine which lines are selected...the comparison will run between this and the other lines.

1) Never reset the value of jockey. Search for a \n after the the current value of jockey and proceed to step 2. If '\0' exists instead of '\n', then come out of the loop.
2) The value of from will always be the previous value of jockey + 2...that's cause jockey always points to the end of the new line.
4) Implementing search (algorithm later).
*/


//Input will be stored to this array.
char input[1000];
///make jockey unsigned.
short jockey = -1;
short from = 0;
//Full form -- Search_index, this will work as an increment in value of jockey, the incremented value of jockey will be an element of the next line...since jockey point to a character before '\n', shr_indx's minimum value will be 2.
short shr_indx = 2;
//This variable will store 2 indexes (in a column) of input which are suspected to be duplicates. These indexes will be a part of the duplicate lines.
unsigned char dupes[2][10];
//indexes of dupes
char l = 0;
char m = 0;
//Initiating random value of input.
input[0] = 'g';
//Inputting
printf("\nPaste the list of all contents in /portage/distfiles; this can be generated by running ls while in /portage/disfiles\nEnsure to type a '!' after pasting.\n");
//utilizing variable 'from' temporarily
while(input[from] != '!')
{
///Remove the ifs
if(from > 0)
from++;
input[from] = getchar();
if(from == 0)
from++;
}
input[from] = '\0';
from = 0;
printf("DID I EVER GET TO THIS STEP???");//DEBUG
}

"DID I EVER GET TO THIS STEP???" gets printed!!! :o :mad: :confused:

Arndt
November 10th, 2009, 04:03 PM
The program get's stuck in this part -


while(input[from] != '!')
{
///Remove the ifs
if(from > 0)
from++;
input[from] = getchar();
if(from == 0)
from++;
}
input[from] = '\0';
from = 0;
printf("DID I EVER GET TO THIS STEP???");//DEBUG




You don't print a newline here, so the line will only appear when a newline is printed later (because of line buffering). That probably accounts for the mystery.

PmDematagoda
November 10th, 2009, 04:50 PM
Your while loop logic is wrong. For example, if four characters were entered excluding the '!', then the loop would go like this:-


from = 0
array[from] = char
from = 1

array[from] == '!' ?
from = 2
array[from] = char
do you see the problem?

You are not only trying to use elements in an uninitialised array you are also skipping some parts.

I think this loop may be better:-

do{
input[from] = getchar();

} while(input[from++] != '!');
it is cleaner and does the job.

Also, please try and stick to standard C99, things like the implicit int rule are not there anymore, and in any case, you aren't returning a value at the end of the program either, and there are other errors too, which you can see by compiling the program with -Wall specified to GCC.

dE_logics
November 10th, 2009, 05:00 PM
You don't print a newline here, so the line will only appear when a newline is printed later (because of line buffering). That probably accounts for the mystery.

Yes, that worked, thanks.

dE_logics
November 10th, 2009, 05:03 PM
Your while loop logic is wrong. For example, if four characters were entered excluding the '!', then the loop would go like this:-


from = 0
array[from] = char
from = 1

array[from] == '!' ?
from = 2
array[from] = char
do you see the problem?

You are not only trying to use elements in an uninitialised array you are also skipping some parts.

I think this loop may be better:-

do{
input[from] = getchar();

} while(input[from++] != '!');
it is cleaner and does the job.

Also, please try and stick to standard C99, things like the implicit int rule are not there anymore, and in any case, you aren't returning a value at the end of the program either, and there are other errors too, which you can see by compiling the program with -Wall specified to GCC.

hummm...thanks for the info I'll see to it.

I'm just a beginner, so all that will come later...the 'code standards' for Linux.

PmDematagoda
November 10th, 2009, 05:12 PM
I'm just a beginner, so all that will come later...the 'code standards' for Linux.

Well, actually that standard is supposed to apply for all C code, the implicit int rule was in C89 which isn't supposed to be followed now. :)

I can recommend the C Complete Reference by Herbert Schildt, it is really useful.

Edit: The book does have some tutorials in it.

Tony Flury
November 10th, 2009, 05:42 PM
hummm...thanks for the info I'll see to it.

I'm just a beginner, so all that will come later...the 'code standards' for Linux.

In my opinion, that is a bad move - start with good habits - that way you wont have any bad habits to break later.

dwhitney67
November 10th, 2009, 05:48 PM
[CODE]
...
Just ignore the comments.
...

How can I? Your excessive use of comments make the code hard to digest. You don't need to place a comment on every line!