A C++ Help System for loops

This is a discussion on A C++ Help System for loops within the C++ Programming forums, part of the General Programming Boards category; I wrote this a few days ago, and just made an account on here. Tell me what you guys think ...

  1. #1
    Registered User sloppyjoe69's Avatar
    Join Date
    May 2011
    Location
    florida
    Posts
    1

    A C++ Help System for loops

    I wrote this a few days ago, and just made an account on here.

    Tell me what you guys think :]
    And if theres need for improvement, let me know.

    --------------------->

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	char choice;
    	
    	for(;;) {
    		do {
    			cout << "Help on:\n";
    			cout << " 1. if\n";
    			cout << " 2. switch\n";
    			cout << " 3. for\n";
    			cout << " 4. while\n";
    			cout << " 5. do-wjile\n";
    			cout << " 6. break\n";
    			cout << " 7. continue\n";
    			cout << " 8. goto\n";
    			cout << " Choose one (q to quit): ";
    
    			cin >> choice;
    			cout << "\n";
    
    			if(choice < '1' || choice > '8' && choice != 'q')
    				cout << "Incorrect selection, please try again.\n\n";
    			
    		} while(choice < '1' || choice > '8' && choice != 'q'); 
    		
    
    		if(choice == 'q') break;
    
    		cout << "\n\n";
    
    
    		switch(choice)
    		{
    			case '1':
    				cout << "The if:\n\n";
    				cout << "if(condition) statement;\n";
    				cout << "else statement;\n";
    				cout << "\n";
    				break;
    			case '2':
    				cout << "The switch:\n\n";
    				cout << "switch(expression) {\n";
    				cout << "	case constant: \n";
    				cout <<	"		statement sequence\n";
    				cout << "		break;\n";
    				cout << "}\n";
    				cout << "\n";
    				break;
    			case '3':
    				cout << "The for:\n\n";
    				cout << "for(init; condition; increment)";
    				cout << "	statement;\n";
    				cout << "\n";
    				break;
    			case '4':
    				cout << "The while:\n\n";
    				cout << "while(condition) statement;\n";
    				cout << "\n";
    				break;
    			case '5':
    				cout << "The do-while:\n\n";
    				cout << "do {\n";
    				cout << "	statement;\n";
    				cout << "} while(condition);\n";
    				cout << "\n";
    				break;
    			case '6':
    				cout << "The breake:\n\n";
    				cout << "break;\n";
    				cout << "\n";
    				break;
    			case '7':
    				cout << "The continue:\n\n";
    				cout << "continue;\n";
    				cout << "\n";
    				break;
    			case '8':
    				cout << "The got:\n\n";
    				cout << "goto label;\n";
    				cout << "\n";
    				break;
    		}
    		cout << "\n";
    	}
    
    	return 0;
    
    }
    Last edited by sloppyjoe69; 05-06-2011 at 01:52 PM.

  2. #2
    Registered User
    Join Date
    May 2011
    Posts
    19
    very good !

  3. #3
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    eh..how does "very good !" improve the code..
    You ended that sentence with a preposition...Bastard!

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    This looks suspicious:
    Code:
    if(choice < '1' || choice > '8' && choice != 'q')
    If I remember correctly, && has higher precedence than ||, so that might not do what you want. (Try entering '9', it might break.) Even if it's correct it can be a good idea to parenthesize expressions where both && and || are involved (gcc even gives you a warning if you don't, if you want that particular warning enabled).

    Also the if's condition is repeated in the while later on. I'd be inclined to make the loop infinite and use a break statement (as you've done with the outer for(;;) loop). Just a matter of style, some people dislike break/continue, but I prefer it to duplicated code.

    Instead of lots of cout statements you can always write
    Code:
    std::cout << "hello there\n"
        << "again\n"
        << "and bye.\n";
    or even
    Code:
    std::cout << "hello there\n"
        "again\n"
        "and bye.\n";
    (The preprocessor concatenates string constants that are separated only by whitespace.)

    Finally: the secret about switch statements? Arrays are often more convenient. For example:
    Code:
    const char *message[] = {
        // '1'
        "Comment for '1'\n"
        "some text\n"
        "and some more text.\n",
    
        // '2'
        "Comment for '2'\n"
        // ...
    };
    
    if(choice >= '1' && choice <= '8') {
        std::cout << message[choice - '1'];
    }
    Note that '0', '1', ... are guaranteed to be contiguous so there's no problem with this.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loops, menu loops
    By gloworm in forum C Programming
    Replies: 17
    Last Post: 04-12-2010, 07:59 PM
  2. 2 more for loops
    By newbie101 in forum C++ Programming
    Replies: 6
    Last Post: 05-04-2006, 11:46 AM
  3. for loops
    By newbie101 in forum C++ Programming
    Replies: 10
    Last Post: 05-04-2006, 11:38 AM
  4. Loops
    By memyselfi in forum C++ Programming
    Replies: 2
    Last Post: 12-02-2005, 01:32 PM
  5. Cannot convert system object to system string?
    By Robert_Sitter in forum Windows Programming
    Replies: 0
    Last Post: 11-18-2005, 01:44 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21