Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

If/else practice

Got an idea for a practice project / debug snippet / short-code contest / etc? Tell us about it here!

If/else practice

Postby Strazku on Tue Aug 26, 2008 8:37 pm

Code: Select all
#include <iostream>

int main()

{
   
   int theNumber = 76;
   int playersGuess;

   std::cout << "lets play guess my number! \n";
   std::cout << "type in a number from 0-100: ";

   std::cin >> playersGuess;

   if(playersGuess == theNumber)
   {
      std::cout << "They match";
   }
   else
   {
      if(playersGuess > theNumber)
      {
         std::cout << "lower \n";
      }
      else
      {
         if(playersGuess < theNumber)
         {
            std::cout << "Higher! \n";
         }

      
      }
   }


   std::cout << "You're close!  Try again! \n";
   std::cout << "Pick again: ";
   std::cin >> playersGuess;

   if(playersGuess == theNumber)
   {
      std::cout << "Great job, you got it! \n";
   }
   else
   {
      if (playersGuess > theNumber)
      {
         std::cout << "To high!, Go lower! \n";
      }
      else
         if(playersGuess < theNumber)
         {
            std::cout << "To low :(, go higher! \n";
         }
   }


   
   std::cout << "Ok, one last try!  try between 70 and 80 \n";
   std::cin >> playersGuess;

   if(playersGuess == theNumber)
   {
      std::cout << "You win!! Great job! \n";
   }
   else
      std::cout << "You lose! Maybe next time!";
   

   
   


   

   char f;
   std::cin >> f;
   return 0;
}
Strazku
 
Posts: 16
Joined: Sat Aug 23, 2008 11:21 pm

Re: If/else practice

Postby Doctor Salt on Mon Sep 01, 2008 10:15 pm

Am I the only one who likes switch statements a lot more than If else? It seems the only thing switch statements can't do, that if else can, is comparing strings, but even some code work arounds are available.
I'm a 15 year old kid who has his heart set on Programming, almost as much as Marss++.......

Came here hoping to ask more questions so if you have any free time just say!
Doctor Salt
 
Posts: 147
Joined: Wed Jul 30, 2008 3:32 pm

Re: If/else practice

Postby antiRTFM on Mon Sep 01, 2008 11:37 pm

yea i like switch over if/else too, though be careful because there are some dirty secrets behind the switch statement which you may bump into one day... like creating objects inside cases requires braces, or that once a case-match has been encountered the rest of the cases are not even tested anymore and are executed regardless, which is why you must have 'break'... etc

if/else can be more readable sometimes.
User avatar
antiRTFM
Administrator
 
Posts: 449
Joined: Sun Apr 13, 2008 9:10 am

Re: If/else practice

Postby asib on Tue Sep 02, 2008 2:57 pm

Also, you can't have multiple conditions like :
Code: Select all
if ( ( x == y ) && ( ! ( x > z ) ) )

And I remember you talking about the curly brace problem, and how it deletes the object when it goes out of scope in a switch. Just mean :P
asib
 
Posts: 86
Joined: Tue Jul 22, 2008 9:15 pm

Re: If/else practice

Postby Doctor Salt on Tue Sep 02, 2008 5:33 pm

Hey, i was wondering, could you have a switch statement like, cause that would be nice.

Code: Select all
switch(true)
{
case (( x == y ) && ( ! ( x > z ) ) ):
{
do something();
}
break;

case  ( ! x > y ) || ( x < z ) ):
{
dosomething
else();
break;
}

}
I'm a 15 year old kid who has his heart set on Programming, almost as much as Marss++.......

Came here hoping to ask more questions so if you have any free time just say!
Doctor Salt
 
Posts: 147
Joined: Wed Jul 30, 2008 3:32 pm

Re: If/else practice

Postby antiRTFM on Tue Sep 02, 2008 8:19 pm

Doctor salt: nope you cant, like asib said
User avatar
antiRTFM
Administrator
 
