| | |
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:
Solved Threads: 3
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.
I've pinpointed thee error to the assignment statement:
but I don't see what I'm doing wrong.
Thanks.
c Syntax (Toggle Plain Text)
char * processString(char * s) { char * ret = s; int a=0; while (a < strlen(ret)) { if (ret[a] == '|') { ret[a] = '\n'; } a++; } return ret; }
I've pinpointed thee error to the assignment statement:
C++ Syntax (Toggle Plain Text)
ret[a] = '\n';
Thanks.
Well if you're calling it with a string constant to test it, eg
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
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.
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.
•
•
Join Date: Aug 2005
Posts: 188
Reputation:
Solved Threads: 3
c Syntax (Toggle Plain Text)
#include <iostream.h> char * processString(char * s) { char * ret = s; int a=0; while (a < strlen(ret)) { if (ret[a] == '|') { ret[a] = '\n'; } a++; } return ret; } int main() { char * b = "Hello|World"; cout<<processString(b); //cout<<processString("Hello|World"); int a; cin>>a; //Keep on screen }
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.
>neither of them work
Because they both attempt to modify a string literal. Change this:
To this:
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
Because they both attempt to modify a string literal. Change this:
C++ Syntax (Toggle Plain Text)
char * b = "Hello|World";
C++ Syntax (Toggle Plain Text)
char b[] = "Hello|World";
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.
I think the same .Bops you must use it as in C++
This in C++ may be it might help you in C
The logic is correct.
c Syntax (Toggle Plain Text)
#include<iostream.h> #include<stdio.h> void main() { char a[100]; cout<<"enter your sentence"; gets(a); for(int x=0;a[x]!='\0';x++) { if(a[x]=='|') a[x]=' '; cout<<a[x]; } }
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...
•
•
Join Date: Aug 2005
Posts: 188
Reputation:
Solved Threads: 3
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.
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.
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.
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.
>#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:
And because the modern C++ standard library is in the std namespace, you need to qualify it. The easiest way to do that is
>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++:
You don't need to return anything because 0 is returned implicitly by default. And this is the template for C:
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.
>#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)
#include <iostream> #include <cstdio>
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)
#include <iostream> using namespace std; int main() { }
C++ Syntax (Toggle Plain Text)
#include <stdio.h> int main ( void ) { return 0; }
>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.
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
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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: CHtmlEditor, plz help me
- Next Thread: New to C++ and program
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






