0

I have a question about structures. I'm trying to have the user input a letter and have the the computer return two values. Here's what I have till now:

#include <iostream>
struct coords{
int coordsx1;
int coordsy1;
} a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p;
int main()
{
char first;
int coordsx1;
int coordsy1;
    a.coordsx=0;    e.coordsx=0;    i.coordsx=0;    m.coordsx=0;
    b.coordsx=1;    f.coordsx=1;    j.coordsx=1;    n.coordsx=1;
    c.coordsx=2;    g.coordsx=2;    k.coordsx=2;    o.coordsx=2;
    d.coordsx=3;    h.coordsx=3;    l.coordsx=3;    p.coordsx=3;
    cin >> first;
    coordsx1= first.coordsx; // this is the part that doesn't work
}

For example if the user inputs 'd' for the variable first, I want the computer to set the value of coordsx1 to be equal to 3 and coordsy1 to be equal to 0 (I haven't done the coordsy1 part yet). Also, is this a good way to return more than one value for a user input?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
aminmozel
  • 13
  • 1
  • first is a `char` in the code you provided, not your struct, why do you think that you can access its coordsx member – Fantastic Mr Fox May 21 '14 at 06:42
  • This `a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p;` shouts out for `a[16]` (assuming you didn't miss a letter). – Mats Petersson May 21 '14 at 06:42
  • Also how is `a.coordsx=0;` working? the struct has members `coordsx1`? – Fantastic Mr Fox May 21 '14 at 06:43
  • 1
    The "duplicate" may not be perfect, but you are looking for a feature that C++ doesn't have, called "Reflection" (use a variable with the name of a variable). There are a number of different ways that you COULD implement this, one of which is to use an array and ask for an index of an array [and declare an array as my first comment says] (or calculate the index from a character). – Mats Petersson May 21 '14 at 06:46

2 Answers2

2

What the user inputs in cin >> first is text, and it is stored in first, which is char. This is a variable that contains a character, not the variable whose name is the contained character, which is why it doesn't work.

C++ does not have the reflexive capabilities required to transform the text representation of a variable into the variable name, but you can do the conversion explicitly with chained ifs or, more conveniently, a switch:

switch(first)
{
    case 'a':
        coordsx1 = a.coordsx;
        coordsy1 = a.coordsy;
        break;
    case 'b':
        coordsx1 = b.coordsx;
        coordsy1 = b.coordsy;
        break;
    case 'c':
        coordsx1 = c.coordsx;
        coordsy1 = c.coordsy;
        break;
    // All other cases
    default:
        std::cerr << "Wrong input" << std::endl;
}

Alternatively, you can simply put your data in an array and use the characters to index it, this also makes the data initialisation more readable:

#include <iostream>

struct Coords
{
    int x;
    int y;
};

Coords coords[3] = {
    { 1, 2 }, // a
    { 2, 3 }, // b
    { 3, 4 }, // c
};

int main(int arg, char** argv)
{
    char first;
    int coordsx1;
    int coordsy1;
    std::cin >> first;
    if (first > 'c' || first < 'a')
    {
        std::cerr << "Wrong input" <<  std::endl;
        return 1;
    }
    int index = first - 'a'; // Convert 'a' into 0, 'b' into 1, etc.
    coordsx1 = coords[index].x;
    coordsy1 = coords[index].y;
}
Jacobo de Vera
  • 1,863
  • 1
  • 16
  • 20
1

Ups :-)

You first have to learn that a character in runtime is not a variable definition from compile time!

What you get from cin is a character because you made

char first;

so if you want to select from this input your output, you have to write a cascade of case statements like:

switch ( first ) {
    case 'a': coordsx1=a.coordsx; coordsy1=a.coordsy; break;
    case 'b': coordsx1=b.coordsx; coordsy1=b.coordsy; break;
    ... a lot more ...
    default: std::cerr << "Unknown value entered" << std::endl;
 }

please have a look at std::vector or std::map. Both containers can help a lot in this use case!

Klaus
  • 24,205
  • 7
  • 58
  • 113