Hello everyone, now I’ve dealt with the cctype library in C and came across a toupper function that changes characters from lower case to upper case. Here I wrote a small code (I did not use char, as in the example):

#include <iostream> #include <cctype> using namespace std; int main() { string str1; cout << "Введите Y или N для дальнейшей работы: "; do { str1 = cin.get(); str1 = toupper(str1); } while(str1 != "Y" || str1 != "N") if (str1 == "Y") { cout << "Спасибо, что ввели: " << "str1" << endl; } else {cout << "попробуйте еще раз" << endl;} } 

I get a compiler error:

prog.cpp: 12:28: error: no matching function for call to 'toupper (std :: string &)' str1 = toupper (str1);

I can not understand what's the matter - because everything seems to be correctly written in the 12th line. Help me fix it.

  • @Hamsternik, isn't it Sishny toupper ()? It needs to pass one character, not std :: string. - avp
  • Yes exactly! It turns out that you just had to write: str1 = toupper (str1 [0]); But the problem is now that the program works forever ... - hamsternik
  • Well, you again have an error with cin.get (); Clean up the input stream after use - brightside90
  • @Hamsternik, besides, some strange logic - why use std :: string to rewrite its contents each time you enter a new character, and also apply a C-function to it. You can also take a variable of type char, enter a new character each time into it, and simply add it to your str1 line, and output it at the output. - brightside90

2 answers 2

You can connect the header file algorithm and use its benefits in the form of the function std::transform :

 #include <algorithm> ... string str1; getline(cin, str1); std::transform(str1.begin(), str1.end(), str1.begin(), toupper); ... 

What can be done specifically in your case:

  1. As I wrote in the comment above, add a variable of type char for the character to be entered (you, as I understand it, form a string of single characters);
  2. Add with each new entry this character to your string str1
  3. Slightly change the cycle

 char c = '\0'; cout << "Введите Y или N для дальнейшей работы: "; while(1) { c = cin.get(); c = toupper(c); if (c == 'Y') { cout << "Спасибо, что ввели: " << str1 << endl; break; } cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); str1 = str1 + c; } 
  • Don't you have to write like that? std :: transform (str1.begin (), str1.end (); str1.begin (), :: toupper); - hamsternik
  • And yes - so you can write, I just want to understand what the problem is in my version? It seems everything is correctly written down ... - hamsternik
  • 1. Your record is fundamentally different from mine only by the presence of '::', i.e., the operator resolving the scope, but in this case it is not critical, you can leave it, but you can remove it. 2. If you look at the documentation, it does not say that toupper accepts an argument of type std :: string, so the problem is this. - brightside90
  • Thanks for the decision. All the same, your bike is not always good, especially when it is already unclear what :) - hamsternik
  • However, the original version does not work for me, where "::" is not used before the toupper. - hamsternik

A function from C accepts an int value (into which it is implicitly converted from a char element). Therefore, you need to take a pointer to a buffer (the c_str () method) and bypass the entire line in a loop. http://www.cplusplus.com/reference/cctype/toupper/

For std :: string, take a look at: http://www.cplusplus.com/reference/locale/toupper/