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.
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.'
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.
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
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