Thread: Simple program I made - Question about code

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by msh
    I personally prefer std::cin.sync(), but that might have side-effects I'm not aware of
    Refer to Prelude's comments concerning std::istream::sync() in Simple C++ help for a beginer?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by laserlight View Post
    Refer to Prelude's comments concerning std::istream::sync() in Simple C++ help for a beginer?
    Thanks!
    Code:
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    So what that does is: "from input buffer, extract and discard the number of characters that correspond to the maximum possible value of type streamsize, e.g. the maximum number of characters that can be placed in the buffer, OR extract and discard all character up until and including the first '\n' that is encountered, whichever comes first". Did I get that about right?

    Elegant!

    Thanks, laserlight!

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    May I suggest using a do while loop?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int HottestGirl = 0;
    
    	cout << "Who is the hottest girl?\n";
    	cout << "1)Claudia Lynx\n";
    	cout << "2)Megan Fox\n";
    	cout << "3)Audrina Patridge\n";
    	cout << "\n";
    
    //------------------------------------------------------------------------------------------------------------------------// START   //
        do {                                                                                                                  // Do While -
            cin >> HottestGirl;                                                                                               // Input
            if (HottestGirl == 1) {                                                                                           // Set Case If
                cout << "\nClaudia Lynx is the hottest girl!\n\n";
            }
            else if (HottestGirl == 2) {                                                                                      // Set Case If
                cout << "\nMegan Fox is the hottest girl!\n\n";
            }
            else if (HottestGirl == 3) {                                                                                      // Set Case If
                cout << "\nAudrina Patridge is the hottest girl!\n\n";
            }
            else {                                                                                                            // If none above then do
                cout << "Invalid Selection\n\n";
            }
        } while (HottestGirl == 0);                                                                                           // Do the above while HottestGirl equals 0
    
    
    }

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    20
    Quote Originally Posted by Aliaks View Post
    May I suggest using a do while loop?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int HottestGirl = 0;
    
    	cout << "Who is the hottest girl?\n";
    	cout << "1)Claudia Lynx\n";
    	cout << "2)Megan Fox\n";
    	cout << "3)Audrina Patridge\n";
    	cout << "\n";
    
    //------------------------------------------------------------------------------------------------------------------------// START   //
        do {                                                                                                                  // Do While -
            cin >> HottestGirl;                                                                                               // Input
            if (HottestGirl == 1) {                                                                                           // Set Case If
                cout << "\nClaudia Lynx is the hottest girl!\n\n";
            }
            else if (HottestGirl == 2) {                                                                                      // Set Case If
                cout << "\nMegan Fox is the hottest girl!\n\n";
            }
            else if (HottestGirl == 3) {                                                                                      // Set Case If
                cout << "\nAudrina Patridge is the hottest girl!\n\n";
            }
            else {                                                                                                            // If none above then do
                cout << "Invalid Selection\n\n";
            }
        } while (HottestGirl == 0);                                                                                           // Do the above while HottestGirl equals 0
    
    
    }
    hmm... I understand that except for the last part where it says:
    Code:
        } while (HottestGirl == 0);
    What is this there for?
    Also, one thing this doesn't do is display "Selection: " before the user inputting the selection. Is there a way to fix this or no? It's not that it's so important, the program means nothing, I am just trying to learn
    Last edited by PersianStyle; 07-13-2009 at 01:14 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  2. Random Question Assign Program
    By mikeprogram in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2005, 10:04 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM
  5. Have You Got A program To Match Question.
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 06-01-2002, 03:50 PM