alternative to .getline?

I am asking for a single character from the user so i can search for the items in an array that match the character. Each character corresponds with an Enum type. Right now i am using cin.getline but i am getting an error on cin.getline. is there an alternative to using getline but for different variable types like an enum type?

This is what I have
1
2
3
4
5
6
7
  Category findCat[1]; //character corresponding to the category to search for
//Categories are : enum Category {SONG, POEM, ESSAY, NOVEL, NOTE}; 

  cout << "Input the category you would like to search for. Input the first character of the category: " << endl;
  cin.getline(findCat, 2); //this is where the error is "no instance of overloaded //function matches the argument list

int thisCat = FindCategory(findCat);
If you are just trying to get a single character you can use std::cin.get()

http://www.cplusplus.com/reference/istream/istream/get/?kw=cin.get
There are two forms of getline(). Neither accepts an enum, even if it did, it wouldn't work the way you want since an enum is an integer type, not a character type.

BTW, if the user inputs the first character of the category. how are you going to distinguish between Novel and Note?





if i changed findCat to a char, would that be more effective?
Yes. You could do:
1
2
3
 
  char findCat;
  cin >> findCat;


That gets you a character, but you still have to translate the character to an enum.

1
2
3
4
5
6
7
8
9
10
  string firstchars = "SPEN";
  size_t temp;
  Categoty cat; 

  temp = firstchars.find (findCat);
  if (temp == string::npos)
  {  // not found 
  } 
  cat = (Category)temp;  // Cast from size_t to enum
}

As I pointed out before, this won't distinguish between Note and Novel.

IMO, you would be better off asking the user to enter a number.
1
2
3
4
5
6
7
8
9
10
 
  int num;
  Category cat;

  do
  { cout << "0=Song,1=Poem,2=Essay,3=Novel,4=Note" << endl;
     cin >> num;
  }
  while (num < 0 || num > 4);
  cat = (Category)num;
Last edited on
Topic archived. No new replies allowed.