Elements

I need help, on just the adding part of elements. I have an example at the end of the code. Please help me. :D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>
#include<algorithm>
#include<cstring>
#define MAX 50
using namespace std;
main()
{
void remove(char[], char);
char cstring[MAX];
char letter;
std::string str;

cout << "Enter String: ";
cin.getline(cstring, MAX);

cout << "Size of String: " << strlen(cstring) << endl;

cout << "Delete Element: ";
letter = cin.get();

remove(cstring, letter);

cout << "String: " << cstring << endl;
cout << "Size of String: " << strlen(cstring) << endl;

cin.ignore();

cout << "Add Element: ";
letter = cin.get()

cout << "String: " //<< i dont know what to put here, so that the letters i inputted
//will be added to the string. help pls.
}

void remove(char * cstring, char c)
{
char * end = cstring + strlen(cstring);
end = std::remove(cstring, end, c);
*end = '\0';
}


An example would be.

Enter String: superfragilisticexpialidocious
Size of String: 30
Delete Element: a
String: superfrgilisticexpilidocious
Size of String: 28
Add Element: zzz
String: superfrgilisticexpilidociouszzz <<< This is the part i don't know how to do. please help me!
Size of String: 31
i would be very grateful. a simple code will do.
Last edited on
Use the append function.

EDIT: Just realised you seem to be using cstrings (despite declaring a std::string). You'll need to use the strcat function for those. I'd recommend using C++ strings, though.
Last edited on
i used append but i don't know how to put it there. it just says error. could you please edit the part where i should use the append.
append will only work on C++ strings.

If you continue using C-strings, you'll need to use strcat.

http://www.cplusplus.com/reference/cstring/strcat/
when i use strcat, this is what it says.
error: invalid conversion from 'char' to 'const char*' [-fpermissive]|
I'd advise using std::string instead of character arrays (c strings). string automatically expands, is much safer, and has more features.

http://www.cplusplus.com/reference/string/string/

1
2
3
4
5
6
7
8
9
10
11
12
string str;
cin >> str;

remove(str.begin(), str.end(), someChar);

string addStr;
cin >> addStr; // one or more characters

str.append(addStr);

cout << str;




i just need a code for the add element part. like the code in the remove section.

1
2
3
4
5
6
void remove(char * cstring, char c)
{
    char * end = cstring + strlen(cstring);
    end = std::remove(cstring, end, c);
    *end = '\0';
}


can you please help me, cause i'm really a newbie in programming.
You can add a character to the string only if the character array has enough space to accomodate the character. So before adding a character you should check the current size of the array with its maximumsize.

So function add can look for example the following way.

1
2
3
4
5
6
7
8
9
10
11
12
char * add( char *s, char c )
{
   int n = strlen( s );

   if ( n < MAX - 1 ) 
   {
      s[n] = c;
      s[n + 1] = '\0';
   }

   return ( s );
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#define MAX 50
using namespace std;
int main()
{
    void remove(char[], char);
    char cstring[MAX];
    char letter;
    std::string str;

    cout << "Enter String: ";
    cin.getline(cstring, MAX);

    cout << "Size of String: " << strlen(cstring) << endl;

    cout << "Delete Element: ";
    letter = cin.get();

    remove(cstring, letter);

    cout << "String: " << cstring << endl;
    cout << "Size of String: " << strlen(cstring) << endl;

    cin.ignore();

    cout << "Add Element: ";
    letter = cin.get();

    add(cstring, letter);

    cout << "String: " << cstring << endl;


}

void remove(char * cstring, char c)
{
    char * end = cstring + strlen(cstring);
    end = std::remove(cstring, end, c);
    *end = '\0';
}
    char * add( char *cstring, char c )
{
   int letter = strlen( cstring );

   if ( letter < MAX - 1 )
   {
      cstring[letter] = c;
      cstring[letter + 1] = '\0';
   }

   return ( cstring );
}


It already compiles, but after i input zzz in the "Add Element: " part. It just shows up with 1 z at the end. Please help!
This statement

letter = cin.get();

enters only one character. Two other characters will be in the input buffer untilo you will read them.
so how can i make them go together. can you please help me on what's the code.
You've defined letter as a char, which means that it only holds a single character.

And I can only echo what several other people have said: you'll find all this much easier if you learn how to use std::string, instead of C-style char arrays.
I figured it out. Thank you all for the help. :)
vlad from moscow - thank you for the idea. ;)
@EJ Destua

so how can i make them go together. can you please help me on what's the code.


You can do this in a loop until letter will be equal to the new line character '\n'
Topic archived. No new replies allowed.