C++ toupper help!

Discussion in 'Computer Programming, Emulation, and Game Modding' started by Boogieboo6, Sep 30, 2016.

  1. Boogieboo6
    OP

    Boogieboo6 @realDonaldTrump

    Member
    945
    1,278
    Jul 30, 2015
    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.
    My Code
     


  2. The Real Jdbye

    The Real Jdbye Always Remember 30/07/08

    Member
    GBAtemp Patron
    The Real Jdbye is a Patron of GBAtemp and is helping us stay independent!

    Our Patreon
    11,862
    5,017
    Mar 17, 2010
    Norway
    Alola
    Try:
    if (toupper(variable) == 'N')
     
    Boogieboo6 likes this.
  3. 0x40

    0x40 GBAtemp Regular

    Member
    224
    63
    Apr 20, 2013
    Try this instead:
    Code:
    if (toupper(variable) == 'N')
     
    Boogieboo6 likes this.
  4. Logan Pockrus

    Logan Pockrus Knawledge is key.

    Member
    1,339
    1,020
    Jan 1, 2016
    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.
     
    Boogieboo6 likes this.
  5. Boogieboo6
    OP

    Boogieboo6 @realDonaldTrump

    Member
    945
    1,278
    Jul 30, 2015
    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.
     
  6. Logan Pockrus

    Logan Pockrus Knawledge is key.

    Member
    1,339
    1,020
    Jan 1, 2016
    United States
    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, Oct 1, 2016
    Boogieboo6 likes this.
  7. Boogieboo6
    OP

    Boogieboo6 @realDonaldTrump

    Member
    945
    1,278
    Jul 30, 2015
    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.
    Final Product!
     
  8. grossaffe

    grossaffe GBAtemp Addict

    Member
    2,728
    2,098
    May 5, 2013
    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;
    }
    
     
    Scarlet likes this.
  9. 0x40

    0x40 GBAtemp Regular

    Member
    224
    63
    Apr 20, 2013
    You don't need the brackets if it's only one line. You can do it like this:

    Code:
    if (something)
        do_stuff();
    
     
  10. Boogieboo6
    OP

    Boogieboo6 @realDonaldTrump

    Member
    945
    1,278
    Jul 30, 2015
    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.
     
  11. grossaffe

    grossaffe GBAtemp Addict

    Member
    2,728
    2,098
    May 5, 2013
    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)
     
  12. Logan Pockrus

    Logan Pockrus Knawledge is key.

    Member
    1,339
    1,020
    Jan 1, 2016
    United States
    I bet he knows that, but uses brackets anyway. A lot of people do, primarily because they find it more readable.
    Probably preparation for macros, but I'm just guessing.
     
    Boogieboo6 likes this.
  13. 0x40

    0x40 GBAtemp Regular

    Member
    224
    63
    Apr 20, 2013
    Was your teacher also the one who told you to use system("pause"), by any chance?
     
  14. Boogieboo6
    OP

    Boogieboo6 @realDonaldTrump

    Member
    945
    1,278
    Jul 30, 2015
    United States
    Yep! I was taught that by the same teacher!
     
  15. 0x40

    0x40 GBAtemp Regular

    Member
    224
    63
    Apr 20, 2013
    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.
     
    Minnow likes this.
  16. Boogieboo6
    OP

    Boogieboo6 @realDonaldTrump

    Member
    945
    1,278
    Jul 30, 2015
    United States
    Why are system("pause") and commenting //end if terrible coding habits?
     
  17. grossaffe

    grossaffe GBAtemp Addict

    Member
    2,728
    2,098
    May 5, 2013
    United States
    Well that's... I guess this is supposed to be a "first foray into programming" class? I've got some reservations with this class

    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).
     
  18. Logan Pockrus

    Logan Pockrus Knawledge is key.

    Member
    1,339
    1,020
    Jan 1, 2016
    United States
    Note, though, that "system("pause")" only works on Windows. You'd need to use std::cin.get() to pause on Linux-based operating systems.
     
    Boogieboo6 likes this.
  19. Issac

    Issac I

    Global Moderator
    GBAtemp Patron
    Issac is a Patron of GBAtemp and is helping us stay independent!

    Our Patreon
    5,217
    2,311
    Apr 10, 2004
    Sweden
    Sweden
    I just want to make sure you realize why yours didn't work :)
     
  20. 0x40

    0x40 GBAtemp Regular

    Member
    224
    63
    Apr 20, 2013
    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.
     
    Minnow and Boogieboo6 like this.