|
-
July 13th, 2006, 04:19 AM
#1
Urgent Variable Question
hello. here is my problem-
say i have variables:
* int a, b, c, d;
string x; *
the string x contains one of four options- a, b, c, d.
now i need a way without using:
* if(x=="a") {..}
if(x=="b") {..}
if(x=="c") {..}
if(x=="d") {..} *
to store a number in one of those variables (a\b\c\d) that are what contained in string x..
for example-
string x="c";
so then i use some kind of *cin>>* function to store some number inside the variable c..
can anyone help me??
-
July 13th, 2006, 04:29 AM
#2
Re: Urgent Variable Question
You mean you dont want to use if else conditions ?
Any reason ?
Last edited by Krishnaa; July 13th, 2006 at 04:32 AM.
Regards,
Ramkrishna Pawar
-
July 13th, 2006, 04:30 AM
#3
Re: Urgent Variable Question
 Originally Posted by benzedrine
hello. here is my problem-
to store a number in one of those variables (a\b\c\d) that are what contained in string x..
for example-
string x="c";
so then i use some kind of *cin>>* function to store some number inside the variable c..
I really don't get what you want to do? It probably easy programming when you can explain exactly what you want... :-D
Asafor
-
July 13th, 2006, 04:36 AM
#4
Re: Urgent Variable Question
Maybe you need something like this:
Code:
int a, b, c, d;
string x = "c";
int * p[4] = { &a, &b, &c, &d };
cin >> *p[ x[0] - 'a' ]; // read c
I hope it helps.
-
July 13th, 2006, 04:40 AM
#5
Re: Urgent Variable Question
lol yes im a new programers.. and i maybe really didn't explain my thought to the end..
I'm trying to build a sudoku solver, but forget about the logic and all.. i just use 81 variables (not arrays, but a1, a2 ... i8, i9), and in the start of the program i request the user to input a few starting known cube..
say he chooses a1 (he can choose what ever he likes..) , i store it using:
so "a1" is now stored in string x.
i want now to request a number to be placed in the variable ** int a1 **, and that without using:
Code:
if(x=="a1") { cin>>a1; }
if(x=="a2") { cin>>a2; }
.
.
if(x=="i8") { cin>>i8; }
if(x=="i9") { cin>>i9; }
-
July 13th, 2006, 04:46 AM
#6
Re: Urgent Variable Question
You can have the 2 dimensional array of int 9*9, and you can modify the logic to move around X,Y dimensions.
If you are new programmer then I would recommand keeping the program logic As Simple As possible, adding complex algoritms/ways could lose the readability also it can create confusion while debugging.
Regards,
Ramkrishna Pawar
-
July 13th, 2006, 04:47 AM
#7
Re: Urgent Variable Question
BTW from where do you get the variable name in string ? like "C" ,"B"...
Regards,
Ramkrishna Pawar
-
July 13th, 2006, 04:54 AM
#8
Re: Urgent Variable Question
the user inputs them (in a sudoku there are some cubes that their value is known from the start- the program asks the user which cubes are known (cin>>x;) and what is their value (the value of the variable the is stored as a string is x).
-
July 13th, 2006, 05:07 AM
#9
Re: Urgent Variable Question
 Originally Posted by Viorel
Maybe you need something like this:
Code:
int a, b, c, d;
string x = "c";
int * p[4] = { &a, &b, &c, &d };
cin >> *p[ x[0] - 'a' ]; // read c
I hope it helps.
i tryed this and it doesn't work.
what was it supose to do?
-
July 13th, 2006, 05:15 AM
#10
Re: Urgent Variable Question
I think what you need is a way to convert a string type to a numeric type. See the FAQs for the various way of doing so.
Otherwise, what you can do is have a map with string or char as key (this is what the user enters) and the value of the variables correspondingly into map's value part. You would not need any conversions or if-else's this way.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
July 13th, 2006, 05:37 AM
#11
Re: Urgent Variable Question
i cant find that in the FAQ, anyway im not sure that is what i need (english is not my main language) - string to numeric is not like if string x contains the value "11", so i can convert it to int y = x = 11 ?
-
July 13th, 2006, 05:46 AM
#12
Re: Urgent Variable Question
 Originally Posted by benzedrine
i cant find that in the FAQ, anyway im not sure that is what i need (english is not my main language) - string to numeric is not like if string x contains the value "11", so i can convert it to int y = x = 11 ?
Forget about the conversions, use the map. Here is where you can find how to use it - http://www.cppreference.com/cppmap/index.html
Non-fool-proof sample code:
Code:
#include<map>
#include<iostream>
int main()
{
char user_input;
std::map<char, int> my_map;
int a = 10;
int b = 20;
my_map['a'] = a;
my_map['b'] = b;
std::cout << "Enter character\n";
std::cin >> user_input;
std::cout << "Corresponding value is : " << my_map[user_input] << "\n";
}
Last edited by exterminator; July 13th, 2006 at 05:54 AM.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
July 13th, 2006, 06:06 AM
#13
Re: Urgent Variable Question
 Originally Posted by exterminator
Forget about the conversions, use the map. Here is where you can find how to use it - http://www.cppreference.com/cppmap/index.html
Non-fool-proof sample code:
Code:
#include<map>
#include<iostream>
int main()
{
char user_input;
std::map<char, int> my_map;
int a = 10;
int b = 20;
my_map['a'] = a;
my_map['b'] = b;
std::cout << "Enter character\n";
std::cin >> user_input;
std::cout << "Corresponding value is : " << my_map[user_input] << "\n";
}
actually, tanks, but this doesn't help.. the problem that i had with the most simple way of doing that (using the if statment) is that my code had to contain 81 lines (one for each variable) every time i wanted to do this..
the solution u gave me is just like that..
my_map['a'] = a1;
my_map['b'] = a2;
.
.
.
my_map['a'] = i8;
my_map['b'] = i9;
im looking for a FAST 1-2 lines to solve this and get to the desirable effect, if there is any..
can u think of another solution PLZ?
-
July 13th, 2006, 06:41 AM
#14
Re: Urgent Variable Question
Code:
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
int a1=0, b1=0, c1=0, d1=0, e1=0, f1=0, g1=0, h1=0, i1=0,
a2=0, b2=0, c2=0, d2=0, e2=0, f2=0, g2=0, h2=0, i2=0,
a3=0, b3=0, c3=0, d3=0, e3=0, f3=0, g3=0, h3=0, i3=0,
a4=0, b4=0, c4=0, d4=0, e4=0, f4=0, g4=0, h4=0, i4=0,
a5=0, b5=0, c5=0, d5=0, e5=0, f5=0, g5=0, h5=0, i5=0,
a6=0, b6=0, c6=0, d6=0, e6=0, f6=0, g6=0, h6=0, i6=0,
a7=0, b7=0, c7=0, d7=0, e7=0, f7=0, g7=0, h7=0, i7=0,
a8=0, b8=0, c8=0, d8=0, e8=0, f8=0, g8=0, h8=0, i8=0,
a9=0, b9=0, c9=0, d9=0, e9=0, f9=0, g9=0, h9=0, i9=0;
int *p[81]={&a1, &a2, &a3, &a4, &a5, &a6, &a7, &a8, &a9, &b1, &b2, &b3, &b4, &b5, &b6, &b7, &b8, &b9,
&c1, &c2, &c3, &c4, &c5, &c6, &c7, &c8, &c9, &d1, &d2, &d3, &d4, &d5, &d6, &d7, &d8, &d9,
&e1, &e2, &e3, &e4, &e5, &e6, &e7, &e8, &e9, &f1, &f2, &f3, &f4, &f5, &f6, &f7, &f8, &f9,
&g1, &g2, &g3, &g4, &g5, &g6, &g7, &g8, &g9, &h1, &h2, &h3, &h4, &h5, &h6, &h7, &h8, &h9,
&i1, &i2, &i3, &i4, &i5, &i6, &i7, &i8, &i9 };
string x1="\0";
cout<<"x1 = ";
cin>>x1;
cout<<endl<<x1<<" = ";
cin>>*p[x1[0]+x1[1]];
cout<<"\na1= "<<a1<<endl
<<"a2= "<<a2<<endl
<<"a3= "<<a3<<endl
<<"a4= "<<a4<<endl
<<"a5= "<<a5<<endl
<<"a6= "<<a6<<endl
<<"a7= "<<a7<<endl
<<"a8= "<<a8<<endl
<<"a9= "<<a9<<endl
<<"b2= "<<b2<<endl
<<"f5= "<<f5<<"\n\n";
return 0;
}
why doesn't this work? and how can i get it to work?..
thnks
-
July 13th, 2006, 07:04 AM
#15
Re: Urgent Variable Question
Can you explain the physical significance of what you are trying to achieve? What do those huge number of variables used for and mean? And how are they to be used? May be then someone could suggest something better.
If all those variables are supposed to be constants, then a file can be used to keep them and hence work with them... but first the answers to above questions are important!
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|