Thread: code compiles without error, but

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    75

    code compiles without error, but

    hi,
    thanks for your help with that program, i actually cleared all the errors, but on executing the program it does not go through the first loop...any ideas??

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<ctype.h>
    
    void translateline(char [], char []);
    int extractintoew(char [], char [],int);
    void convertintopig(char [], char [] );
    int wherevowel(char []);
    int copyintopl(char [], char [],int);
    
    int main()
    {
    	char el[100]={'\0'},pl[200]={'\0'};
    	int a=0;
    	
    
    	printf("Please enter a line(# to end): ");
    	gets(el);
    
    	while(el[0]!='#')
    	{
    		translateline(el,pl);
    		puts(pl);
    
    		
    
    		printf("\nPlease enter a line(# to end): ");
    		fflush(stdin);
    	    gets(el);
    	}
    
    	printf("Good Bye. Oodgay Ebyay\n");
    	system("PAUSE");
    	return 0;
    }
    
    void translateline(char el[], char pl[])
    {
    	char ew[17]={'\0'},pw[17]={'\0'};
    	int ep=0,pp=0,k=0,r;
    
    	while(el[ep]!='\0')
    	{
    		if(isalpha(el[ep]))
    		{
    			k=extractintoew(el,ew,ep);
    			ep+=k;//this is to keep everything at par, so even here, one behind space or punct
    			convertintopig(ew,pw);//copies the contents of pw into pl, pp at this point is still 0
    			r=copyintopl(pw,pl,pp);//this will return the value of pp, as to where it is in pl so that we can put the punct or space
    			pp+=r;//now pp is set at before that point
    			pp--;//now it is set for pp++
    			pp++;//now ready to accept punct or space position
    			ep++;//now at punct or space position
    		}
    		else
    		{
    			if(isspace(el[ep]))
    			{
    				el[ep]=pl[pp];
    			}
    			else
    			{
    				el[ep]=pl[pp];
    				strcat(pl," ");//after punctuation put a space
    			}
    		
    
    			
    		}
    		
    	}
    	pl[pp]='\0';//setting the last value to null as this is required by the output function
    	
    }//complete
    
    int extractintoew(char el[],char ew[],int ep)
    {
    	int i=0;
    
    	while(isalpha(el[i]))
    	{
    		ew[i]=el[ep];
    		i++;
    		ep++;
    	}
        return ep;//ep at this point is one place behind the space or punctuation and returns that number to 
    }//completed
    
    void convertintopig(char ew[], char pw[])
    {
    	int y,m;
    
    	y=wherevowel(ew);//incomplete at this point
    
    	if(y==0||y==(-1))//this indicates if it begins with a vowel, or has no vowel at all
    	{
    		strcat(ew,"way");
    		strcpy(pw,ew);
    	}
    	else
    	{
    		m=strlen(ew);//calculate the length of the string;
    		memmove(ew,&ew[y],m-y);// m-y indicates that number of alphabets to move
    		strcat(ew,"ay");
    		strcpy(pw,ew);//now pw, has the word with the vowels moved back, and truncated with "ay"
    	}
    }//completed
    
    int wherevowel(char ew[])
    {
    	int j;
    	char *vowel="aeiou";
    
    	if(ew[0]=='a'||ew[0]=='e'||ew[0]=='i'||ew[0]=='o'||ew[0]=='u')
    	{
    		j=0;
    		return j;
    	}
    
    	if(ew[0]=='A'||ew[0]=='E'||ew[0]=='I'||ew[0]=='O'||ew[0]=='U')
    	{
    		j=0;
    		return j;
    	}
    
    	else
    	{
    		j=strcspn(ew,vowel);
    
    		if(j==0)
    		{
    			j=(-1);
    			return j;
    		}
    		else return j;//this value here has the place where j occurs
    	}
    }//completed
    
    int copyintopl(char pw[], char pl[], int pp)
    {
    	int a=0;
    	int b;//declared for strlen of pl
    	b=strlen(pw);
    
    	while(a<b-1)
    	{
    		pw[a]=pl[pp];
    		a++;
    		pp++;
    	}
    
    	return pp;
    }//completed
    Last edited by kashifk; 04-10-2003 at 03:07 PM.

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    > but on executing the program it does not go through the first
    > loop...any ideas??

    Here

    > while(el[0]!='\0')

    the first element of el must be equal to '\0'. Perhaps it isn't? You could do some debugging by adding a printf() in your code to print out what the value of el[0] is before entering the loop.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    75
    yeah that should be #, i changed that, but still it doesnt go thru.

  4. #4
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    >>ep++,pp++;
    This isn't right, you need to seperate these,

    ep++;
    pp++;
    The keyboard is the standard device used to cause computer errors!

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    75
    all changes suggested have been made, but the output spits out nothing

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Can you tell us where in the code things go wrong? I'm not sure if we are talking about the same piece of the code.

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    75
    it is the same piece of code, the error occurs during the outputting of pl, nothing comes out

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    75
    hi
    i have done everything(and have edited the code posted above heavily), why the hell doesnt the puts commands output the value of pl????
    ps:- the code up there right now is the latest one with all the updates and changes.

    thanks for all your help guys, i just dont why it isnt working like its supposed to!!!!

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    75
    no i am on a windows machine, but thanks for that tip there. The thing is the code posted above works for vowels, but that too single vowels, no sentences, and does not work for consonants which i find to be very strange.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by stumon
    >>ep++,pp++;
    This isn't right, you need to seperate these,

    ep++;
    pp++;
    What on earth are you talking about? No you don't. The comma operator in this instance would have the same effect on said code as having them on seperate lines. Illustrated:
    Code:
    #include <stdio.h>
    int main( void )
    {
    	int x,y, xy[20] = {0}, *xp, *yp;
    
    	x = 0; y = 10;
    
    
    	x++,y++;
    	xy[x] = x;
    	xy[y] = y;
    	printf("x is %d, y is %d.\n", x, y );
    
    	x++;
    	y++;
    	xy[x] = x;
    	xy[y] = y;
    	printf("x is %d, y is %d.\n", x, y );
    
    
    	xp = &xy[x-1];
    	yp = &xy[y-1];
    	printf("\nx is %d, y is %d.\n", x, y );
    	printf("xy[%d] is %d, xy[%d] is %d.\n", *xp, x-1, *yp, y-1 );
    	xp++,yp++;
    
    	printf("\nx is %d, y is %d.\n", x, y );
    	printf("xy[%d] is %d, xy[%d] is %d.\nBacktracking...\n", *xp, x-1, *yp, y-1 );
    	xp--,yp--;
    	printf("Advancing...\n");
    	printf("\nx is %d, y is %d.\n", x, y );
    	printf("xy[%d] is %d, xy[%d] is %d.\n", *xp, x-1, *yp, y-1 );
    
    	return 0;
    }
    With or without pointers, it doesn't matter. The comma operator functions the same in the outlined use.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-21-2006, 07:52 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  4. weird code which compiles
    By pinko_liberal in forum C Programming
    Replies: 0
    Last Post: 07-06-2002, 12:52 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM