toupper function

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2007
Posts: 113
Reputation: zoner7 is an unknown quantity at this point 
Solved Threads: 4
zoner7 zoner7 is offline Offline
Junior Poster

toupper function

 
0
  #1
Jun 7th, 2008
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.

  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;
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 509
Reputation: selvaganapathy is an unknown quantity at this point 
Solved Threads: 88
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Pro

Re: toupper function

 
0
  #2
Jun 7th, 2008
Hi
I think
x [i] = toupper (x[i]) ;
KSG
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 509
Reputation: selvaganapathy is an unknown quantity at this point 
Solved Threads: 88
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Pro

Re: toupper function

 
0
  #3
Jun 7th, 2008
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.
KSG
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 280
Reputation: joshmo is an unknown quantity at this point 
Solved Threads: 19
joshmo joshmo is offline Offline
Posting Whiz in Training

Re: toupper function

 
0
  #4
Jun 7th, 2008
why dont you do something like this
  1. if (x == "EASY" || "easy" )
  2. {
  3. difficulty = 0;
  4. }
  5. // and so on..
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 12
Reputation: Arpy Giri is an unknown quantity at this point 
Solved Threads: 3
Arpy Giri's Avatar
Arpy Giri Arpy Giri is offline Offline
Newbie Poster

Re: toupper function

 
0
  #5
Jun 7th, 2008
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[i]=toupper(x[i]);
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: toupper function

 
0
  #6
Jun 7th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 280
Reputation: joshmo is an unknown quantity at this point 
Solved Threads: 19
joshmo joshmo is offline Offline
Posting Whiz in Training

Re: toupper function

 
0
  #7
Jun 7th, 2008
thanx for the correction...didnt see it there.. but actully this was the intetion
  1. if (x == "EASY" || x=="easy" )
Last edited by joshmo; Jun 7th, 2008 at 6:40 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 280
Reputation: joshmo is an unknown quantity at this point 
Solved Threads: 19
joshmo joshmo is offline Offline
Posting Whiz in Training

Re: toupper function

 
0
  #8
Jun 7th, 2008
[edit] sorry multiple post
Last edited by joshmo; Jun 7th, 2008 at 6:45 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 47
Reputation: Cybulski is an unknown quantity at this point 
Solved Threads: 3
Cybulski's Avatar
Cybulski Cybulski is offline Offline
C++ wannabe

Re: toupper function

 
0
  #9
Jun 7th, 2008
  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC