C++ toupper help!

Boogieboo6

@realDonaldTrump
OP
Member
Joined
Jul 30, 2015
Messages
965
Trophies
1
Age
23
XP
807
Country
United States
Today I've been having trouble using toupper. Can somebody point out what's wrong? I made a little example in Visual Studio quickly, it isn't what all my code looks like! :D
Thanks for the help everybody.
/*Enter anything. If you type n, or N, success should show up! Anything else should show failure.
n
Failure...
Press any key to continue . . .

Enter anything. If you type n, or N, success should show up! Anything else should show failure.
N
Success!
Press any key to continue . . .*/
#include <iostream>
using namespace std;
int main()
{
char variable = 'n';
cout << "Enter anything. If you type n, or N, success should show up! Anything else should show failure.\n";
cin >> variable;
if (toupper(variable == 'N'))
cout << "Success!\n";
else
cout << "Failure...\n" ;
system("pause");
return 0;
}
 

The Real Jdbye

*is birb*
Member
Joined
Mar 17, 2010
Messages
23,374
Trophies
4
Location
Space
XP
13,978
Country
Norway
Today I've been having trouble using toupper. Can somebody point out what's wrong? I made a little example in Visual Studio quickly, it isn't what all my code looks like! :D
Thanks for the help everybody.
/*Enter anything. If you type n, or N, success should show up! Anything else should show failure.
n
Failure...
Press any key to continue . . .

Enter anything. If you type n, or N, success should show up! Anything else should show failure.
N
Success!
Press any key to continue . . .*/
#include <iostream>
using namespace std;
int main()
{
char variable = 'n';
cout << "Enter anything. If you type n, or N, success should show up! Anything else should show failure.\n";
cin >> variable;
if (toupper(variable == 'N'))
cout << "Success!\n";
else
cout << "Failure...\n" ;
system("pause");
return 0;
}
Try:
if (toupper(variable) == 'N')
 
  • Like
Reactions: Boogieboo6

Logan Pockrus

Knawledge is key.
Member
Joined
Jan 1, 2016
Messages
1,338
Trophies
0
XP
1,062
Country
United States
Question: what's the point of using toupper in this way? Why not just use:
Code:
int main()
{
    // ...
    if(variable == 'N' || variable == 'n')
        std::cout<< "Success!\n";
    else
        std::cout<< "Failure...\n";    // personally, I'd use std::cerr right here

    std::cin.get();    // as opposed to "system("pause")"
    return 0;
}

Some other things are changed, but they're not as important.
 
  • Like
Reactions: Boogieboo6

Boogieboo6

@realDonaldTrump
OP
Member
Joined
Jul 30, 2015
Messages
965
Trophies
1
Age
23
XP
807
Country
United States
Try:
if (toupper(variable) == 'N')
Try this instead:
Code:
if (toupper(variable) == 'N')
Thanks to the both of you! This worked!
Question: what's the point of using toupper in this way? Why not just use:
Code:
int main()
{
    // ...
    if(variable == 'N' || variable == 'n')
        std::cout<< "Success!\n";
    else
        std::cout<< "Failure...\n";    // personally, I'd use std::cerr right here

    std::cin.get();    // as opposed to "system("pause")"
    return 0;
}

Some other things are changed, but they're not as important.
That works well, and it's what my friend used. toupper is shorter to write, but I guess it's also easy to mess up as shown by me!
I haven't learned std::cerr or std::cin.get() yet, and I had no clue what they were up until this point. Until I learn them in class, I'll keep going with system("pause") and another cout. Thanks for the tip though, I'll try those out sometime.
 

Logan Pockrus

Knawledge is key.
Member
Joined
Jan 1, 2016
Messages
1,338
Trophies
0
XP
1,062
Country
United States
Thanks to the both of you! This worked!

That works well, and it's what my friend used. toupper is shorter to write, but I guess it's also easy to mess up as shown by me!
I haven't learned std::cerr or std::cin.get() yet, and I had no clue what they were up until this point. Until I learn them in class, I'll keep going with system("pause") and another cout. Thanks for the tip though, I'll try those out sometime.
Yeah, not trying to force my style down your throat, just a habit. :rofl:
Just in case you're wondering, std::cin.get() pauses the program and waits until the "enter" key is pressed (behaves like "pause >nul" if you're familiar with batch programming). std::cerr is essentially std::cout with a few minor changes; however, it is used for outputting errors, for whatever reason.
 
Last edited by Logan Pockrus,
  • Like
Reactions: Boogieboo6

Boogieboo6

@realDonaldTrump
OP
Member
Joined
Jul 30, 2015
Messages
965
Trophies
1
Age
23
XP
807
Country
United States
Hey everybody! This is what I was able to make thanks to your help with toupper! This is an assignment from my coding class.
/*(Name Removed) C++ 5 Chapter 5 Lab 3
How much money is owed?
125.99
Are you a member? Y/N?
n
You owe $126.98.
Press any key to continue . . .

How much money is owed?
400
Are you a member? Y/N?
y
You owe $360.99.
Press any key to continue . . .*/
#include <iostream>
using namespace std;
int main()
{
//define variables
const double DISCOUNT_RATE = 0.1;
const double SHIPPING1 = 0.99;
const double SHIPPING2 = 4.99;
double amountOwed = 0.0;
char memStat = 'Y';
//gathering info
cout << "How much money is owed?\n";
cin >> amountOwed;
cout << "Are you a member? Y/N?\n";
cin >> memStat;
//algorithm start!
if (toupper(memStat) == 'Y')
amountOwed = amountOwed - (DISCOUNT_RATE * amountOwed);
//end if
if (amountOwed >= 100)
amountOwed = amountOwed + SHIPPING1;
else
amountOwed = amountOwed + SHIPPING2;
//end if
cout << "You owe $" << amountOwed << ".\n";
system("pause");
return 0;
}
 

grossaffe

Well-Known Member
Member
Joined
May 5, 2013
Messages
3,007
Trophies
0
XP
2,799
Country
United States
Instead of commenting "//end if", why not just make your if statements explicitly contained?
Code:
if (something) {
do stuff;
do more stuff;
}
else {
do something else;
}
 
  • Like
Reactions: Scarlet

0x40

Well-Known Member
Member
Joined
Apr 20, 2013
Messages
281
Trophies
1
Location
/
XP
807
Country
United States
Instead of commenting "//end if", why not just make your if statements explicitly contained?
Code:
if (something) {
do stuff;
do more stuff;
}
else {
do something else;
}
You don't need the brackets if it's only one line. You can do it like this:

Code:
if (something)
    do_stuff();
 

Boogieboo6

@realDonaldTrump
OP
Member
Joined
Jul 30, 2015
Messages
965
Trophies
1
Age
23
XP
807
Country
United States
Instead of commenting "//end if", why not just make your if statements explicitly contained?
Code:
if (something) {
do stuff;
do more stuff;
}
else {
do something else;
}
The teacher said to end if statements with //end if. I have no clue why, but maybe it's just to form a habit for later down the line. I also do use those brackets sometimes, and other times I just forget to put them in.
 

grossaffe

Well-Known Member
Member
Joined
May 5, 2013
Messages
3,007
Trophies
0
XP
2,799
Country
United States
You don't need the brackets if it's only one line. You can do it like this:

Code:
if (something)
    do_stuff();
Yes, that's what he did. But then commented "//end if" after each of those to tell him where the end of the if statement was. (plus, I'm not a fan of doing it that way as it can lead to headaches down the line when someone doesn't notice it was done that way and tries to add more code into it)
 

Logan Pockrus

Knawledge is key.
Member
Joined
Jan 1, 2016
Messages
1,338
Trophies
0
XP
1,062
Country
United States
You don't need the brackets if it's only one line. You can do it like this:

Code:
if (something)
    do_stuff();
I bet he knows that, but uses brackets anyway. A lot of people do, primarily because they find it more readable.
The teacher said to end if statements with //end if. I have no clue why, but maybe it's just to form a habit for later down the line.
Probably preparation for macros, but I'm just guessing.
 
  • Like
Reactions: Boogieboo6

0x40

Well-Known Member
Member
Joined
Apr 20, 2013
Messages
281
Trophies
1
Location
/
XP
807
Country
United States
The teacher said to end if statements with //end if. I have no clue why, but maybe it's just to form a habit for later down the line. I also do use those brackets sometimes, and other times I just forget to put them in.
Was your teacher also the one who told you to use system("pause"), by any chance?
 

0x40

Well-Known Member
Member
Joined
Apr 20, 2013
Messages
281
Trophies
1
Location
/
XP
807
Country
United States
Yes, that's what he did. But then commented "//end if" after each of those to tell him where the end of the if statement was. (plus, I'm not a fan of doing it that way as it can lead to headaches down the line when someone doesn't notice it was done that way and tries to add more code into it)
When someone tries to add another line to an if statement, any sane editor will add the brackets for you so you don't have to do that manually. If they're not using a sane editor, they're probably not sane themselves, and them being unable to get a working commit into my project is more of a benefit.
Yep! I was taught that by the same teacher!
Run. He's a moron and will teach you nothing but terrible coding habits. Run, and don't look back.
 

Boogieboo6

@realDonaldTrump
OP
Member
Joined
Jul 30, 2015
Messages
965
Trophies
1
Age
23
XP
807
Country
United States
When someone tries to add another line to an if statement, any sane editor will add the brackets for you so you don't have to do that manually. If they're not using a sane editor, they're probably not sane themselves, and them being unable to get a working commit into my project is more of a benefit.

