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;

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.