A little help with a simple task

Hello,
My task is to write a program that concatenates the last characters of three input strings. This is my code, but my program is not compiling and I don`t know where is my error.

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
  #include <iostream>
#include <string>
using namespace std;
int main()
{
    cout << "Enter first string: " << endl;
    string str1;
    cin >> str1;

    int lastCharStringOne = str1.length() -1;
    string lastStringOne = str1[lastCharStringOne];

    cout << "Enter second string: " << endl;
    string str2;
    cin >> str2;

    int lastCharStringTwo = str2.length() -1;
    string lastStringTwo = str2[lastCharStringTwo];

    cout << "Enter third string: " << endl;
    string str3;
    cin >> str3;


    int lastCharStringThree = str3.length() -1;
    string lastStringThree = str3[lastCharStringThree];

    cout << "lastStringOne" + "lastStringTwo" + "lastStringThree"<< endl;

    return 0;
}
If your program isn't compiling that usually means that there are error messages. Would you mind posting those messages, all of them, exactly as they appear in your development environment. These messages have important information to aid in locating and fixing the problems.


I am using CodeBlocks. After Build&Run a red square is presented on line 11. After just Run everything is OK, but the result is some number and I don`t know what is the reason for such behavior.
Like I said if your program is not compiling there should be error messages generated. For example your program generated these messages from my compiler:

main.cpp||In function ‘int main()’:|
main.cpp|11|error: conversion from ‘char’ to non-scalar type ‘std::string {aka std::basic_string<char>}’ requested|
main.cpp|18|error: conversion from ‘char’ to non-scalar type ‘std::string {aka std::basic_string<char>}’ requested|
main.cpp|26|error: conversion from ‘char’ to non-scalar type ‘std::string {aka std::basic_string<char>}’ requested|
main.cpp|28|error: invalid operands of types ‘const char [14]’ and ‘const char [14]’ to binary ‘operator+’|
||=== Build finished: 4 errors, 0 warnings (0 minutes, 0 seconds) ===|


These messages are telling you that you can't use the assignment operator to convert char to a string (lastStringOne).

You can however use the operator+= instead.

1
2
3
    int lastCharStringOne = str1.length() -1;
    string lastStringOne;
    lastStringOne += str1[lastCharStringOne];


You also can't use the operator+ in your cout statement, try just the insertion operator instead.'

cout << "lastStringOne" << "lastStringTwo" << "lastStringThree"<< endl;

Jim
Thanks a lot, this is my new code, but still givving me errors.

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
#include <iostream>
#include <string>
using namespace std;
int main()
{
    cout << "Enter first string: " << endl;
    string str1;
    cin >> str1;

    int lastCharStringOne = str1.length() -1;
    string lastStringOne += str1[lastCharStringOne];

    cout << "Enter second string: " << endl;
    string str2;
    cin >> str2;

    int lastCharStringTwo = str2.length() -1;
    string lastStringTwo += str2[lastCharStringTwo];

    cout << "Enter third string: " << endl;
    string str3;
    cin >> str3;


    int lastCharStringThree = str3.length() -1;
    string lastStringThree += str3[lastCharStringThree];

    cout << "lastStringOne" << "lastStringTwo" << "lastStringThree"<< endl;

    return 0;
}
And the errors are???

There is a Build Messages window in Code::Blocks that contains these messages.

Do your modifications match what I provided? Your string must be declared prior to the use of the operator+=.

Ok I think I've figured out the problem. Lines 11, 18 and 26 are uninitialized because you are not using any of the int variable for anything. In other words, every string below line 10 is being assigned to nothing.
In addition to whatever errors the compiler is giving - check line 28 - the code is just printing out the names of the variables as text because they're within double quotes. You'll need to remove the double quotes to print out the actual values the variables are holding.
Ok, the new code, still errors guys and I can`t figure out why?

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
#include <iostream>
#include <string>
using namespace std;
int main()
{
    cout << "Enter first string: " << endl;
    string str1;
    cin >> str1;

    int lastCharStringOne = str1.length() -1;
    string lastStringOne;
    string lastStringOne += str1[lastCharStringOne];

    cout << "Enter second string: " << endl;
    string str2;
    cin >> str2;

    int lastCharStringTwo = str2.length() -1;
    string lastStringTwo;
    string lastStringTwo += str2[lastCharStringTwo];

    cout << "Enter third string: " << endl;
    string str3;
    cin >> str3;


    int lastCharStringThree = str3.length() -1;
    string lastCharStringThree;
    string lastStringThree += str3[lastCharStringThree];

    cout << lastStringOne << lastStringTwo << lastStringThree<< endl;

    return 0;
}
Last edited on
How many times do we need to ask for the error messages?

Here are all the errors and warnings jlb

All are 3 ints variables are not being used
line 12, 20 and 29-expected initializer before +=
line 28-redeclaration of charstringthree
last string three was not declared in scope.
line 27-charstringthree was previously declared
Last edited on
Here are the errors, sorry

home/rcson/Desktop/Practise21October/Task3.cpp: In function ‘int main()’:
/home/rcson/Desktop/Practise21October/Task3.cpp:12:26: error: expected initializer before ‘+=’ token
/home/rcson/Desktop/Practise21October/Task3.cpp:20:26: error: expected initializer before ‘+=’ token
/home/rcson/Desktop/Practise21October/Task3.cpp:28:9: error: redeclaration of ‘int lastCharStringThree’
/home/rcson/Desktop/Practise21October/Task3.cpp:27:9: error: ‘int lastCharStringThree’ previously declared here
/home/rcson/Desktop/Practise21October/Task3.cpp:29:28: error: expected initializer before ‘+=’ token
/home/rcson/Desktop/Practise21October/Task3.cpp:31:47: error: ‘lastStringThree’ was not declared in this scope
Do your modifications match what I provided? Your string must be declared prior to the use of the operator+=.
Ok, my new code and it`s working fine, but with using substr

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
#include <iostream>
#include <string>
using namespace std;
int main()
{
    cout << "Enter first string: " << endl;
    string str1;
    cin >> str1;

    int lastCharStringOne = str1.length() -1;



    cout << "Enter second string: " << endl;
    string str2;
    cin >> str2;

    int lastCharStringTwo = str2.length() -1;


    cout << "Enter third string: " << endl;
    string str3;
    cin >> str3;


    int lastCharStringThree = str3.length() -1;

    string all = str1.substr(lastCharStringOne,1) + str2.substr(lastCharStringTwo,1) +str3.substr(lastCharStringThree,1);

    cout << all<< endl;

    return 0;
}


Can someone write how my old program should look like, I really can`t find my mistake.
Topic archived. No new replies allowed.