Switch vs. if..else

i was asked to change the switch statments to if..else

Page 1 of 1

5 Replies - 1605 Views - Last Post: 06 October 2010 - 05:06 PM Rate Topic: -----

#1 yadira_hinojosa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 11-January 10

Switch vs. if..else

Posted 06 October 2010 - 04:49 PM


#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cmath> 
#include <cstdlib>
#include <iomanip>

using namespace std; 

/*************************************************************************************
 switch vs. if/else

 Compile and run this code.  Read through it and make sure that you understand
 why it behaves the way it does.  

 Once you've done that, modify the main function to use an if/else tree instead
 of a switch. (hint: you can't just change the cases to conditions, you have to
 pay attention to the break statements as well)

*************************************************************************************/

// function to show simple use of the random number generator library
void show_random()
{
    int seed, test;

    // get a number from the user
    cout << "Give me a random seed (int) => "; 
    cin >> seed;
    
    // use that number to "seed" the random generator
    srand( seed );
    // now you can get a random number with the function rand()
    test = rand();
    
    if (test%100 >= 70)
        cout<<" With 30% of chance, cat hits rat with a bat" <<endl; 
    else 
        cout<<" With 70 % of chance, rat catches cat with a hat" <<endl; 
}

int main( )
{
    // variable declarations
    char choice1, choice;                    // choice1 for switch and choice for replay//changed by sirisha

    // a loop to play forever until the user chooses to stop
    while  (true)
    {
        // prompt the user to enter a choice
        cout << "What is your choice? ('a' to 'd', or 'A' to 'D') =>" << endl; 
        cin >> choice1;

        // switch to deal with four cases
        switch (choice1)                    //changed by sirisha
        {
        case 'a':    
        case 'A':
            cout << "I see a rat " << endl;    //no break here
        case 'b':
        case 'B':    
            cout << "I have a cat " << endl;
            break;
        case 'c':
        case 'C':  
            cout << "Cat hates rat " << endl; 
            cout << "Rate chases cat " << endl; 
            break;
        case 'd':
        case 'D':
            show_random();
            break; 

        default: 
            cout << choice1 << " is not a valid choice" << endl; 
        }

        // ask the user if they want to repeat
        cout << "Want to play again? (y/n) => "; 
        cin >> choice;

        // check if the choice is not 'y' or 'Y'
        if (choice != 'y' && choice != 'Y')
        {
            // stop if the choice is not 'y' or 'Y'
            cout << "Bye-bye now ........ " << endl; 
            // this break statement exits the while loop
            break;                    
        }

        // otherwise, we loop back up to the top
        cout << "Okay, one more time >>>>>>>>>>>>>>>> " << endl; 
    }

    return 0; 
}



Mine...

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cmath> 
#include <cstdlib>
#include <iomanip>

using namespace std; 

/*************************************************************************************
 switch vs. if/else

 Compile and run this code.  Read through it and make sure that you understand
 why it behaves the way it does.  

 Once you've done that, modify the main function to use an if/else tree instead
 of a switch. (hint: you can't just change the cases to conditions, you have to
 pay attention to the break statements as well)

*************************************************************************************/

// function to show simple use of the random number generator library
void show_random()
{
    int seed, test;

    // get a number from the user
    cout << "Give me a random seed (int) => "; 
    cin >> seed;
    
    // use that number to "seed" the random generator
    srand( seed );
    // now you can get a random number with the function rand()
    test = rand();
    
    if (test%100 >= 70)
        cout<<" With 30% of chance, cat hits rat with a bat" <<endl; 
    else 
        cout<<" With 70 % of chance, rat catches cat with a hat" <<endl; 
}

int main( )
{
    // variable declarations
    char choice1, choice;                    // choice1 for switch and choice for replay//changed by sirisha

    // a loop to play forever until the user chooses to stop
    while  (true)
    {
        // prompt the user to enter a choice
        cout << "What is your choice? ('a' to 'd', or 'A' to 'D') =>" << endl; 
        cin >> choice1;

        // switch to deal with four cases
        //switch (choice1)                    //changed by sirisha
        if (choice1 == 'a' && choice1 == 'A')
    
        {cout << "I see a rat " << endl;}    //no break here
        
        else if(choice1 == 'b' && choice1 == 'B')
    
        {cout << "I have a cat " << endl;}
        
        else if(choice1 == 'c' && choice1 == 'C')
         
            {cout << "Cat hates rat " << endl; 
        cout << "Rat chases cat,cat is scared of rat " << endl; }
            
        else if(choice1 == 'd' && choice1 == 'D')
        
        {show_random();}
          

        //else 
             //{cout << choice1 << " is not a valid choice" << endl;} 
        

        // ask the user if they want to repeat
        cout << "Want to play again? (y/n) => "; 
        cin >> choice;

        // check if the choice is not 'y' or 'Y'
        if (choice != 'y' && choice != 'Y')
        {
            // stop if the choice is not 'y' or 'Y'
            cout << "Bye-bye now ........ " << endl; 
            // this break statement exits the while loop
            break;                    
        }

        // otherwise, we loop back up to the top
        cout << "Okay, one more time >>>>>>>>>>>>>>>> " << endl; 
    }

    return 0; 
}




when i run it if i input any letter a-d or A-D i get '' is not a valid choice.
what am i doing wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: Switch vs. if..else

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Switch vs. if..else

Posted 06 October 2010 - 04:59 PM

if (choice1 == 'a' && choice1 == 'A')
Translate this into English for me.
Was This Post Helpful? 1
  • +
  • -

#3 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Switch vs. if..else

Posted 06 October 2010 - 04:59 PM

if (choice1 == 'a' && choice1 == 'A')


How can choice1 both equal lowercase a, and uppercase A? Should you not be using an OR, and not AND?

if (choice1 == 'a' || choice1 == 'A')

Was This Post Helpful? 1
  • +
  • -

#4 aaa111  Icon User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Re: Switch vs. if..else

Posted 06 October 2010 - 05:00 PM

Look at this statement:
if(choice1 == 'a' && choice1 == 'A')

Even if you enter 'a' it still is false because (choice1=='A') is false.What you need to do is this:
choice1 == 'a' || choice1 == 'A'

If either one of the condition is true then the whole statement become true.
OMG beaten.

This post has been edited by aaa111: 06 October 2010 - 05:05 PM

Was This Post Helpful? 1
  • +
  • -

#5 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Switch vs. if..else

Posted 06 October 2010 - 05:02 PM

if (choice1 == 'a' && choice1 == 'A')


choice1 cannot be both values at the same time. You need to "OR" the possible values not "AND" them:

if (choice1 == 'a' || choice1 == 'A')

Was This Post Helpful? 2
  • +
  • -

#6 yadira_hinojosa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 11-January 10

Re: Switch vs. if..else

Posted 06 October 2010 - 05:06 PM

Thank you so much. i totally forgot about || :sweatdrop:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1