i'm a newbie to programming and i just wrote a program in 'C' but i'm having problem running it.It compiles, however, when i run it, it crashes on windows.Can anyone tell what i've done wrong?? if possible give me a solution...

converter.c

#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>

#include "Converter.h"

myConvertStruct* Con()
{
     myConvertStruct* aCon = (myConvertStruct*) malloc(sizeof(myConvertStruct));
     //allocating memory to aCon

     return aCon;
     //returning aCon
}           


char *substr(const char *pstr, int start, int numchars)
{
    char *pnew = malloc(numchars+1);
    strncpy(pnew, pstr + start, numchars);
    pnew[numchars] = '\0';
    return pnew;
}


void del(char* input)
{
	int length = strlen(input); //int length = input.length();
	int i;
	char ch; // char ch;
	char* temp; // string temp;

	for ( i = 0; i < length ; i++ ) // for(int i=0;i<length;i++)
		{
		ch = input[i]; // ch = input[i];
		if( ch != ' ' && ch != '\n' ) // if(ch!=' ' && ch!='\n')
			{
			temp = substr(input,i,i + length); // temp=input.substr(i,length-i);
            break; // break;
			}	
		}
		strcpy( input, temp ); //input=temp;
} 



void extInput(char* filename)
{
     FILE *file;
     file = fopen("textFile", "r");
    // file = fopen(filename.c_str()); //  MAKE CODE CHANGE HERE
     char ch; // char ch;
     int counter = 0; //  int counter=0;
     
     char* str; // string str;
     while(!(feof(file))) //  
     {
         do
         {
            ch = fgetc(file); 
            str = str + ch;
         }while( ch!='?' && ch!='!' &&  ch!='.' );       
         
         
        del(str);//remove trailing space at the start of the string
          
        counter++;
		printf("\nTextLine No.: %d\n", counter); // cout<<"\nTextLine No.: "<<counter<<endl;
		printf("String on that line : %s\n", str); // cout<<"String on that line : "<<str<<endl;
		extWord(str); // extWord(str);  
         
        strcpy(str,"\n");     //  RECHECK CODE
     }
     
     
}



void extWord(char* str)
{
	char* word; // string word;
    int length = strlen(str); // int length = str.length();//because of punctuation mark!
	char puntuation = str[length-1]; // char punctuation = str[str.length()-1];//get the punctuation
	char ch; // char ch;
    int first = 0, second = 0; // int first=0,second=0;
    int i;

	printf("Madam Segue format: "); // cout<<"Madam Segue format: ";

	for( i = 0 ; i < length ; i++ ) // for(int i=0;i<length;i++)
		{
		ch = str[i]; // ch= str[i];
		if(ch ==' '  || ch =='.' || ch =='!') // if(ch==' '  || ch=='.' || ch=='!')
			{
			second = i; // second=i;
			word = substr(str,first,first + second); 
			printf("%s", convert(word), ch);  
			first = second + 1 ;
			}
		}
	printf("\n"); // cout<<endl;
}


// checks whether a character is a special consonant,i.e, either 'l' or 'h'
// or 'r' or 'w'
int specialCharacter(char c)
	{
        int valid = 0;
        if (c=='l' || c=='h' || c=='r' || c=='w')
        {             
             valid = 1;
        }
        return valid;
	}
	
// checks whether a character is a vowel
int vowel(char c)
	{
        int valid = 0;
        if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u' 
		     || c=='A' || c=='E' || c=='I' || c=='O' || c=='U')
        {             
             valid = 1;
        }
        return valid;       
	}

