943,791 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2718
  • C++ RSS
Jun 7th, 2008
0

toupper function

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  1. cout << "Choose a difficulty among Easy, Intermediate, and Hard.";
  2. string x;
  3. int difficulty;
  4. cin >> x;
  5. for (int i =0; i <=12; i++)
  6. { x[i] = x[toupper(i)]; }
  7. if (x == "EASY" )
  8. { difficulty = 0; }
  9. if (x == "INTERMEDIATE" )
  10. { difficulty = 1; }
  11. if (x == "HARD" )
  12. { difficulty = 2; }
  13.  
  14. cout << difficulty;
Similar Threads
Reputation Points: 34
Solved Threads: 4
Junior Poster
zoner7 is offline Offline
114 posts
since Dec 2007
Jun 7th, 2008
0

Re: toupper function

Hi
I think
x [i] = toupper (x[i]) ;
Reputation Points: 44
Solved Threads: 101
Posting Pro
selvaganapathy is offline Offline
547 posts
since Feb 2008
Jun 7th, 2008
0

Re: toupper function

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.
Reputation Points: 44
Solved Threads: 101
Posting Pro
selvaganapathy is offline Offline
547 posts
since Feb 2008
Jun 7th, 2008
0

Re: toupper function

why dont you do something like this
C++ Syntax (Toggle Plain Text)
  1. if (x == "EASY" || "easy" )
  2. {
  3. difficulty = 0;
  4. }
  5. // and so on..
Reputation Points: 19
Solved Threads: 20
Posting Whiz in Training
joshmo is offline Offline
280 posts
since Oct 2007
Jun 7th, 2008
0

Re: toupper function

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
Quote ...
x[i]=toupper(x[i]);
Reputation Points: 11
Solved Threads: 3
Newbie Poster
Arpy Giri is offline Offline
14 posts
since Jan 2008
Jun 7th, 2008
0

Re: toupper function

Quote originally posted by joshmo ...
> 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jun 7th, 2008
0

Re: toupper function

thanx for the correction...didnt see it there.. but actully this was the intetion
C++ Syntax (Toggle Plain Text)
  1. if (x == "EASY" || x=="easy" )
Last edited by joshmo; Jun 7th, 2008 at 6:40 am.
Reputation Points: 19
Solved Threads: 20
Posting Whiz in Training
joshmo is offline Offline
280 posts
since Oct 2007
Jun 7th, 2008
0

Re: toupper function

[edit] sorry multiple post
Last edited by joshmo; Jun 7th, 2008 at 6:45 am.
Reputation Points: 19
Solved Threads: 20
Posting Whiz in Training
joshmo is offline Offline
280 posts
since Oct 2007
Jun 7th, 2008
0

Re: toupper function

C++ Syntax (Toggle Plain Text)
  1. #include <cctype> // for toupper()
  2. #include <algorithm> // for transform()
  3.  
  4. transform(x.begin(), x.end(), x.begin(), toupper);

try this
Last edited by Cybulski; Jun 7th, 2008 at 7:27 am.
Reputation Points: 15
Solved Threads: 3
Light Poster
Cybulski is offline Offline
47 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Use a function from a different file (C++)
Next Thread in C++ Forum Timeline: Vector and virtual function questions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC