Simple character change in a char* error

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

Simple character change in a char* error

 
0
  #1
Jun 15th, 2007
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:
  1. ret[a] = '\n';
but I don't see what I'm doing wrong.

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Simple character change in a char* error

 
0
  #2
Jun 15th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

Re: Simple character change in a char* error

 
0
  #3
Jun 15th, 2007
  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,573
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Simple character change in a char* error

 
0
  #4
Jun 15th, 2007
>neither of them work
Because they both attempt to modify a string literal. Change this:
  1. char * b = "Hello|World";
To this:
  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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 82
Reputation: hinduengg is an unknown quantity at this point 
Solved Threads: 3
hinduengg's Avatar
hinduengg hinduengg is offline Offline
Junior Poster in Training

Re: Simple character change in a char* error

 
0
  #5
Jun 16th, 2007
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...
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

Re: Simple character change in a char* error

 
0
  #6
Jun 16th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 82
Reputation: hinduengg is an unknown quantity at this point 
Solved Threads: 3
hinduengg's Avatar
hinduengg hinduengg is offline Offline
Junior Poster in Training

Re: Simple character change in a char* error

 
0
  #7
Jun 16th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,573
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Simple character change in a char* error

 
0
  #8
Jun 16th, 2007
>#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:
  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++:
  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:
  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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 82
Reputation: hinduengg is an unknown quantity at this point 
Solved Threads: 3
hinduengg's Avatar
hinduengg hinduengg is offline Offline
Junior Poster in Training

Re: Simple character change in a char* error

 
0
  #9
Jun 16th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

Re: Simple character change in a char* error

 
0
  #10
Jun 18th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC