943,985 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3491
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 15th, 2007
0

Simple character change in a char* error

Expand Post »
Basically I'm writing a function to change all the '|' characters in a string passed as a parameter to '\n' characters. Seems simple enough to do, but I get one of those "Application has encountered a problem and needs to close. We are sorry for the inconvenience." messages.

  1. char * processString(char * s)
  2. {
  3. char * ret = s;
  4. int a=0;
  5.  
  6. while (a < strlen(ret))
  7. {
  8. if (ret[a] == '|')
  9. {
  10. ret[a] = '\n';
  11. }
  12. a++;
  13. }
  14. return ret;
  15. }

I've pinpointed thee error to the assignment statement:
C++ Syntax (Toggle Plain Text)
  1. ret[a] = '\n';
but I don't see what I'm doing wrong.

Thanks.
Reputation Points: 23
Solved Threads: 5
Posting Whiz in Training
bops is offline Offline
214 posts
since Aug 2005
Jun 15th, 2007
0

Re: Simple character change in a char* error

Well if you're calling it with a string constant to test it, eg
processString("hello|world");

Then yes, it will fail because string constants are really constant, and the OS will halt your program if it even tries.

Try something like
char test[] = "hello|world";
processString(test);


If that isn't it, you need to post a complete ptogram which shows the problem, not a snippet of where it seems to fail.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jun 15th, 2007
0

Re: Simple character change in a char* error

  1. #include <iostream.h>
  2.  
  3. char * processString(char * s)
  4. {
  5. char * ret = s;
  6. int a=0;
  7.  
  8. while (a < strlen(ret))
  9. {
  10. if (ret[a] == '|')
  11. {
  12. ret[a] = '\n';
  13. }
  14. a++;
  15. }
  16. return ret;
  17. }
  18.  
  19. int main()
  20. {
  21. char * b = "Hello|World";
  22. cout<<processString(b);
  23. //cout<<processString("Hello|World");
  24. int a; cin>>a; //Keep on screen
  25. }

Thats a program I've made just there to demonstrate how I'm using it, neither of them work, I get the same error. I thought it would be pointless posting a very large program.

Thanks.
Reputation Points: 23
Solved Threads: 5
Posting Whiz in Training
bops is offline Offline
214 posts
since Aug 2005
Jun 15th, 2007
0

Re: Simple character change in a char* error

>neither of them work
Because they both attempt to modify a string literal. Change this:
C++ Syntax (Toggle Plain Text)
  1. char * b = "Hello|World";
To this:
C++ Syntax (Toggle Plain Text)
  1. char b[] = "Hello|World";
I know it looks like nothing, but the difference is huge. The first is a pointer to a string literal, which is read-only and cannot be changed. The second is an array initialized with the contents of a string literal, which is owned by you and can be modified.

On another note:

>#include <iostream.h>
The iostream.h header doesn't exist in C++. You want to use <iostream> and get a newer book that tells you how to use it and a newer compiler that supports it.

>while (a < strlen(ret))
To use strlen you need to include <cstring>.

>int a; cin>>a; //Keep on screen
It's easier and more intuitive to simply use cin.get();. What you have requires you to type the end-of-file key combination to end the program, but cin.get only requires you to hit Enter.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 16th, 2007
0

Re: Simple character change in a char* error

I think the same .Bops you must use it as in C++
  1. #include<iostream.h>
  2. #include<stdio.h>
  3. void main()
  4. {
  5. char a[100];
  6. cout<<"enter your sentence";
  7. gets(a);
  8. for(int x=0;a[x]!='\0';x++)
  9. {
  10. if(a[x]=='|')
  11. a[x]=' ';
  12. cout<<a[x];
  13. }
  14.  
  15. }
This in C++ may be it might help you in C
The logic is correct.
Last edited by WaltP; Jun 16th, 2007 at 3:16 pm. Reason: Added CODE tags -- you actually typed right over how to use them when you entered this post...
Reputation Points: 36
Solved Threads: 4
Junior Poster in Training
hinduengg is offline Offline
88 posts
since Jun 2007
Jun 16th, 2007
0

Re: Simple character change in a char* error

Thanks for your help, I understand what you are saying to me, I included string.h originally but I didn't include it in the code snippet here since I missed it in the copy&paste.

Why is it that I should include <iostream> instead of <iostream.h> and <cstring> instead of <string.h>, if this is because its the different libraries of C and C++ I can understand but when I code I tend to use both C and C++, I don't know if this is bad practice or not, but I get the job done eventually.

Yeah, your solution works fine, thanks a lot.
Reputation Points: 23
Solved Threads: 5
Posting Whiz in Training
bops is offline Offline
214 posts
since Aug 2005
Jun 16th, 2007
0

Re: Simple character change in a char* error

You are welcome.

Perhaps I think that if you write a program in C++ then you should include header files with extension - .h
and if use C then you should use haeder fies like -<iostream>
and <cstring> etc. as Narue said because both are different programming languages.

Compilation errors might occur otherwise.
Last edited by hinduengg; Jun 16th, 2007 at 8:11 am.
Reputation Points: 36
Solved Threads: 4
Junior Poster in Training
hinduengg is offline Offline
88 posts
since Jun 2007
Jun 16th, 2007
0

Re: Simple character change in a char* error

>#include<iostream.h>
>#include<stdio.h>
Don't use iostream.h, it's not valid C++ anymore. New compilers will refuse to compile your code. Also, stdio.h is deprecated in C++. The two headers you should be using are:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cstdio>
And because the modern C++ standard library is in the std namespace, you need to qualify it. The easiest way to do that is using namespace std;, but that's generally not a good idea for reasons that you'll learn later.

>void main()
void main is wrong in both languages. It's never been correct in 40 years. main returns an int, always, without fail. This is your template for all new programs in C++:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. }
You don't need to return anything because 0 is returned implicitly by default. And this is the template for C:
C++ Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2.  
  3. int main ( void )
  4. {
  5. return 0;
  6. }
The latest C standard also returns 0 implicitly, but it's not widely implemented enough yet that you can adopt the convention. Not to mention the C community is leaning toward considering it bad practice to use that feature.

>gets(a);
gets is a bad idea. It's never safe, so you should just forget that it exists at all. In C, the replacement is fgets, and in C++ the replacement is getline.

>cout<<a[x];
I highly recommend not mixing C and C++ style I/O.

>but when I code I tend to use both C and C++
That's a mistake. There are subtle differences between C and C++, and unless you're an expert in both languages, mixing them with burn you eventually.
Last edited by Narue; Jun 16th, 2007 at 10:22 am.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 16th, 2007
0

Re: Simple character change in a char* error

Narue thank you for informing me about latest trend of C++
I still use Turbo C++ which accepts .h header files.Even in my college the same old setup is followed.
Definitely after finals I would install the latest hardware.
I am sure now thats why a lot of members use std strings and the info you gave and not string.h

and no char arrays in C++

I am sorry Bops for giving you old info
Last edited by hinduengg; Jun 16th, 2007 at 11:09 am.
Reputation Points: 36
Solved Threads: 4
Junior Poster in Training
hinduengg is offline Offline
88 posts
since Jun 2007
Jun 18th, 2007
0

Re: Simple character change in a char* error

Do you know of any free updated compilers? I use Dev-C++ 4.9.9.2 with MinGW compiler. I'm not sure which version of MinGW it is, whichever comes with it.
Reputation Points: 23
Solved Threads: 5
Posting Whiz in Training
bops is offline Offline
214 posts
since Aug 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: CHtmlEditor, plz help me
Next Thread in C++ Forum Timeline: New to C++ and program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC