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,388
Trophies
4
Location
Space
XP
14,022
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
  • SylverReZ @ SylverReZ:
    Bitcoin on the other hand, requires a very powerful mining rig and is more costly.
  • Xdqwerty @ Xdqwerty:
    @SylverReZ, different cultures so probably they have their reasons
  • K3Nv2 @ K3Nv2:
    Yes a $2,000 rig for $5 a day
  • SylverReZ @ SylverReZ:
    @Xdqwerty, Or could be in correlation to laws.
    +1
  • SylverReZ @ SylverReZ:
    Technically, cryptocurrencies aren't illegal, as long as you don't use it for criminal activity. If you sign up with an exchange, and that they get a tip from law enforcement, they have the right to freeze your assets.
    +1
  • Xdqwerty @ Xdqwerty:
    @SylverReZ, Yea I heard law is vastly different there
  • SylverReZ @ SylverReZ:
    Nigeria's central bank did make crypto illegal before, until last year they lifted the ban. Always make sure to check before you exchange. ;)
    +1
  • Xdqwerty @ Xdqwerty:
    @SylverReZ, speaking of crypto i remember reading about a guy who spend like 10 grand bitcoin on a couple pizzas (when bitcoin wasnt as worthy as nowadays)
  • SylverReZ @ SylverReZ:
    @Xdqwerty, That's mental. If he had saved up when Bitcoin sky-rocketed in price, then he might've become the next Jeff Bezos for all I know.
  • Xdqwerty @ Xdqwerty:
    @SylverReZ, he couldnt know the price was gonna skyrocket anyway
  • SylverReZ @ SylverReZ:
    @Xdqwerty, Then it dropped sometime in 2022, 2021 was when it really went up in price.
  • Xdqwerty @ Xdqwerty:
    @SylverReZ, I also recall watching a video of some failed games with their own crypto currency or smth like that, lemme link it to you (it's in spanish tho)
  • SylverReZ @ SylverReZ:
    @Xdqwerty, I remember that. Didn't Steam ban games with NFTs before somehow devs managed to circumvent it? Think EA tried to dabble in NFTs too.
  • SylverReZ @ SylverReZ:
    Glad to hear people realise that NFTs aren't worth it these days. They realise that they're JPEGs hosted on a file-hosting site like AWS or Google Drive.
  • Xdqwerty @ Xdqwerty:
    Atleast it has captions
  • SylverReZ @ SylverReZ:
    @Xdqwerty, Meta also tried to create their own crypto, but that fell through.
  • SylverReZ @ SylverReZ:
    Was called Libra, they had around 100 employees and wanted to hire more near to end of the year.
  • Xdqwerty @ Xdqwerty:
    @SylverReZ, idk why facebook tried to do that metaverse thing if mmo games already existed before
  • Xdqwerty @ Xdqwerty:
    And Yea I refuse to call it meta
  • SylverReZ @ SylverReZ:
    @Xdqwerty, I refuse to call it Meta, too. It's a stupid name.
    +1
  • Xdqwerty @ Xdqwerty:
    @SylverReZ, then why did you call it like that a minute ago?
  • SylverReZ @ SylverReZ:
    I don't get why Facebook ever wanted to dabble into virtual reality, when other companies were doing it.
    +1
  • SylverReZ @ SylverReZ:
    @Xdqwerty, Just to emphasize on what I mean.
    +1
    K3Nv2 @ K3Nv2: https://youtube.com/shorts/TmSRzJxdKJQ?si=usvUpZdAc5TJnICA