-5

When I try to compile the following code, I get the error

strupr.cpp: In function ‘int main()’: strupr.cpp:9:10: error: ‘strupr’ was not declared in this scope cout << strupr(st) << endl; ^~~~~~ strupr.cpp:9:10: note: suggested alternative: ‘strstr’ cout << strupr(st) << endl; ^~~~~~ strstr

#include<iostream>
#include<cstring>

using namespace std;

int main(void){
    cout << strupr("hello world") << endl;
}
Phidelux
  • 2,043
  • 1
  • 32
  • 50
Aryan Choudhary
  • 492
  • 1
  • 8
  • 14
  • 2
    Please include code *in the question*, not as link and especially not as image. – Yksisarvinen Mar 04 '20 at 15:02
  • your title says `string.h` and that was also in the screenshot. Now you have `cstring`, which is it? And what compiler error do you get? – 463035818_is_not_an_ai Mar 04 '20 at 15:10
  • 1
    The c-String functions are located in the cstring header not string.h. Furthermore is stdupr not a standard function and may not be included in your libc. Use [std::transform](https://en.cppreference.com/w/cpp/algorithm/transform) with [std::toupper](https://en.cppreference.com/w/cpp/string/byte/toupper) instead. – Phidelux Mar 04 '20 at 15:11
  • I used both cstring and string.h and none of them helped me – Aryan Choudhary Mar 04 '20 at 15:12
  • 1
    `int main(void){` is not valid C++ code. – Thomas Sablik Mar 04 '20 at 15:13
  • 1
    It's `namespace` and not `namesapce` – Thomas Sablik Mar 04 '20 at 15:14
  • 1
    Voted to reopen. It's clear what the problem is, and this down-vote fever is inappropriate. – Pete Becker Mar 04 '20 at 15:16
  • 3
    `strupr` converts its argument to uppercase. `strupr("hello world");` attempts to convert a literal string to uppercase; you're not allowed to modify a literal string, so this won't work. `char str[] = "hello world"; strupr(str);` is okay, because `str` is not a literal, and can be modified. – Pete Becker Mar 04 '20 at 15:17
  • 1
    OP posted code as image and the code doesn't reproduce the problem because it contains typos. That makes it a bad question. `strupr` is a Microsoft extension. It's not part of standard C or C++. – Thomas Sablik Mar 04 '20 at 15:17
  • What error do you get? – JohnFilleau Mar 04 '20 at 15:21
  • @PeteBecker That's good enough for an answer I think. – Ted Lyngmo Mar 04 '20 at 16:01
  • 1
    @TedLyngmo -- yes, but at the time I posted the question was closed, so I couldn't post an answer. – Pete Becker Mar 04 '20 at 16:27
  • @Phidelux -- The c-String functions are declared in **both** `` and ``. The first one declares them in the global namespace. The second one declares them in namespace `std` and, optionally, in the global namespace. – Pete Becker Mar 04 '20 at 16:45

3 Answers3

3

In C++ you want to do something like this:

#include<iostream>
#include<string>
#include <algorithm>
#include <cctype>

int main(){
    std::string s = "hello world";
    std::transform(s.begin(), s.end(), s.begin(), ::toupper);
    std::cout << s << std::endl;
    return 0;
}
theWiseBro
  • 1,439
  • 12
  • 11
2

First, strupr is not part of the standard C library nor the standard C++ library. So there is no standard header that is required to declare it.

That said, it started out as a Microsoft thing, and was, indeed, declared in <string.h>. See this question for more discussion.

So, unless your compiler has it as an extension, it won't exist. That's what the error message is telling you.

Even if it does exist, you can't use it the way that the code in the question tries to do. The problem is that it modifies the contents of its argument, so you can't pass a const char* to it. That's what this code does:

strupr("hello world");

"hello world" is a string literal, and in the olden days its type was char[12], that is, formally, it looked like a modifiable string. But in fact, it was up to the compiler to decide whether string literals were modifiable, so with some compilers it was okay and with others it wasn't. When it wasn't, it would be because the compiler put string literals in read-only memory, so attempting to modify the literal would produce a runtime error.

In C++, the type of "hello world" is const char[12], that is, the characters in the string are not modifiable. So calling strupr("hello world") is ill-formed, and the compiler should issue a diagnostic.

To make it work, if your compiler provides this function, you have to call it on a modifiable string:

char str[] = "hello world";
strupr(str);

str is a modifiable array of char that holds a copy of the text in its initializer, in this case, "hello world". That string can be modified, so strupr can do its job.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
-1

Since strupr() is a string manipulation function, it is natural to mistake that it belongs to string.h header file. But, it is actually defined in the cstring header file, which is used to convert all the characters in a string to upper case letters i.e. a string which is stored in a c-style char array.

Try to include cstring.h and it should run without error. If you further get errors, please include the error as well, as we can get a better idea about what might be going wrong.

A J A Y
  • 585
  • 1
  • 7
  • 22
  • `strupr` is not standard C nor standard C++, so neither language specifies a header that declares it. And `cstring.h` is not a standard header. – Pete Becker Mar 04 '20 at 16:33
  • In general, the names declared in the `` headers from C are also declared in the corresponding `` headers in C++. As best as I can recall, there are no functions declared in the `` headers that do not have counterparts in the `` headers. After all, the reason they exist is to provide access to the standard C library. In a handful of cases the **signatures** were changed in C++ to improve const-correctness, and that sometimes meant providing overloads with different const qualifiers, but the names are still the same. – Pete Becker Mar 04 '20 at 16:48