Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

timo86m Group Title

In c++ lets say i have a variable char lets call it char variable; and i want to test it if it is any of these 'a', 'b', or 'c' after the user inputs a char will the below snip of code work as a shorcut if(variable== 'a'|| 'b' || 'c') cout << "your variable is one of those"; or do i have to do it the long way like below if(variable== 'a'|| variable =='b' || variable== 'c') cout << "your variable is one of those";

  • 9 months ago
  • 9 months ago

  • This Question is Open
  1. timo86m Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Or lets say i made this function that takes char c and returns true if it is a number 0-9. Do I absolutely have to write it like that? all of it on the if statement? bool boolisnum( char c) { if (c=='0'||c=='1'||c=='2'||c=='3'||c=='4'||c=='5'||c=='6'||c=='7'||c=='8'||c=='9') return true; else return false; }; can if (c=='0'||c=='1'||c=='2'||c=='3'||c=='4'||c=='5'||c=='6'||c=='7'||c=='8'||c=='9') be reduced to if (c=='0'|| '1'||'2'||'3'||'4'||'5'||'6'||'7'||'8'||'9')

    • 9 months ago
  2. hba Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Use switch case.

    • 9 months ago
  3. bibby Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    char c = 'a'; if (c=='0'|| '1'||'2'||'3'||'4'||'5'||'6'||'7'||'8'||'9') cout << "test"; that code segment still outputs "test" no matter what value c is. I think the reasoning behind this is that you can think of characters as integers. if(CONSTANT) executes the conditional as long as the number doesn't evaluate to 0 (false). if('6') is the same thing as saying if(54). Look at the ascii table: http://www.asciitable.com/ bool b = true; if(1) and if(54) and if(-1) and if(b) all execute the conditional. Use a switch or the conditional operator. Either way, write it out.

    • 9 months ago
  4. timo86m Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    I know for sure works. bool boolisnum( char c) { if (c=='0'||c=='1'||c=='2'||c=='3'||c=='4'||c=='5'||c=='6'||c=='7'||c=='8'||c=='9') return true; else return false; }; if i where to type in main boolisnum('4'); it will evaluate to true Note that idk what boolisnum(4); would do. It probably would not compile since 4 is of type int not char. I am just looking to make my code shorter by not putting all those c==' 's. oh and if boolisnum('b'); it evaluates as false.

    • 9 months ago
  5. bibby Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    #include <cctype> isdigit() http://www.cplusplus.com/reference/cctype/isdigit/

    • 9 months ago
  6. bibby Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    or, switch(c){ case '1': case '2': ... case '0': etc. break; }

    • 9 months ago
  7. timo86m Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    switch makes it longer. The whole point is to make my code shorter. Oh and os is messing up.

    • 9 months ago
  8. bibby Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    I literally just gave you a single function. It doesn't get shorter than that

    • 9 months ago
  9. bibby Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    bool boolisnum( char c){ if (isdigit(c)) return true; return false; }

    • 9 months ago
  10. timo86m Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    yes thank you that does work now i wanna know is if (c=='0'|| '1'||'2'||'3'||'4'||'5'||'6'||'7'||'8'||'9') same as if (c=='0'||c=='1'||c=='2'||c=='3'||c=='4'||c=='5'||c=='6'||c=='7'||c=='8'||c=='9')

    • 9 months ago
  11. bibby Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    I explained it in my first post. It isn't the same thing

    • 9 months ago
  12. bibby Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    It'll always evaluate to true no matter what

    • 9 months ago
  13. timo86m Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    darn so i do gotta use c=='0'||c=='1'||c=='2'||c=='3'||c=='4'||c=='5'||c=='6'||c=='7'||c=='8'||c=='9'

    • 9 months ago
  14. bibby Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    You don't gotta use anything when there's a function written for you. If you refuse to use perfectly find functions, yeah, you have to do that...

    • 9 months ago
  15. timo86m Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    I am just saying as an example. I know there is one that checks for if its upper of lower case. BUt There wont be one for every single one of my needs :).

    • 9 months ago
  16. bibby Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    lol the STL is pretty comprehensive check http://www.cplusplus.com/reference/cctype/ for more. Read about how chars/ascii work in relation to integers. remember if(some constant that isn't 0) always evaluates to true

    • 9 months ago
  17. timo86m Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    it is a huge library. And sometimes it be quicker making my function instead of looking for it :P

    • 9 months ago
  18. timo86m Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    and it's a great learning experience :D

    • 9 months ago
  19. TuringTest Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    to reiterate why if (c=='0'|| '1'||'2'||'3'||'4'||'5'||'6'||'7'||'8'||'9') will always evaluate to "true", is because c++ interprets zero as false, and all non-zero values as true. No char has an ascii value of zero, as binary zero in ascii is the NUL value. For instance if (false || 1) will always evaluate to "true", since the second statement, 1, counts as true. equally it could be any non-zero number. if (false || -534.7) will also evaluate to "true" As far as the compiler is concerned, this is the same as if (false || true) only something like if (false || 0) will evaluate to "false", since zero counts as false. Since the char '0' has an ascii value of 48 in decimal, the statement if (false || '0') is equivalent to if (false || 48) which, as I explained above, is equivalent to if (false || true) which will, of course, evaluate to "true"

    • 9 months ago
  20. timo86m Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    'a'=='d' will evaluate as false 'a'=='a' will evaluate as true i know that much.

    • 9 months ago
  21. bibby Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    there's no equivalence being done here. The characters are getting interpreted by their INTEGER ascii values. if('a') is the same thing as if(65) or whatever

    • 9 months ago
  22. timo86m Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    idk what it does but those statements will work i have compiled them and it does work.

    • 9 months ago
  23. bibby Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    use full sentences because I don't understand you. if(c=='0'||'1'...) will not work...

    • 9 months ago
  24. wio Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Okay, ASCII was ordered the way it was for a reason. ``` # is digit if '0' <= variable <= '9': # is a,b, or c if 'a' <= variable <= 'c': ``` Look at the chart.

    • 9 months ago
  25. wio Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    I suppose since this is C++ you should be using `if (` ... `) {` ... `}` instead, but the point still stands.

    • 9 months ago
  26. seandisanti Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Why reinvent the wheel? Regex is a great way to check input against a pattern in any language. http://www.cplusplus.com/reference/regex/regex_search/

    • 9 months ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.