2

I'm trying to make a program that flips the words you input. The problem comes when I try to write the word and it asks twice for the input:

cin >> Arr >> myStr;

It is logical that it ask twice but everytime I try to use getline the compiler gives out an error (and even if it worked I have to give the input twice) and I need it to include spaces in the character array.

Here is my full code:

#include <iostream>
#include <string>

using namespace std;

string myStr;
string newVal;
int i;

int main()
{
    char Arr[i];
    cout << "Enter: ";
    cin >> Arr >> myStr;

    for (i = myStr.length(); i >= 0; i--)
    {   
        cout << Arr[i];
    }

    cout << endl;

    return 0;
}

The loop works.

The first problem can be corrected by achieving the proper use of getline.

The second one, I have got no idea (use a single input to assign two variables).

Thanks in advance, and I apologise if this is too much of a ridiculous question.

15
  • did you try doing this on two different lines? cin>>Arr; cin>>myStr; Commented Aug 10, 2014 at 6:24
  • 2
    What is the error that getline gives you? And why are you using an array of char instead of std::string? Commented Aug 10, 2014 at 6:27
  • it should give out the same as cin >> Arr >> myStr; Commented Aug 10, 2014 at 6:28
  • 1
    char Arr[i]; is an error because i is an int (not to mention that it has value 0) Commented Aug 10, 2014 at 6:37
  • 1
    @0x499602D2 static has nothing to do with it; const is fine and non-const isn't Commented Aug 10, 2014 at 7:50

2 Answers 2

2

Maybe you can try to have a look to this solution that it's more C++ style than yours:

 #include <iostream>
 #include <string>

 int main() {
      std::string myStr;

      std::cout << "Please give me a string: ";

      if(std::getline(std::cin, myStr)) {
           for(std::string::reverse_iterator it = myStr.rbegin(); 
               it != myStr.rend(); 
               ++it) {
               std::cout << *it;
               }

               std::cout << std::endl;

        }


}

I suggest to you to use always std::string in C++ because has all the methods and function that you could need. So don't waste your time with char array like you do in C.

Sign up to request clarification or add additional context in comments.

1 Comment

This was actually a better approach. I wasn't aware of the reverse_iterator class. Instead of using the std:: prefix I always prefer using namespace std; but I want to the habit of using the prefix to grow on me, that way I can retain more information about the properties and usage of all references in C++. Thank you very much, I will select this as an answer to the question but I will also publish my findings.
1

Here it is, as I said. I was digging around and came up with this:

#include <iostream>
#include <string>
using namespace std;

string myStr;
int i;

int main()
{
cout << "Enter: ";
getline (cin, myStr);
i = myStr.size();
cout << i << endl;
char Arr[i];

for (int a = 0; a <= i; a++)
{
    Arr[a] = myStr[a];

}

for (i; i >= 0; i--)
{

    cout << Arr[i];

}

return 0;   
}

I did not know string contents could also have array-like behaviour. Test it out! Works like a charm (as far as tested).

The way I formatted the code it takes no more than 27 lines of code.

Thanks everyone involved, you helped me a lot.

P.S: Couldn't answer before, I can't do it soon enough with my reputation.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.