// method converter is the really method that converts the given input into
// madame segue format	
char* convert(char* input)
	{
                    
        char* Expr;
        char* newExpr;
		int size = strlen(input);
        char* str1;
        char* str2;
        char* temp;

        if(size==1)
        
        strcpy(Expr,input);
        strcat(Expr,"G");
        strcat(Expr,input);
        strcpy(newExpr, input);
        return newExpr;
        
        //return(input + "G" + input);
      
           
//case 1: All-vowel-word section
//This section consists of words containing only vowels.

		int vowelsOnly = 1;
		int i = 0;
		while(i<size && vowelsOnly == 1)
		{
			if(!vowel(input[i]))
			{
				vowelsOnly	= 0;
            }
            i++;
        }
        /*
  		for(int i=0;i<size ;i++)
		{

		}
		*/
		if(vowelsOnly)
		{
            char* str1 = input;
            char* str2 = "G";
            strcat(str1, str2);
            strcat(str1,input);
            return str1;
        }
		
		if(input[size-1]=='y')
		{
            if(!vowel(input[size-2]))
            {
                str1 = substr(input,0,size-2);
                str2 = substr(input,size-2,size-2+2);
            }
            
            else
            {
                str1 = substr(input,0,size-3);
                str2 = substr(input,size-3,size-3+3);
            }
            
            temp = str2;
            temp[0] = 'G';
            
            char* str3 = convert( str1);
            char* str4 = ",";
            char* str5 = str2;
            char* str6 = temp;
            strcat( str3, str4);
            strcat( str3, str5);
            strcat( str3, str6);
            return str3;

            //return (convert(str1) + "," + str2+temp);
        }
		
		
//case 2: Multiple-letter section
// A string qualifies as multiple-letter section if:
// - It ends with a vowel which is either at the end of the word or is followed
//   by another consonant. A multiple-letter section can only end with a
//   consonant if the consonant ends the word.
//AND
// - The second letter in the string is either a vowel or anyone of the
//   following consonants: l, h, r, w.

		if(!vowel(input[0]))
		{
             char ch;
             
             
             if(input[size-1]=='e')
             {
                 if(input[size-2]=='u')
                 {
                    str1 = substr(input,0,size-3);
                    str2 = substr(input,size-3,size-3+3);
                    temp = str2;
                    temp[0] = 'G';
                    
                    char* str3 = convert( str1);
                    char* str4 = ",";
                    char* str5 = str2;
                    char* str6 = temp;
                    strcat( str3, str4);
                    strcat( str3, str5);
                    strcat( str3, str6);
                    return str3;
                    //return(convert(str1)+ ","  + str2 + temp);
                 }
                 
                 int i = size-2;
                 for(i;i>=0;i--)
                 {
                    if(vowel(input[i]))
                    {
                       if(vowel(input[i-1]))
                       {}
                       
                       else if(i-1!=0)
                       {
                           if(!specialCharacter(input[i-1]))
                           {
                              str1 = substr(input,0,i-1);
                              str2 = substr(input,i-1,(i-1)+size-i+1);
                           }
                           
                           else
                           {
                               str1 = substr(input,0,i-2);
                               str2 = substr(input,i-2,(i-2)+size-i+2);
                           }
                           
                           temp = str2;
                           temp[0] = 'G';
                           
                           char* str3 = convert( str1);
                           char* str4 = ",";
                           char* str5 = str2;
                           char* str6 = temp;
                           strcat( str3, str4);
                           strcat( str3, str5);
                           strcat( str3, str6);
                           return str3;
                           //return(convert(str1) +"," + str2  + temp);
                       }
                       
                       else
                       {
                           temp=input;
                           temp[0]='G';
                           
                           char* str3 = input;
                           strcat(str3, temp);
                           return str3;
                           //return(input+temp);
                       }
                    }
                 }
             }

             int i = 1;
             for(i;i<size;i++)
             {
                  ch=input[i];
                  str1 = substr(input,0,i);
                  str2 = substr(input,i,i+size-i);




                  if(strlen( str2 )>1 && !vowel(str2[0]))
                  {
                      if(validMultiWord(str1) && validMultiWord(str2))
                      {
                           char* temp1 = str1;
                           temp1[0] = 'G';
                           
                           char* str3 = str1;
                           char* str4 = temp1;
                           char* str5 = ",";
                           char* str6 = convert(str2);
                           strcat(str3,str4);
                           strcat(str3,str5);
                           strcat(str3,str6);
                           return str3;
                           //return(str1+temp1+","+convert(str2));
                      }
                  }
 
 
                  else if(strlen(str2)==1)
                  {
                       str1 = input;
                       str1[0] = 'G';
                       
                       char* str3 = str1;
                       char* str4 = input;
                       strcat( str4, str3);
                       //return(input + str1);
                  }

             }
             
              char* temp1 = str1;
              temp1[0] = 'G';
              
              char* str3 = str1;
              char* str4 = temp1;
              char* str5 = ",";
              char* str6 = convert(str2);
              strcat(str3,str4);
              strcat(str3,str5);
              strcat(str3,str6);
              return str3;
              //return(str1+temp1+","+convert(str2));
		}
		

//case 3:  Starting-vowel section
//This section is made up of a single vowel which is the first letter of a word
 
		else
		{
            int consonantOnly = 1;
            
            int i = 1;
            while(i<size && consonantOnly==1)
            //for(int i=1;i<size && consonantOnly;i++)
            {
                if(vowel(input[i]))
                {
                    consonantOnly = 0;
                }
                i++;
            }
            
            if(consonantOnly)
            {
                char* str1 = input;
                char* str2 = "G";
                strcat(str1, str2);
                strcat(str1,input);
                return str1;             
                //return(input + "G" + input);
            }
            
            int j = 1;
            for(j;j<size;j++)
            {
                str1 = substr(input,0,i);
                str2 = substr(input,i,i+size-i);
               
                if(validMultiWord(str2))
                {
                    char* str3 = str1;
                    char* str4 = "G";
                    char* str5 = str1;
                    char* str6 = ",";
                    char* str7 = convert(str2);
                    strcat( str3, str4);
                    strcat( str3, str5);
                    strcat( str3, str6);
                    strcat( str3, str7);
                    return str3;                    
                    //return(str1+"G"+str1+","+convert(str2));
                }
                
            }
       }
	}
	
