Hey guys, long time reader -- with maybe one post I think. I'm pretty new to C++ but can hold my own.... but I was looking for some help.

What is the best way to go about allowing the user to input as many inputs as they want, then when they stop the program will print all inputs in reverse.

The reverse part is easy, and declaring and all. What I can't comprehend is the best way to declare an Unknown number of inputs so that the program can then print those inputs back in reverse.

I don't want any code as much as I want an explanation as to what functions and procedures would work best for this situation.

I'm no copy and paster and I love learning C++ -- not plagiarizing. :)

Pseudo code:
- Ask user how many arguments they would like to input.
- If inputs less than 1, tell user that their request cannot be processed.
- If greater than or equal to 1, continue.

From there... not sure if I need to just declare a dozen input strings/numbers and then ask the user after each input if they are done...

There must be a much more efficient way to do this using a while loop.

Thanks a lot guys/gals.

The standard container classes will grow as you add to them, so there is no need to ask the user for how many inputs. You can even use a reverse iterator to go backward on most of the containers. Here is a start for research.

1) Use a string for input.
2) Check if all is numbers.
3) Output the string reverse.

I would use a stack. Stacks work in a "first in last out" manner, meaning the first value entered will be the last output. Start with the value at "size" (which is how big the stack is) and work your way down to position 0. This is just my interpretation, there are probably many other (and better) ways to accomplish this as Tom Gunn and firstPerson have already demonstrated.

Ps. Sorry if you already know about stacks and my post was completely boring =)

#include <iostream>
using namespace std;

int main(int argc, char* argv[]) {
   for(int i = 0; i < argc; i++)
      cout << "argv[" << i << "] = " << argv[i] << endl;
   return 0;
}

Ok guys, so I was misinformed. We are using command line arguments which I understand....

Now all I need to know is the correct way to write a reverse for loop that will start with arv, print it, add a space and then print argv[i-1], space etc... until we get to argv[1].

#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
   for(int i = 0; i < argc; i++)
      cout << "argv[" << i << "] = " << argv[i] << endl;
   return 0;
}

Now all I need to know is the correct way to write a reverse for loop that will start with arv, print it, add a space and then print argv[i-1], space etc... until we get to argv[1].

This code won't work because your stopping condition (argc) has not been given an initial value. Also, if you want to print in reverse order you'll probably need to use i--.

This code won't work because your stopping condition (argc) has not been given an initial value.

The runtime environment initializes argc before calling main(). The code will work, but it will print the arguments in order, not in reverse order.

Now all I need to know is the correct way to write a reverse for loop

The for loop does not care if you start low and go high or start high and go low. You can initialize i to argc-1, set the condition to i > 0 , and decrement i each time. That will print all of the arguments in argv except the first.

The runtime environment initializes argc before calling main(). The code will work, but it will print the arguments in order, not in reverse order.

Whoops. Sorry I never saw that argc before. I thought that was the name of his variable.:confused:
Sorry for the bad post. :'(

I thought that was the name of his variable.

It is. There is no magic to the name argc , it is just an idiomatic name because K&R used it. :) Whoever defines main() can choose the names of the parameters. I have seen ac and av as the next most common to argc and argv:

#include <iostream>

int main(int ac, char *av[])
{
    for (int x = 0; x < ac; ++x) std::cout << av[x] << '\n';
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.