-4

Is there a simple way to convert the char choice from a lower case q to upper case Q?

I've tried the c version of toupper but I can't get it to work in C++. I need it that all the characters typed in will be upper case; so, they link to the choice selection of choice in main.

for example if they type in c it is turned to C and the function that C is linked to can be accessed or used.

Code so far without any change:

include <iostream>
#include <stdlib.h>
#include <string>
#include "link.h"

using namespace std;

int main()
{
    link obr;
    string n;
    long int x;
    char choice;

    do{
    cout << "C: Create/Add\n P: Display\nQ: Quit";
    cin >> choice;

      if(choice == 'C'){
                cout << "Name";
                cin >> n;
                cin >> x;
                obr.push(n,x);
        }

    if (choice == 'P'){
        obr.display();
    }
} while(choice != 'Q');

    return 0;
}
Gus
  • 69
  • 5
  • post your code, now it's too broad. C functions can be called from C++ – Jean-François Fabre Nov 13 '16 at 20:13
  • 1
    `std::toupper` should work. Didn't you forget to include the relevant header? You can create a [mcve] and ask why the code with `std::toupper` doesn't compile :) <-- Can be better answered than now, as the best answer is `std::toupper` :) – Rakete1111 Nov 13 '16 at 20:14
  • I would think that a homework assignment to convert lowercase or uppercase, or vice versa, without using standard library functions, would be a part of any introductory curriculum for learning C++. At least it was in my days... – Sam Varshavchik Nov 13 '16 at 20:16
  • 2
    "can't get it to work" is not a problem description – Christian Hackl Nov 13 '16 at 20:17
  • aren't you annoyed when 563 people comment on a bad question, downvotes accumulating, and the OP is not responding? I'll downvote. Shoot, it doesn't work. What's this orange triangle doing there? :) – Jean-François Fabre Nov 13 '16 at 20:18

2 Answers2

1

Just write

#include <cctype>

//...

choice = std::toupper( ( unsigned char )choice );

provided that variable choice has type char.

You should be sure that choice indeed contains an alpha character and not a control symbol.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

If you can ensure that your character-set is compatible with C's toupper() then you can do it quite trivially:

std::string s = "this is a string";
std::transform(s.begin(), s.end(), s.begin(), ::toupper);
Olipro
  • 3,489
  • 19
  • 25