I've got a pretty simple question. I'm trying to ask the player for to choose a difficulty. I want the case to be to not affect his choice, so I am using the toupper() function. Unfortunately, when I try to return the value of the difficulty, no matter what difficulty I choose, I receive 2009768935. However, when I enter an answer is all caps, avoiding the use of the toupper() function, everything turns out great.

Here is my code.

cout << "Choose a difficulty among Easy, Intermediate, and Hard.";
    string x;
    int difficulty;
    cin >> x;
    for (int i =0; i <=12; i++)
    { x[i] = x[toupper(i)]; }
    if (x == "EASY" )
    { difficulty = 0; }
    if (x == "INTERMEDIATE" ) 
    { difficulty = 1; }
    if (x == "HARD" )
    { difficulty = 2; }
    
    cout << difficulty;

Dani AI

Generated

A short, focused explanation and safe fixes.

The root problem is the conversion loop: the code calls toupper on the loop index and uses that as a string index (x[toupper(i)]) while also iterating a fixed 0..12 range. That produces out‑of‑bounds access and undefined behavior; the large number printed is consistent with reading an uninitialized or corrupted difficulty. and called out the misuse of toupper, and correctly warned about logical errors in the OR expression shown by .

Safely convert the string to uppercase by iterating only over the string length and calling std::toupper with a cast to unsigned char (avoids undefined behavior on negative char values):

for (std::size_t i = 0; i < x.size(); ++i) {
    x[i] = static_cast<char>(std::toupper(static_cast<unsigned char>(x[i])));
}

A concise alternative using the STL (safer than passing toupper directly) is to use std::transform with a small lambda that does the same cast.

Map the normalized string to an integer and initialize difficulty so it is never used uninitialized:

#include <unordered_map>

const std::unordered_map<std::string,int> difficulties = {
    {"EASY", 0}, {"INTERMEDIATE", 1}, {"HARD", 2}
};
int difficulty = -1;
auto it = difficulties.find(x);
if (it != difficulties.end()) difficulty = it->second;

Notes and cautions: always initialize variables, iterate to x.size() instead of a magic constant, and prefer a single else if chain or a lookup table to avoid leaving difficulty unset. If input can contain spaces, use std::getline instead of operator>>.

Recommended Answers

All 8 Replies

Hi
I think x [i] = toupper (x[i]) ;

Hi
I think x [i] = toupper (x[i]) ; Also iterate to the length of the String. Not the fixed value. In the coding u specified 0 to 12.

why dont you do something like this

if (x == "EASY" || "easy" )
    { 
       difficulty = 0; 
    }
// and so on..

i believe your syntax is wrong
here i is an integer and you are converting that integer into upper case which is absurd.
the use of toupper function works differently

x=toupper(x);

> why dont you do something like this
> if (x == "EASY" || "easy" )

You've written (with red bits for clarity) if ( ( x == "EASY" ) || ( "easy" != NULL ) ) In essence, it's always true, because the right hand side is always true, and the OR makes the whole thing true.

thanx for the correction...didnt see it there.. but actully this was the intetion

if (x == "EASY" || x=="easy" )

[edit] sorry multiple post

#include <cctype> // for toupper()
#include <algorithm> // for transform()

transform(x.begin(), x.end(), x.begin(), toupper);

try this

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.