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.

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:

ret[a] = '\n';

but I don't see what I'm doing wrong.

Thanks.

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.

#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:

char * b = "Hello|World";

To this:

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 think the same .Bops you must use it as in C++

#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];
  }
  
}

This in C++ may be it might help you in C
The logic is correct.

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.

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.

>#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:

#include <iostream>
#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++:

#include <iostream>

using namespace std;

int main()
{
}

You don't need to return anything because 0 is returned implicitly by default. And this is the template for C:

#include <stdio.h>

int main ( void )
{
  return 0;
}

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.

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

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.

>I use Dev-C++ 4.9.9.2 with MinGW compiler.
That's the latest version. You can use standard C++ with it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.