CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Apr 2013
    Posts
    45

    Code does not work!!!

    Hi everybody!!!!
    Could you tell me why the following code doesn't work???
    That is the code:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void func(char *p1,char *p2){
    	char ch;
    	while (p1<p2){
    		   ch=*p1;
    		   *p1=*p2;
    		   *p2=ch;
    		   p1++;
    		   p1--;
    	}
    }
    		  
    int main()
    {
    	char a[8]="VENICE";
    	puts(a);
    	func(a,a+4);
    	puts(a);
    	return 0;
    }
    Thanks in advance!

  2. #2
    Join Date
    Jan 2009
    Posts
    596

    Re: Code does not work!!!

    You are trying to modify a string literal ("VENICE"). This will cause undefined behaviour.

  3. #3
    Join Date
    Apr 2013
    Posts
    45

    Re: Code does not work!!!

    So....how can I change my code so that it works???

  4. #4
    Join Date
    Jan 2009
    Posts
    596

    Re: Code does not work!!!

    Either:
    1) Change to use std::string instead of char arrays, or
    2) Create a char array on the heap using new and initialize this array to contain "VENICE" (plus the null-terminator)

  5. #5
    Join Date
    Apr 2013
    Posts
    45

    Re: Code does not work!!!

    You mean like that:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void func(char *p1,char *p2){
    	char ch;
    	while (p1<p2){
    		   ch=*p1;
    		   *p1=*p2;
    		   *p2=ch;
    		   p1++;
    		   p1--;
    	}
    }
    		  
    int main()
    {
    	char a[8];
    	printf("Give a string:\n");
    	gets(a);
    	puts(a);
    	func(a,a+4);
    	puts(a);
    	return 0;
    }
    ????The output is still wrong

  6. #6
    Join Date
    Jan 2009
    Posts
    596

    Re: Code does not work!!!

    You have a little bug in func(). Run it through the debugger -you'll spot it eventually...

    Also, don't use gets() - it's dangerous, and has been deprecated. For input it is safer to use streams (i.e. cin or getline)
    Last edited by Peter_B; June 20th, 2013 at 05:14 PM.

  7. #7
    Join Date
    Apr 2013
    Posts
    45

    Re: Code does not work!!!

    I haven't got it and also I haven't got taught streams.Could you explain me again???

  8. #8
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Code does not work!!!

    Quote Originally Posted by mathmari View Post
    I haven't got it and also I haven't got taught streams.Could you explain me again???
    Streams are used in c++ - not in c which I understand from your previous posts you are using. Instead of using gets, it's better to use fgets for which you specify the maxmimum number of chars to get and also null terminates the got string.

    Replace
    Code:
    gets(a);
    with
    Code:
    fgets(a, 8, stdin);
    In your func()
    Code:
    p1++;
    p1--;
    Are you sure you want to increment p1 and then decrement it?????
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    Apr 2013
    Posts
    45

    Re: Code does not work!!!

    Ok...nice...Oh no,I am sorry I wanted to decrement p2!!!!

  10. #10
    Join Date
    Apr 2013
    Posts
    45

    Re: Code does not work!!!

    I changed the code:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void func(char *p1,char *p2){
    	char ch;
    	while (p1<p2){
    		   ch=*p1;
    		   *p1=*p2;
    		   *p2=ch;
    		   p1++;
    		   p2--;
    	}
    }
    		  
    int main()
    {
    	char a[8];
    	printf("Give a string:\n");
    	gets(a);
    	puts(a);
    	func(a,a+4);
    	puts(a);
    	return 0;
    }
    and now the output is right....Thank you very much!!!!!!!!!!

  11. #11
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Code does not work!!!

    But your code still has some problems. You are still using gets() rather than fgets(). Also with func(a, a + 4), what if the user enters a string with less than 5 characters?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  12. #12
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Code does not work!!!

    Quote Originally Posted by 2kaud
    Streams are used in c++ - not in c
    Technically, C has the notion of input/output streams too, but they would not be the C++ I/O streams that Peter_B mentioned, but rather say, stdin and in this case the use of fgets.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  13. #13
    Join Date
    Apr 2013
    Posts
    45

    Re: Code does not work!!!

    I want that my code works,for the string VENICE...If I use fgets,do I not have to create a file???

    Quote Originally Posted by 2kaud View Post
    But your code still has some problems. You are still using gets() rather than fgets(). Also with func(a, a + 4), what if the user enters a string with less than 5 characters?

  14. #14
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Code does not work!!!

    Quote Originally Posted by mathmari
    If I use fgets,do I not have to create a file?
    No, you don't. stdin is already acts as a file pointer.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  15. #15
    Join Date
    Apr 2013
    Posts
    45

    Re: Code does not work!!!

    Ah ok...I haven't get taught stdin yet

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured