1

What I want to do is to enter a number of loops , then all the entered words will be displayed on reverse . I tried numbers to display on reverse , and it works . But , I don't know what to change in the codes . I'm not good in c++ , so I'm practicing . Thanks for helping me =)

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

int main()
{
    int x, y;
    string a[y];
    cout << "Enter number: ";
    cin >> x;
    x=x-1;
    for (y=0; y<=x; y++)
    {
        cout << y+1 << ". ";
        cin >> a[y];
    }
    for (y=x; y>=0; y--)
    {
        cout << a[y] << endl;
    }
    return 0;
}
4
  • do yo want to input a string and then print that string in reverse order ?? Commented Oct 12, 2014 at 9:14
  • This is undefined: string a[y]; you'll need to put this line at least after cin >> x; x=x-1; Commented Oct 12, 2014 at 9:26
  • @kempoy211 As I pointed out in my post C++ has no Variable Length Arrays. So you marked an answer as the best that uses VLA. That is the code presented in the answer does not satisfy the C++ Standard and can be not compiled by other compilers. Commented Oct 12, 2014 at 12:01
  • @VladfromMoscow I'm sorry, I thought I could mark two answers. Sorry Commented Oct 15, 2014 at 13:07

3 Answers 3

1

Your ptogram is invalid. For example you are declaring array a

string a[y];

while variable y was not initialized

int x, y;

C++ does not allow to define Variable Length Arrays.

So instead of an array it would be better to use standard container std::vector

The program could look the following way

#include <iostream>
#include <vector>
#include <string>

int main() 
{
    std::cout << "Enter number: ";

    size_t n = 0;
    std::cin >> n;

    std::vector<std::string> v;
    v.reserve( n );

    for ( size_t i = 0; i < n; i++ )
    {
        std::cout << i + 1 << ". ";

        std::string s;
        std::cin >> s;
        v.push_back( s );
    }

    for ( size_t i = n; i != 0; i-- )
    {
        std::cout << v[i-1] << std::endl;
    }

    return 0;
}

For example if the input looks like

4
Welcome
to
Stackoverflow
kempoy211

then the output will be

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

4 Comments

THANK YOU VERY MUCH ! Do you know what's the best website to learn more on C++ ? Sorry for follow-up question . Thank you ! =)
@kempoy211 I do not use web sites to learn C++. I only see through web sites when I have a question. So I can not advice a web site for learning C++.
Can I send to the database all entered strings using the preprocessor fstream ? And then , when I want to view my database , can I send them back to the program ? I mean , display or view it .
@kempoy211 What is "preprocessor fstream"? Of course you can write amd read files.
0

You can use std::reverse from algorithm library in C++. With that you don't need to write these bulky loops

EDITED:-

If you just want a reverse traversal and print your string below is the Pseudo code:-

for ( each string str in array )
{
for ( int index = str.length()-1; index >= 0; index -- )
cout << str[index];
}
cout <<endl;
}

1 Comment

Not really, std::reverse will change the container, the OP asks for a reverse traversal
0
#include <iostream>
#include <string>
using namespace std;

int main()
{
    int x, y;
    cout << "Enter number: ";
    cin >> x;
    string a[x];  //this should be placed after cin as x will then be initialized
for (y=0; y<x; y++)
    {
        cout << y+1 << ". ";
        cin >> a[y];
    }
for (y=x-1; y>=0; y--)  // x-1 because array index starts from 0 to x-1 and not 0 to x
    {
        cout << a[y] << endl;
    }
return 0;
}

Output:

Enter Number: 5
1. one
2. two
3. three
4. four
5. five
five
four
three
two
one

1 Comment

Can I send the entered & scanned string to the database using the preprocessor fstream ? TIA.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.