Re: Urgent Variable Question
You mean you dont want to use if else conditions ?
Any reason ?
Re: Urgent Variable Question
Quote:
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
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.
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; }
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.
Re: Urgent Variable Question
BTW from where do you get the variable name in string ? like "C" ,"B"...
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).
Re: Urgent Variable Question
Quote:
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?
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.
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 ?
Re: Urgent Variable Question
Quote:
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";
}
Re: Urgent Variable Question
Quote:
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?
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
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!
Re: Urgent Variable Question
Supposing x1 is a string value between "a1" and "i9", I guess instead of
Code:
cin>>*p[x1[0]+x1[1]];
you should try
Code:
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
Code:
int array[9][9];
cin >> array[x1[0]-'a'][x1[1]-'1'];
or even better.
Re: Urgent Variable Question
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.
Re: Urgent Variable Question
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
Code:
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 !
Re: Urgent Variable Question
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..
Re: Urgent Variable Question
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?
Re: Urgent Variable Question
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..