Thread: The program that i can un with dev c++ but it cannot work with visual

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    5

    The program that i can un with dev c++ but it cannot work with visual

    Print number of a word case-insensitively. For instance in the expression “Van’da van kedisi
    Vanlıların nesesidir” -which is a weird sentence- the word “van” is found 3 times.

    Code:
    void FindWord(char *ptr){
    	
    	int counter=0;
    	char *str;
    	int i=0;
    	int j,f,word=0;
    	char v=' ';
    	char text[15];
    	printf("please enter the text that you will search :");
    	gets(text);
    	int size=strlen(ptr);
    	int size2=strlen(text);
    
    
    	while(i<size){
    	
    		j=f=0;
    
    
    	while(j<size2){
    	
    	if(ptr[i++]!=text[j++]){
    
    
    		f=1;
    		i--;
    		
    
    
    
    
    	}
    	}
    
    
     if((f==0) && (i==size || ptr[i]==' ') && (v==' '))
    	
    		word++;
    
    
    		v=ptr[i++];
    	}
    	printf("the word %d times occured",word);
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    1) Don't use gets().

    2) Don't use Dev C++

    3) Post the entire code.

    4) Name your variables something meaningful not alphabet letters.

    Wait wait... before you post... let me guess: Your main prototype looks like this:

    Code:
    void main()
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Most likely it isn't a difference between VS and dev-c++. You're only allocating 15 bytes, and "Van’da van kedisi Vanlıların nesesidir" is way more than that. And using gets() will cause a buffer overflow either way. It happens to be that the time you ran it with dev-c++, you got lucky and were able to parse it.

    Instead, allocate more memory, use fgets() instead of gets(), and drop dev-c++.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    5
    my main prototype like that :

    Code:
    int main(){
    
    
        char text[500],copy[500];
        
        printf("Please enter text:");
        gets(text);
        strcpy(copy,text);
        Vowel(text);  //other functions
        Consonant(text); //other functions
        Punctuation(text); //other functions
        
        NumberCounter(text); //other functions
        FindWord(text);
        strcpy(copy,text);
    system("pause");
    return 0;
    }
    whatever how can i do this? If i erase that function what kind of method can i use?

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Nevermind the main, I was just messing. Remove gets() and replace it with fgets() (look up the documentation for fgets()) and post your entire code after you change it.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    5
    Code:
    void FindWord(char *ptr){
        
        int counter=0;
        char *str;
    
    
        int i=0;
        int j,f,word=0;
        char v=' ';
        char text[15];
        printf("please enter the text that you will search :");
        fgets(text,15,stdin);
        int size=strlen(ptr);
        int size2=strlen(text);
    
    
        while(i<size){
        
            j=f=0;
    
    
        while(j<size2){
        
        if(ptr[i++]!=text[j++]){
    
    
            f=1;
            i--;
            
    
    
    
    
        }
        }
    
    
     if((f==0) && (i==size || ptr[i]==' ') && (v==' '))
        
            word++;
    
    
            v=ptr[i++];
        }
        printf("the word %d times occured",word);
    }
    after that changing eveyting is same the result as everytime 1

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    5
    i wrote that problem with easiest way can you check it again? bur the problem is: when i write this code with fgets the result is every time 0 but when i write with gets result is everytime 1. and then when i write with dev c++ result is correct. what can i do again


    Code:
    void FindWord(char *ptr){
    	char *strptr, word[20] = {""};
    	int i = 0, j = 0;
    	
    	fgets(word,15,stdin);
    	strptr = ptr;
    	
    	while ((strptr = strstr(strptr, word)) != NULL)
    	{
    		strptr++;
    		j++;
    	}
    	
    	printf("%s occured %d times in the string\n", word, j);
    	
    }

  8. #8
    Registered User
    Join Date
    Mar 2012
    Posts
    5
    iwrote that with scanf too but result again 1

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    So do some debugging. Either (preferably) step through the code and see what is happening in the debugger, or add more statements to output things as the code runs.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to work with a .LIB in C++ (IDE:Visual Studio 6)
    By Arkanos in forum C++ Programming
    Replies: 4
    Last Post: 03-30-2007, 12:16 PM
  2. gotoxy don't work with my visual C++
    By carlosramos in forum C++ Programming
    Replies: 1
    Last Post: 05-25-2005, 09:05 AM
  3. I can't get profiling to work with Visual Studio 6
    By Darkness in forum C++ Programming
    Replies: 7
    Last Post: 12-30-2004, 10:37 PM
  4. Why does it work in Visual Studio and Not Borland
    By MonteMan in forum C++ Programming
    Replies: 14
    Last Post: 10-20-2002, 09:36 PM