Need help reading in a CSTRING and returning a single CHAR from the input

Hello everyone,
I am trying to implement a menu-driven program using C strings. i.e char array
What I want to do is allow the user to type the full name of the menu choice, and return only the FIRST char they input in upper case.

1
2
3
4
5
6
  char option;
  cout << "Display " << endl;
  cout << "Load " << endl;
  cin.get(option)
  return toupper(option[0]);


but it doesn't work. I would really appreciate any help you can provide! Thank you!
First make your var an array
char option[100];

Then get a line
cin.getline(option, sizeof(option));

see https://cplusplus.com/reference/istream/istream/getline/
Okay: heres what I have come up with. I am just struggling on getting the first char from the user input upcasing it and then returning just that single car. I've tried this:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

  char menu()
  {
      char option;
      char userChoice[MAX_CHARS];
      cout << "Please select an option:" << endl;
      cout << "Load" << cout.width(5)
           <<"- Load roster data from an input file" << endl;
      cout << "Display" << cout.width(5)
           << "- Display the current roster data" << endl;
      cout << "GPA" << cout.width(5)
           <<"- Display the average GPA for the roster" << endl;
      cout << "PCT" << cout.width(5)
           <<"-  Display the Pass, Fail, and Completion percentages" << endl;
      cout << "Quit" << cout.width(5)
           <<"- Quit this program" << endl;
      cin.ignore(100, '\n');
      cin.getline(userChoice, MAX_CHARS, '\n');
      cin.ignore(100, '\n');
      // this works but when it first loads you have to type the first menu choice 2x

      // EX load load >> then works as normal 
      for(auto i = 0; i < 1; i++) {
>>      if(strcmp(userChoice[i], "l") == 0 || strcmp(userChoice[i], "L") == 0) {
             option = 'L';
           }

    }
      return option;
  }

Last edited on
I guess somewhere you're using stream extraction cin >> to obtain input from the keyboard? This does not play nice with getline().

>> ignores any leading white-space chars (space, tab, \n) and then extracts from the stream up-to a white space but leaves the white space char in the buffer.

getline() extracts from the buffer all chars up-to and including the terminating char (\n by default if not specified) and does not ignore leading white-space.

When getline() is used after >>, the terminating white-space char is still in the buffer (often \n) which is then obtained by getline() and if it is \n will terminate it.

ignore() will ignore up-to a specified number of chars until the specified delimiter. However, if there are no chars available - or no specified delimiter, this will 'hang' until the specified delimiter has been entered.

If you mix >> with getline(), one way to fix this is to use:

 
cin.ignore();


after each cin >> which will ignore the \n (usually) char terminating the input.

Also to just test the first char, strcmp isn't what is used. Just test the first char direct. eg something like:

 
option = toupper(userChoice[0]);

Last edited on
It's not clear why you're using a char array for your string. This is clearly a C++ program, so what's wrong with using std::string?
kbw -- I would prefer to use the string class honestly, but for this program, it is not an option sadly.

seeplus!! thank you so much for your response this makes things very clear for me.
Topic archived. No new replies allowed.