8 Replies - 574 Views - Last Post: 25 October 2009 - 02:34 PM Rate Topic: -----

#1 claudio  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 29-January 09

Creating a variable with a loop? Is it possible?

Post icon  Posted 25 October 2009 - 01:53 PM

Hello everyone, good evening!


This time I'm coming here with a doubt that striked me while writing a code, but can't find any concise information probably because I'm not using the right terms, so I'm going to try to explain the situation.

What I ask is, can I create a new variable everytime I execute a loop? I mean, I receive from standard input an int number. After that, I want my program to have that number of variables. Is it possible?

for Example:

int user = keyboard.nextInt();
for (i=0; i<=user;i++)
{
	 //create int var_"i" ();
}






This is probably a wrong way to view the problem since what I probably need to do is:

Ask the user how many numbers are to be read from the input and read only those first ones no matter how long the input is.
But still I ask, is the loop for creating variables possible? and if it is, how is it called? Or how are those kind of variables called?


Thank you in advance and sorry if it is too massy, tell me i'll try to explain better.

Best regards everyone!

EDIT: oh sorry, forgot to mention that arrays are out of question.

This post has been edited by claudio: 25 October 2009 - 01:57 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Creating a variable with a loop? Is it possible?

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Creating a variable with a loop? Is it possible?

Posted 25 October 2009 - 01:55 PM

First thing that comes to mind is to use a dynamic structure such as a stack, queue, or vector to store the input. If you know there will be a finite amount of input and you know at compile time you could use a regular array.
Was This Post Helpful? 0
  • +
  • -

#3 claudio  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 29-January 09

Re: Creating a variable with a loop? Is it possible?

Posted 25 October 2009 - 01:56 PM

oh sorry, forgot to mention that arrays are out of question.
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Creating a variable with a loop? Is it possible?

Posted 25 October 2009 - 01:59 PM

What about other data structures?

Without a place to store them you're SOL.

Will the user enter all the input at once? You could store it all in one giant string and parse it later.
Was This Post Helpful? 1
  • +
  • -

#5 claudio  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 29-January 09

Re: Creating a variable with a loop? Is it possible?

Posted 25 October 2009 - 02:01 PM

View PostKYA, on 25 Oct, 2009 - 12:55 PM, said:

First thing that comes to mind is to use a dynamic structure such as a stack, queue, or vector to store the input. If you know there will be a finite amount of input and you know at compile time you could use a regular array.



I'll only know how many variables are needed after compiling the code. so when it is runnig, it's going to be the user that will decide how many variables are going to be created. Without arrays.

Thank you for the tips KYA! I haven't heard of those yet so i'm going to do a research and see if I can use them from what I'm trying to do.

Still, that kind of simple loop isn't possible right?
Was This Post Helpful? 0
  • +
  • -

#6 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Creating a variable with a loop? Is it possible?

Posted 25 October 2009 - 02:05 PM

You can use a data structure like a Linked List, ArrayList or something, as KYA said, in conjunction with a local variable and just keep adding the local variable (that will change every iteration) to the structure.

This post has been edited by Locke: 25 October 2009 - 02:05 PM

Was This Post Helpful? 1
  • +
  • -

#7 claudio  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 29-January 09

Re: Creating a variable with a loop? Is it possible?

Posted 25 October 2009 - 02:10 PM

ah! so you say:

Store the input in a string, then parse only the numbers of elements i want from it right?

I mean, say the user tells:


-- There are going to be 4 different numbers stored in this program.
-- "user inputs 6 numbers, all of them are stored in the string"
--Tell the user the first 4 numbers he entered.

For the last step I'll parse the string and read only the first 4 values.


Is this what you mean?

View PostLocke, on 25 Oct, 2009 - 01:05 PM, said:

You can use a data structure like a Linked List, ArrayList or something, as KYA said, in conjunction with a local variable and just keep adding the local variable (that will change every iteration) to the structure.




Ahh! Got it! Thank you very much you both!
Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Creating a variable with a loop? Is it possible?

Posted 25 October 2009 - 02:14 PM

Using a vector:
vector<dataType> storage;
dataType local;
while(someCondition){
	 local = user input;
	 storage.push_back(local); //might be a different call check the API
}



String way:

string storage;
storage = user input;
System.out.println(substring consisting of the indexes the user gave);


Was This Post Helpful? 0
  • +
  • -

#9 claudio  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 29-January 09

Re: Creating a variable with a loop? Is it possible?

Posted 25 October 2009 - 02:34 PM

View PostKYA, on 25 Oct, 2009 - 01:14 PM, said:

Using a vector:
vector<dataType> storage;
dataType local;
while(someCondition){
	 local = user input;
	 storage.push_back(local); //might be a different call check the API
}



String way:

string storage;
storage = user input;
System.out.println(substring consisting of the indexes the user gave);




Yes yes, thank you very much, got it ;)

Thank you both for the time!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1