Need Help: Cin -->already stored array

Hi. So im confused as to how I go about doing this. I have preset arrays/classes of data. I'm trying to take whatever the user inputs and then have the program match it with an array of that name. Then have the program call upon that matched array. For example:

float fire[5]{0,1,2,3,4};
float water[5]{0,1,2,3,4};

cout << "What element would you like?";
cin >>string x;

So say they type in water. Is there a way to have the program call upon my water array from here on out?
This isn't possible in C++.
Do you mean to display what is in either fire or water if there is a match?
Last edited on
I have a formula where I want the data from the array punched into the formula based on what the user types in. Basically using sstream or some other means to match what they typed against my arrays. And have that array selected to be punched into the formula
Are you trying to reference the actual array identifier during runtime, or just the contents? The former isn't possible at all with C++
The array identifier. If its not possible what other method would you suggest for me to take user input in such a fashion?
I'm not too sure what functionality you're going for, but you could just take some input, and tie that variable to your arrays through a condition. Like, if(userinput == "fire") use fireArr;
Done this for you(OP-original poster) so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

using namespace std;

int main()
{
	char* elements[]={"fire","water","earth","air"};
	char check[6];
				
	cout<<"Which element is your best?"<<endl;
	cin>>check;
	if(strcmp(elements[0],check)==0 || strcmp(elements[1],check)==0 || strcmp(elements[2],check)==0 || strcmp(elements[3],check)==0 || strcmp(elements[4],check)==0){
	cin.ignore();
	cout<<"Match found";
      }
	else{
	    cin.ignore();
	    cout<<"Not an element";
       }
	cin.get();
	return 0;
}


But it crashes when the user's input doesn't match.
Dunno why. any help!?!
Last edited on
conditions could work nicely actually. And Aceix, thank you for writing that out. That gives me a more broader sense on how to do this. As to why it crashes idk o .o my best guess is you would be calling for a variable in the array which does not exist in your else clause.
Bad news: But it crashes when the user's input doesn't match.
Good news: Got a solution at last!!!

code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>

using namespace std;

int main()
{
	char* elements[]={"water","fire","air","earth"};
	char** e=elements;
	char* check;
	int x=0;
				
	cout<<"Which element is your best?"<<endl;
	cin>>check;
	for(int i=0;i<4;i++){
	 if(strcmp(e[i],check)==0){
		cout<<"Match found!";
		x=1;
	 }
	}
	cin.ignore();
	if(x==0){
	 cout<<"Not an element";
	}
	cin.get();
	return 0;
}
**? is that a pointer two classes down? ive never seen this
It is a pointer to a pointer.
http://www.cplusplus.com/doc/tutorial/pointers/ for reference
Topic archived. No new replies allowed.