So I started on the final section of my C++ project and got almost done until I re-read the requirements and saw this:

Do not use arrays.

*crap*

Is there any other way to store multiple inputs than arrays? Basically the idea is to use a recursive function get a variable amount of #'s from the user and then display them, only in reverse order. But I don't know how I could have the name of the variable change, without using an array.

I am not looking for a whole program, just someone to point me in the right direction.

Thanks!

Recommended Answers

All 5 Replies

how about a linked list ?

or something like this:

void foo(int x)
{
   int n = 0;
   cout << "Enter a number " << x << ":\n";
   cin >> n;
   if(x < maxNum) // maxNum declared elsewhere
        foo(x+1);
   cout << "n = " << n << "\n");
}

Do you even need a container (array, vector, list, whatever) for this project? Just a recursive function with single numerical variable should do it I think. The variable would need a default value to discontinue input and spit the variable from each function call return value back out to the screen or file or whereever, if the value isn't the default terminating value.

Aha! I figured it out on my own:

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;


int reverseInput(int x);  // prototype


void main()
{	
	int x;
	x = 0;
	cout << "ctrl-z to quit\n";
	reverseInput(x);  // call function
}

int reverseInput(int x)
{
	int count;
	count = 0;
	cout << "Enter a number: ";
	cin >> x;
	if (cin.eof())
	{
		return x;
	}
	else
	{
		count++;
		reverseInput(x);  //recursively call function
		cout << endl
			 << setw(count * 3) << x; // output, reversed
	}
}

Thanks everyone.

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.