Noob programming problem

Discussion in 'OT Technology' started by zxghostrider, Sep 13, 2008.

  1. zxghostrider

    zxghostrider Sometimes you gotta hop on two wheels

    Joined:
    Jul 29, 2007
    Messages:
    3,427
    Likes Received:
    0
    Location:
    In your mind
    Tried writing a program for the first time in C++ using a switch. I can't get it to work, and tried doing a switch within a switch. Any help?

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
    	
    	int Yes;
    	int No;
    	
    
    	
    	Yes = 1;
    	No = 2;
    		
    	
    	
    	
    
    	
    	string i;
    	
    	
    	std::cout << "How was your day?\n" ;
    	std::cin >> i;
    	std::cout << "Sounds good\n" ;
    	std::cout << "Want to play a game? " ;
    	std::cout << "Yes or No?\n" ;
    	std::cin >> (Yes or No)
    	
    	
    	
    	
    	switch (Yes or No)  
    	{ 
    		
    			
    		
    			case 1 : 
    						if(Yes) ;
    						std::cout << "Let's get started!\n" ;
    						std::cout << "Are there 50 States?\n" ;
    						std:cin >> Yes or No ;
    			
    			
    							switch (Yes or No)  // wasn't sure if I could do a switch within a switch.
    							{
    				
    							if(Yes) ;
    							std::cout << "You're correct\n" ;
    							break;
    				
    				
    							if(No) ;
    							std:cout << "you mad dumb son\n" ;
    							break;
    							
    							}
    				
    								
    																	
    							
    								
    						
    			case 2 :
    						if(No)
    						cout << "You are such a pussy\n" ;
    						break;
    			
    				
    								
    	
    			
      }
    			
    	
    	
    	
    	return 0;
    }
    	
     
  2. Boots

    Boots huuuuuuuuge vagina

    Joined:
    Jul 2, 2001
    Messages:
    8,014
    Likes Received:
    75
    Location:
    Canada
  3. White Stormy

    White Stormy Take that, subspace!

    Joined:
    Sep 17, 2002
    Messages:
    85,489
    Likes Received:
    70
    Location:
    Sparkopolis
    that's not how a switch works
     
  4. White Stormy

    White Stormy Take that, subspace!

    Joined:
    Sep 17, 2002
    Messages:
    85,489
    Likes Received:
    70
    Location:
    Sparkopolis
    I didn't try compiling this, so it might not work. Also, I haven't written C in like 6 months :o

    but it's a lot better than what you had, and I tried to comment on everything :o

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main ()
    {
    	/* declare and initialize int variables Yes and No */
    	int Yes = 1, No = 2;
    
    	/* declare string variable strInput and char variable charInput */
    	string strInput;
    	char charInput;
    
    	/* Initiate communication with user */
    	cout << "How was your day?\n";
    	/* read user input to strInput */
    	cin >> strInput;
    	cout << "Sounds good\n";
    	cout << "Want to play a game? ";
    
    	/* prompt user for response of Y or N continuously until one or the other is received */
    	do
    	{
    		cout << "Y or N?\n";
    		/* read user input to charInput */
    		cin >> charInput;
    	} while ((charInput != 'Y') && (charInput != 'N'));
    
    	/* determine which code to execute now based on which letter they answered with */
    	switch (charInput)  
    	{
    		/* if charInput == 'Y' */
    		case 'Y': 
    			cout << "Let's get started!\n";
    			cout << "Are there 50 States?\n";
    
    			/* prompt user for response of Y or N continuously until one or the other is received */
    			do
    			{
    				cout << "Y or N?\n";
    				/* read user input to charInput */
    				cin >> charInput;
    			} while ((charInput != 'Y') && (charInput != 'N'));
    
    			/* determine which code to execute now based on which letter they answered with */
    			switch (charInput)
    			{
    				/* if charInput == 'Y' */
    				case 'Y':
    					cout << "You're correct\n";
    					/* end statement list for current switch case */
    					break;
    
    				/* if charInput == 'N' */
    				case 'N':
    					cout << "you mad dumb son\n";
    					/* end statement list for current switch case */
    					break;
    			}
    
    			/* end statement list for current switch case */
    			break;
    
    		/* if charInput == 'N' */
    		case 'N':
    			cout << "You are such a pussy\n";
    			/* end statement list for current switch case */
    			break;
    	}
    
    	return 0;
    }
    
     
  5. White Stormy

    White Stormy Take that, subspace!

    Joined:
    Sep 17, 2002
    Messages:
    85,489
    Likes Received:
    70
    Location:
    Sparkopolis
    and seriously, dude, at least glance at a C++ book
     
  6. CodeX

    CodeX Guest

    Do something like this instead, this gives an example of how to get input properly and how to use an if/else conditional block and a switch:

    Code:
    
    int response;
    
    cout << "Are you a retard? (1=Yes, 2=No)" << endl;
    cin >> response;
    
    if(response != 1 && response != 2)
    {
       cout << endl << "You really are a retard, you can't even follow directions" << endl;
    }
    
    else
    {
       switch(response)
       {
          case 1:
             cout << endl << "Correct, you are a retard" << endl;
             break;
          case 2:
             cout << endl << "Are you sure? You had better check again" << endl;
       }
    }
    
    
    
    You will notice I ommited the break from the second case. You can do this if you don't plan on providing a default case to the conditional, which I could have done also but logically it would have never gotten to that point, since I test for validity of the response (1 or 2) and in any other case the if condition is executed and not the else block, which includes the switch.
     
  7. zxghostrider

    zxghostrider Sometimes you gotta hop on two wheels

    Joined:
    Jul 29, 2007
    Messages:
    3,427
    Likes Received:
    0
    Location:
    In your mind
    I have been, but when I try to do it on my own custom prog, I get a little mixed up. But your code was perfect, thank you. :wavey:
     

Share This Page