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,281
Trophies
4
Location
Space
XP
13,836
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
  • No one is chatting at the moment.
    The Real Jdbye @ The Real Jdbye: sure, it can be hands free