String problems c++

Pages: 12
Really I dont' understand excuse me.As I say I live in Italy and I don't understand well what you write becuase i didn't study at school some operators that you write or I dont understand for my english not perfect.
So can you write the complete code ?
The result mus t be : abcd e efgh aebfcgdh
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

#include <cstdlib>
#include <ctime>
#include <cmath>
#include <iostream>
using namespace std;
int ConcAltStringhe(string a,string b)
// es. abcd e efgh aebfcgdh
{

    int dim=a.length();
    int dim1=b.length();
   // int dim2=c.length();
    int i=0;
    string result;

    if(dim==dim1)
    {
        for(int i=0;i<dim;i++)
        {
          ??????????????????
        }
    }
    return result;

    }
int main()
{
  srand(time(NULL));


  string a;
  cin>>a;
  string b;
  cin>>b;
  cout<<ConcAltStringhe(a,b);



    return 0;
}
Last edited on
Seriously.

result += a[i];
result += b[i];

Doesn't go.
There is a mistake on "return result"....

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
#include <iostream>
using namespace std;
int ConcAltStringhe(string a,string b)
// es. abcd e efgh aebfcgdh
{

    int dim=a.length();
    int dim1=b.length();
   // int dim2=c.length();
    int i=0;
    string result;

    if(dim==dim1)
    {
        for(int i=0;i<dim;i++)
        {
         result += a[i];
         result += b[i];
        }
    }
    return result;

    }
int main()
{
  srand(time(NULL));


  string a;
  cin>>a;
  string b;
  cin>>b;
  cout<<ConcAltStringhe(a,b);



    return 0;
}
Last edited on
You've declared ConcAltStringhe as returning an int:
int ConcAltStringhe(string a,string b)
but at line 21, it returns a string:
return result;
So you need to change the declaration to indicate that it returns a string.

can you write the complete code ?
You'll find that people here don't like to do that. Programming is like football: the only way to get good is to practice.

It's also like math: every letter and every symbol mean something specific You really have to understand the meaning. You can't write a+b instead of a-b in math and expect to solve an equation. In the same way, you can't declare a function as returning an int and then try to return a string. You have read the error message and try to understand what it says.
Here is but now the mistake is in "string ConcAltStringhe( string a, string b)"

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
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <iostream>

string ConcAltStringhe( string a, string b)
// es. abcd e efgh aebfcgdh
{

    int dim=a.length();
    int dim1=b.length();
    //int dim2=c.length();
    int i=0;
    string result;

    if(dim==dim1)
    {
        for(int i=0;i<dim;i++)
        {
           result += 'a[i]';
           result += 'b[i]';
          // b[i]= result
        }
    }
    return result;

    }
int main()
{
  srand(time(NULL));


  string a;
  cin>>a;
  string b;
  cin>>b;
  cout<<ConcAltStringhe(a,b);



    return 0;
}
1
2
           result += 'a[i]';
           result += 'b[i]';

should be:
1
2
           result += a[i];
           result += b[i];

Single quotes are used to express a character literal. So 'a' is the ASCII letter a and '3' is the ASCII character 3 (NOT the number 3). So the compiler doesn't understand 'a[i]' because the quote makes it think you're entering a character literal but a[i] isn't a character a character literal.

Without the single quotes, a[i] is an expression that evaluates to the character at position i within the string a. That's what you want.

Pay attention to the text of the error messages. On my compiler (g++) the error message is:
foo.cxx:20:22: warning: multi-character character constant [-Wmultichar]
            result += 'a[i]';
                      ^~~~~~

So it's confused that you have mutiple characters to express a character constant.

You also need to add:
#include <string>
near the top to get the definition of the string class.

Finally, since string, cin and cout are all defined within a "namespace", you should add
using namespace std;
after your last #include line. Note that for complex programs, it's frowned upon to use the entire namespace, but for a beginner program I think it's okay.

When I make these changes to your code, it works.

Excuse me for the part "if the strings have different lengths, the program returns the empty string" how must I do in the code?

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
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <iostream>

string ConcAltStringhe( string a, string b)
// es. abcd e efgh aebfcgdh
{

    int dim=a.length();
    int dim1=b.length();
    string result;

    if(dim==dim1)
    {
        for(int i=0;i<dim;i++)
        {
           result += 'a[i]';
           result += 'b[i]';
        
        }
    }
    return result;

    }
int main()
{



  string a;
  cin>>a;
  string b;
  cin>>b;
  cout<<ConcAltStringhe(a,b);



    return 0;
}
Last edited on
Dhayden can you explain me :
"
result += a[i];
result += b[i];
return result

and how result give me the solution aebfcgdh ?
I wish really understand th mechanism, I undestrand the the solution but I will never be arrived at this solution , so I wish the undestrand better than I can.
Many thanks!!!
+ for a string is concatenate. It is set up to accept both strings and single letters.

so empty string + 'a' = "a".
"abc"+"def" = "abcdef".

so he is taking 1 letter from each input and putting it on the end of the string each loop iteration.


what do you get if you have abc and def? iteration 1 takes a and d, so its ad .. iteration two takes be, adbe … etc.
I understand thanks, I wish to konw the part of the question of how "if the strings have different lengths, the program returns the empty string"
So I write "abcd " and "edfghi" that have different length I must have string empy, how I can I do this in the code?
Last edited on
he did that. he gets both lengths, and if they are not equal, it just returns result which was empty (it skips the loop).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int dim=a.length();
    int dim1=b.length();
    string result;

    if(dim==dim1)
    {
        for(int i=0;i<dim;i++)
        {
           result += 'a[i]';
           result += 'b[i]';
        
        }
    }
    return result;

Last edited on
> result += 'a[i]';
> result += 'b[i]';
WTF.
Now you're just being a troll if this is your regressive step from previously working code.


I don't think I can help you any more. You should find someone who can help you face-to-face. There are just too many things that you're having trouble understanding. See if you can talk to the teacher, a TA or a fellow student.

Sorry,
Dave
Last edited on
Topic archived. No new replies allowed.
Pages: 12