simple loop problem

This is a discussion on simple loop problem within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, my 1st post new to C++ ,working through tutorials on my own. have a ? about my simple ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    2

    simple loop problem

    Hi everyone, my 1st post new to C++ ,working through tutorials on my own. have a ? about my simple loop program called password, It seems to work but only if user inputs a (int), I try a char and it loops, anyone tell me why ?
    sorry if wrong post style, work on it, flame away.

    #include <iostream>

    using namespace std;

    int main()
    {
    for ( int s = 0; s < 26; s = s + 5 ) {
    cout<< s <<endl;
    }
    cin.get();

    // MY 1st PASSWORD PROGRAM STARTS HERE


    int pw = 1313; // varible set (int)
    cout<<"Enter password: "; // asks user for a password
    cin>> pw; // user input
    cin.ignore();
    while ( pw != 1313 ){ // if pw incorrect
    cout<<"Sorry thats incorrect "<<endl; // statement for incorrect
    cin.ignore();
    pw++;
    cout<<"Reenter password: ";
    cin>> pw;
    }
    if ( pw == pw ){ // if input correct (pw) print statement
    cout<<"Your in"<<endl; // statement for correct answer
    cout<<"You have unlocked the secret of the universe ";
    }
    cin.get();


    }



  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Welcome to the boards. If you haven't already done so then please take some time to familiarise yourself with the faq:
    http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

    Make sure you have a look at the the posting guidelines:
    http://cboard.cprogramming.com/annou...ouncementid=51
    Following the rules will ensure you get a prompt answer to your question.

    Remember, too, that the board has a search facility, a link is at the top of your screen:
    http://cboard.cprogramming.com/search.php
    It will often get you a quicker answer to your questions than waiting for a response to one you have posted.


    If you have any questions about this you may ask or you can contact one of our forum leaders:
    http://cboard.cprogramming.com/showgroups.php

    --------------------------------
    Specifically, you should use [code][/code] tags when posting code so it displays properly.

    As to your problem, the reason it only works if you input an integer is that your pw variable is an int. If you want to be able to read in a string (which is just 0 or more characters), then use the string type. To do this you'll have to #include <string>. You'll also want to put your official password in double quotes: "1313" or "mypassword" or whatever.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    2

    Thank you

    Thanks, Daved for fast answer,looks like (strings) in lesson 9 , guess I'll jump
    ahead and read up.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    I would actually skip lesson 9 and go to the C++ string tutorial (there is a link is at the top of lesson 9). There is little use for C style strings when you are learning C++, they are just often taught first for legacy reasons.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 01:45 AM
  2. having problem with string statement during a loop!
    By Hp7130p in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2005, 09:40 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Replies: 5
    Last Post: 12-03-2003, 04:47 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM

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