Thread: Chapter 5 Practice Problem #3

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    2

    Chapter 5 Practice Problem #3

    Hi, I'm having problems with this practice problem and I need a little help.

    The problem is:

    "Modify your password program from before to put all of the password checking logic into a seperate function, apart from the rest of the program."

    Here is my code:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    void password()
    {
        string checkpassword;
         if (checkpassword == "taylor")
        {
            cout << "Access Allowed";
        }
        else
        {
            cout << "Wrong password";
        }
    }
    
    int main()
    {
        cout << "Enter password: ";
        cin >> password();
    }
    So far, I have gone over Variables, If/Else statements, Loops, and this chapter's topic of functions so my knowledge is a bit limited. All help will be appreciated.

  2. #2
    Registered User
    Join Date
    Jan 2015
    Posts
    2
    I'm sorry it's from chapter 6.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Input tends to direct data into variables. Variables then get passed to functions.

    Input does not go directly into a function. But that is what your code implies.

    If you don't understand what I just said, you need to go over material you've completed already, with a view to learning it properly. You're skipping over things, and resulting to guesswork. Your guesses are flawed.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Separate data and instructions. Instructions take data as input and transforms it into some other data as output. Data is information we're processing. Password is data. Functions are instructions. Functions process data. Information cannot store data. Information can only process data. Therefore, this code is flawed because a function cannot store data.

    Store data into variables.
    Pass those variables into functions to process data. That's how it's done.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists Chapter 15 Practice Problem 1 Jumping Into C++
    By ArtemisFowl2nd in forum C++ Programming
    Replies: 34
    Last Post: 04-30-2014, 12:59 PM
  2. Jumping Into C++ Chapter 14 Practice Problem 1
    By ArtemisFowl2nd in forum C++ Programming
    Replies: 5
    Last Post: 04-16-2014, 09:36 PM
  3. Replies: 2
    Last Post: 02-24-2014, 05:51 PM
  4. "Jumping into C++" Chapter 5 Practice Problem
    By Grae in forum C++ Programming
    Replies: 4
    Last Post: 09-04-2013, 01:46 PM
  5. Chapter 2 practice problems
    By Kyle Spalding in forum C++ Programming
    Replies: 14
    Last Post: 03-19-2013, 06:12 AM