Run. He's a moron and will teach you nothing but terrible coding habits. Run, and don't look back.
Why are system("pause") and commenting //end if terrible coding habits?
 

grossaffe

Well-Known Member
Member
Joined
May 5, 2013
Messages
3,007
Trophies
0
XP
2,799
Country
United States
The teacher said to end if statements with //end if. I have no clue why, but maybe it's just to form a habit for later down the line. I also do use those brackets sometimes, and other times I just forget to put them in.
Well that's... I guess this is supposed to be a "first foray into programming" class? I've got some reservations with this class

When someone tries to add another line to an if statement, any sane editor will add the brackets for you so you don't have to do that manually. If they're not using a sane editor, they're probably not sane themselves, and them being unable to get a working commit into my project is more of a benefit.
I'm gonna have to disagree with you there. The editor does not know if my intention is to add the code into the if statement or not, nor do I want it deciding for me what I want to do. I stick to the basics. I've used Netbeans and Eclipse in the past, which are decent IDEs. Done a bit of work in a generic text editor. But nowadays I work mostly in vim which certainly does not add brackets for me (maybe there's a plugin to do that, but I wouldn't want it).
 

0x40

Well-Known Member
Member
Joined
Apr 20, 2013
Messages
281
Trophies
1
Location
/
XP
807
Country
United States
Why are system("pause") and commenting //end if terrible coding habits?
What system("pause") does, is that it basically searches your computer for a program named "pause", and then runs it. If a user has a program named "pause" anywhere in their system path, it will execute it regardless of what it does. It's also slow compared to other methods, and won't work on any system that doesn't have that program.
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • BakerMan
    I rather enjoy a life of taking it easy. I haven't reached that life yet though.
  • K3Nv2 @ K3Nv2:
    No real point since movies are 30fps
  • BigOnYa @ BigOnYa:
    Not a big movie buff, more of a gamer tbh. And Series X is 120hz 8k ready, but yea only 120hz 4k games out right now, but thinking of in the future.
  • K3Nv2 @ K3Nv2:
    Mostly why you never see TV manufacturers going post 60hz
  • BigOnYa @ BigOnYa:
    I only watch tv when i goto bed, it puts me to sleep, and I have a nas drive filled w my fav shows so i can watch them in order, commercial free. I usually watch Married w Children, or South Park
  • K3Nv2 @ K3Nv2:
    Stremio ruined my need for nas
  • BigOnYa @ BigOnYa:
    I stream from Nas to firestick, one on every tv, and use Kodi. I'm happy w it, plays everything. (I pirate/torrent shows/movies on pc, and put on nas)
  • K3Nv2 @ K3Nv2:
    Kodi repost are still pretty popular
  • BigOnYa @ BigOnYa:
    What the hell is Kodi reposts? what do you mean, or "Wut?" -xdqwerty
  • K3Nv2 @ K3Nv2:
    Google them basically web crawlers to movie sites
  • BigOnYa @ BigOnYa:
    oh you mean the 3rd party apps on Kodi, yea i know what you mean, yea there are still a few cool ones, in fact watched the new planet of the apes movie other night w wifey thru one, was good pic surprisingly, not a cam
  • BigOnYa @ BigOnYa:
    Damn, only $2.06 and free shipping. Gotta cost more for them to ship than $2.06
    +1
  • BigOnYa @ BigOnYa:
    I got my Dad a firestick for Xmas and showed him those 3rd party sites on Kodi, he loves it, all he watches anymore. He said he has got 3 letters from AT&T already about pirating, but he says f them, let them shut my internet off (He wants out of his AT&T contract anyways)
  • K3Nv2 @ K3Nv2:
    That's where stremio comes to play never got a letter about it
  • BigOnYa @ BigOnYa:
    I just use a VPN, even give him my login and password so can use it also, and he refuses, he's funny.
  • BigOnYa @ BigOnYa:
    I had to find and get him an old style flip phone even without text, cause thats what he wanted. No text, no internet, only phone calls. Old, old school.
  • Psionic Roshambo @ Psionic Roshambo:
    @BigOnYa, Lol I bought a new USB card reader thing on AliExpress last month for I think like 87 cents. Free shipping from China... It arrived it works and honestly I don't understand how it was so cheap.
    +1
  • BakerMan @ BakerMan:
    fellas
  • BakerMan @ BakerMan:
    would you rather have a 9-5 desk job with poor pay or work for an intergalactic space militia with no guarantee of being paid?
  • BakerMan @ BakerMan:
    basically, normal boring job or halo and/or helldivers irl
  • SylverReZ @ SylverReZ:
    Lol. Have you heard about this?
  • SylverReZ @ SylverReZ:
    I wish the people who make these emulators know the basic primer course on copyright and trademarks.
    SylverReZ @ SylverReZ: I wish the people who make these emulators know the basic primer course on copyright and trademarks.