Confused on Code Error C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Confused on Code Error C++

#include <iostream> #include <string> #include <sstream> #include <iomanip> #include <ios> using namespace std; int main() { string words[1]; //initializing the array int length; //length will be how we eventually check the size of the input for(int x = 0; x < 1; x++){ cout << "Enter Your word, So it shall be tested.\n\n"; std::getline(std::cin, words[x]); } for(int x = words.length()-1; x >=0; x--){ length = words[x].length(); cout << words[x]; } return 0; } Bit lost on this one, Compiler isn't providing enough information for me to accurately figure out my compile error, Trying to simply produce the user's input backwards.

9th Oct 2018, 10:33 PM
Ryan Finley
1 Answer
+ 4
When you are trying to print the word backwards you are not accessing the string array properly. See: #include <iostream> #include <string> using namespace std; int main() { string words[1]; //initializing the array // int length; //length will be how we eventually check the size of the input for(int x = 0; x < 1; x++){ cout << "Enter Your word, So it shall be tested.\n\n"; std::cin >> words[x]; } // traverse the array for(int x = 0; x < 1; x++){ //traverse the word (backwards) for(int i = words[x].length()-1; i >=0; i--){ //output the character at position i cout << words[x].at(i); } } return 0; }
9th Oct 2018, 11:46 PM
jay
jay - avatar