Posts: 449
Joined: Sun Apr 13, 2008 9:10 am

Re: If/else practice

Postby Doctor Salt on Tue Sep 02, 2008 8:33 pm

Ah, I was pretty sure thats what he meant, but wasn't totally sure. Thanks!
I'm a 15 year old kid who has his heart set on Programming, almost as much as Marss++.......

Came here hoping to ask more questions so if you have any free time just say!
Doctor Salt
 
Posts: 147
Joined: Wed Jul 30, 2008 3:32 pm

Re: If/else practice

Postby DKeller4113 on Tue Jun 09, 2009 2:45 am

so you know, at the top, right under your includes, you can add

Code: Select all
using namespace std;



to save you from having to type std:: each time you use something from iostream.
DKeller4113
 
Posts: 4
Joined: Tue Jun 09, 2009 2:11 am

Re: If/else practice

Postby C++ on Tue Jun 09, 2009 9:10 am

DKeller4113 wrote:so you know, at the top, right under your includes, you can add

Code: Select all
using namespace std;



to save you from having to type std:: each time you use something from iostream.


Just, so you know
New to C++ programming? Click here
C++
teh awesome
 
Posts: 217
Joined: Sun May 25, 2008 7:45 am

Re: If/else practice

Postby OtTa on Mon Jun 15, 2009 4:37 pm

Strazku wrote:
Code: Select all
#include <iostream>
         if(playersGuess < theNumber)
         {
            std::cout << "Higher! \n";
         }


hmm... I don't understand, why do you check this condition?! after we know that "playersGuess == theNumber" and "playersGuess > theNumber" are true, after "else" condition "playersGuess < theNumber" is meant itself : )
OtTa
 
Posts: 2
Joined: Mon Jun 15, 2009 2:07 pm

Re: If/else practice

Postby antiRTFM on Mon Jun 15, 2009 5:24 pm

True that. but, there is still some reason to do that final test;

- For code clarity. You can never be too obvious about what your intentions are. If you can be any more obvious, do so.
- For functionality. Maybe at first the variable was > theNumber, but then later it became not-less-than theNumber! How? who knows how... you'd be surprised. Maybe in a big multithreaded program some other thread changed the variable, or maybe someone who's using your code decided to edit it a bit to add some cool stuff and meanwhile caused this to happen. With overly obvious code, chances of having these unintended bugs sprouting around is very small. With shortened code (like typing "else cout << "higher" " thinking 'duh, its obviously less-than') the chances for bugs are greater...

For the same reason i prefer to put evey possible expected case in a switch statement, and leave the default case for something really unexpected. like;

Code: Select all
// Example 1: wrong

switch(userChoice)
{
case CHOICE_1:
    CallThisFunction();
    break;

case CHOICE_2:
    CallThatFunction();
    break;

default:
    // bah, the user probably made a bad choice
    cout << "Please choose from the available choices"
    break;
}


Code: Select all
// Example 2: Correct

switch(userChoice)
{
case CHOICE_1:
    CallThisFunction();
    break;

case CHOICE_2:
    CallThatFunction();
    break;

case BAD_CHOICE:
    // yep, i expected the user might choose something wrong
    cout << "Please choose from the available choices";
    break;

default:
    cout << "Something is terribly wrong..."
    break;
}


You're probably thinking "That defualt case in example 2 will never be reached"
Well, You'd be surprised... very surprised

Example 2 in a way is much more bug-proof than example 1

Just my personal opinion though
User avatar
antiRTFM
Administrator
 
Posts: 449
Joined: Sun Apr 13, 2008 9:10 am

Re: If/else practice

Postby OtTa on Tue Jun 16, 2009 2:50 pm

antiRTFM

Thank you for such comprehensive description. Now I understand. Such prudence is good habit for bug-free programming!
OtTa
 
Posts: 2
Joined: Mon Jun 15, 2009 2:07 pm


Return to -- Code Practice Submissions --

Who is online

Users browsing this forum: No registered users and 0 guests