Thread: Is a while loop needed here?

  1. #1
    Registered User
    Join Date
    Jun 2015
    Posts
    3

    Question Is a while loop needed here?

    Hey guys, I just started on the road to learning C++. I am working through the book Jumping into C++, but am stuck on a problem. In chapter 4, where if-statements are introduced, there is a question which asks:
    Expand the password checking program from earlier in this chapter and make it take multiple usernames, each with their own password, and ensure that the right username is used for the right password. Provide the ability to prompt users again if the first login attempt failed. Think about how easy (or hard) it is to do this for a lot of usernames and passwords.


    The password checking program given is:
    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    int main ()
    {
        string username;
        string password;
        cout << "Enter your username: " << "\n";
        getline( cin, username, '\n' );
    
    
        cout << "Enter your password: " << "\n";
        getline( cin, password, '\n' );
        if ( username == "root" && password == "xyzzy" )
        {
            cout << "Access allowed" << "\n";
        }
        else
        {
            cout << "Bad username or password. Denied access!" << "\n";
            return 0;
        }
    }
    I understand how the given program works, and understand how I can make it check against other usernames by adding else-if statements. However, I do not understand how to make it prompt users again. I have a little bit of programming experience with Python, and it seems to me that I should run the entire program inside of a while-loop. However, the only concepts that have been covered thus far are variable types and assignment, if/else-if/else statements, arithmetic operations and boolean logic. Loops actually begin in the next chapter. Is there any way to prompt users again without using loops and using only those concepts that I listed? I realize that someone has asked this question here a few years ago, but all of the answers suggested using loops, and I was wondering if anyone knows how to do it with just the material presented thus far. Thanks!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Loops would probably be the best way to proceed, without loops you'll be repeating the code over and over. It may be that the author is trying to show you the reason loop constructs are useful, laying the foundation for the next chapter.

    By the way you may want to find another book, that book hasn't had the greatest of reviews.

    Jim

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    3
    That makes sense. Introduce the effectiveness of loops by showing you how meticulous it would be to repeat code for every attempt, haha! Thanks! So far I like the way the author presents content, almost in a conversational manner, but I am open to any suggestions that you think would help a lot. What book would you recommend? My programming experience is limited to a semester of introductory programming through Python.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    There is a book list pinned at the top of the forum you may want to look through.

    Jim

  5. #5
    Registered User
    Join Date
    Nov 2013
    Posts
    107
    Quote Originally Posted by jimblumberg View Post
    Loops would probably be the best way to proceed, without loops you'll be repeating the code over and over. It may be that the author is trying to show you the reason loop constructs are useful, laying the foundation for the next chapter.

    By the way you may want to find another book, that book hasn't had the greatest of reviews.

    Jim
    Where are the bad reviews?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Elysia's non-review review, probably. Plus we have had a number of people here whose learning style would benefit better from Stroustrup's recommended approach of going from high level to low level than from the book's more conventional approach.
    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

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Elysia's non-review review, probably.
    From the small sections of the book that I have read I believe the book can be confusing to new users. For example dealing with the end of chapter review questions for the chapter in question:


    4. Expand the password checking program from earlier in this chapter and make it take multiple usernames, each with their own password, and ensure that the right username is used for the right password.
    Provide the ability to prompt users again if the first login attempt failed.
    Think about how easy (or hard) it is to do this for a lot of usernames and passwords.
    5. Think about what kind of language constructs or features would make it easier to
    add new users without recompiling the password program. (Note: don't feel like you need to solve these
    problems with the C++ you've learned so far, the goal is to think about how you might use tools
    we'll pick up in future chapters.)
    Notice the part I bolded. A new user who is using this book for their first introduction to C++ probably won't know about any of those tools from future chapters, they haven't yet read the book!

    Also looking at Part 1 Chapter 1, which is provided by the author as a sample, I find the fact that the author describes setting up an IDE for both Windows and OSX but uses the command line for Linux instead of an IDE distressing. IMO most beginner programmers using Linux today are using the GUI the not command line for most tasks so, again IMO, setting up something similar to the other operating systems would be more appropriate. And since the author chose Code::Blocks for Windows, Code::Blocks for Linux would have been a better choice, again IMO.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D array loop...Help needed
    By ReneeJA in forum C++ Programming
    Replies: 2
    Last Post: 03-04-2014, 02:27 PM
  2. c# help needed with a mix of loop terminated sentinal
    By xbox221 in forum C# Programming
    Replies: 6
    Last Post: 01-10-2011, 09:23 PM
  3. decrementing for loop help needed
    By adamuk in forum C Programming
    Replies: 5
    Last Post: 01-17-2006, 02:29 PM
  4. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  5. help needed on loop ??
    By bezzler in forum C Programming
    Replies: 3
    Last Post: 07-08-2005, 07:27 AM

Tags for this Thread