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??
Krishnaa
July 13th, 2006, 04:29 AM
You mean you dont want to use if else conditions ?
Any reason ?
AsaF
July 13th, 2006, 04:30 AM
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
Viorel
July 13th, 2006, 04:36 AM
Maybe you need something like this:
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.
benzedrine
July 13th, 2006, 04:40 AM
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:
string x; cin>>x;
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:
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.
Krishnaa
July 13th, 2006, 04:47 AM
BTW from where do you get the variable name in string ? like "C" ,"B"...
benzedrine
July 13th, 2006, 04:54 AM
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).
benzedrine
July 13th, 2006, 05:07 AM
Maybe you need something like this:
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?
exterminator
July 13th, 2006, 05:15 AM
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.
benzedrine
July 13th, 2006, 05:37 AM
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 ?
exterminator
July 13th, 2006, 05:46 AM
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:#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";
}
benzedrine
July 13th, 2006, 06:06 AM
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:#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..
why doesn't this work? and how can i get it to work?..
thnks
exterminator
July 13th, 2006, 07:04 AM
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!
Viorel
July 13th, 2006, 07:05 AM
Supposing x1 is a string value between "a1" and "i9", I guess instead of
cin>>*p[x1[0]+x1[1]];
you should try
cin >> *p[ (x1[0]-'a') * 9 + (x1[1]-'1') ];
But I think you should consider a methot based on 9x9 array, already suggested before by Krishnaa.
Somethink like this
int array[9][9];
cin >> array[x1[0]-'a'][x1[1]-'1'];
or even better.
NMTop40
July 13th, 2006, 07:07 AM
I doubt you want that. A matrix would be a better idea. Even a 2-d array would be.
For a sudoku solver you'd probably want a method to run through:
1. elements of a row.
2. elements of a column
3. elements of a box.
the first two can be done easily with a matrix, 2-d array or even valarray.
3rd one may require a bit of programming.
benzedrine
July 13th, 2006, 07:33 AM
lol yes i know.. the array of int x[9][9] would cover the whole thing "without" a problem, but the problems will pop out when i get to the logical part of sudoku solving (my first attempt of a sudoku solving program was obviously using an [9][9] array).
anyway, the
cin >> *p[ (x1[0]-'a') * 9 + (x1[1]-'1') ];
really did the job perfectly- THANKS!!!
however, i really want to learn about it, so can u plz explain to me way does this work?
and another thing- if i have a string x, and i insert "a1" to it, the x[0] would be the number and the x[1] would be the char (in this order), or the other way around?
THANKS FOR THE HELP AND THE SPEED !
benzedrine
July 13th, 2006, 10:10 AM
could someone plz explain it to me why does it work now, so i can do it on me own for the next times?
thanks..
cvogt61457
July 13th, 2006, 01:53 PM
Can you post your project so that we can see what you've tried?
Also, what doesn't work?
Have you tried debugging the program to determine where the failure
occurs?
All you say is that it doesn't work. Where? What? How?
benzedrine
July 14th, 2006, 08:48 AM
actually i was kinda focus on the problem.. and the fact is that one of u guys helped me alot and solved me problem..
i just didn't understand how he did it lol..
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.