//this method checks for a condition in the case 2 where it decides whether a 
// string qualifies to be a multiple letter string 	
int validMultiWord(char* str)
	{
		char secondLetter = str[1];// secondLetter is the 2nd letter of the word
		                           // this is used to check for the case 2; 
                                   // where the str qualifies to be a mutiple
                                   // letter string              
		
		return(vowel(secondLetter) || specialCharacter(secondLetter));
		
		                           // if the 2nd letter is a vowel or a special
                                   /// consonantthen this method will return true
	}

converter.h

#ifndef CONVERTER_H
#define CONVERTER_H

#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>

typedef struct
{
          //
}myConvertStruct;

extern void del(char*);

extern void extInput(char*);

extern void extWord(char*);

extern char* convert(char*);

extern int vowel(char);

extern int specialCharacter(char);

extern int validMultiWord(char*);


#endif

manager.c

#include "Manager.h"   

ManagerStruct* Manager(char* name)
{
              
ManagerStruct* man = (ManagerStruct*) malloc(sizeof(ManagerStruct));
                            
    man->filename = name;  
    
   // myConvertStruct* c;

    int ok = chkFile();
    if(ok)
    {
                     
        extInput(man->filename);
    }
    else
    {   
         system("pause");
         exit(0);
    }
}

// a method to check whether the file exits or contains invalid characters
int chkFile(ManagerStruct* m)
{
    int valid = 0;
    //check if file exists
        
     //ifstream file;
     //file.open(m->filename.c_str());
     FILE *file;
     file = fopen(m->filename, "r");
     if(!file)
     {
         printf("Error:File does not exists");
         return valid;
     }    
         
     char ch;
     
     // while not end of file continue checking if there are invalid characters
     do
     {
          ch = fgetc(file);             // getting a character in the file
         if((!isalpha(ch)) && ch!='?' && ch!='!' && ch!='.'
          && ch!=',' && ch!=' ' && ch!='\n'  && ch!=EOF)
         {
           printf("Error: File contains invalid character(s)");
           return valid;
         }
     }while(!feof(file));     
        
     fclose(file);       // closing file
     return valid = 1;  
}

manager.h

#ifndef MANAGER_H
#define MANAGER_H

#include "Converter.h"

typedef struct
{
        char* filename;
        
}ManagerStruct;

             extern ManagerStruct* Manager(char *);
             extern int chkCap();
             extern int chkFile();
             


#endif

main.c

#include "Manager.h"


int main()
{
    ManagerStruct* aMan;
    printf("Enter filename:");  // output "Enter filename: " on the screen
    
    char* textFile;
    scanf(textFile);           // storing user input into textFile
    
    Manager(textFile);
    
    system("PAUSE");
    return 0; 

}
// file = fopen(filename.c_str()); //  MAKE CODE CHANGE HERE
     char ch; // char ch;
     int counter = 0; //  int counter=0;
     
     char* str; // string str;
     while(!(feof(file))) //  
     {
         do
         {
            ch = fgetc(file); 
            str = str + ch;

Well the first thing is to decide whether you're using C or C++.
Because at the moment, it's a horrid mess of both (or at least, it's trying to be).

The really big problem for you at the moment is things like this
> str = str + ch;
In C++, this would probably accumulate characters in a string.
In C, it simply moves the pointer from one uninitialised location to another.


You do this in several places, just doing
char *something;
Then proceeding to use it without bothering to initialise it at all.
Your attempt at using scanf() with such a variable is especially problematic.

Other problems include
- using feof() in a control loop
- casting the return result of malloc (not necessary in C, compulsory in C++).
- including non-standard header files like malloc.h

If it is C++, then stop messing with all those char* variables and just use std::string variables.

this is supposed to be a C program...not a c++ one....since i'm new to C, i guest there are a lot of mistakes in it....do you have any suggestion to correct these errors??

Member Avatar for iamthwee

this is supposed to be a C program...not a c++ one....since i'm new to C, i guest there are a lot of mistakes in it....do you have any suggestion to correct these errors??

You have to be real careful when manipulating c style strings.

If you check out the code snippet section there should be a few good examples written by one Dave Sinkula. Failing that keep trying and someone here will correct you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.