Thread: need to read a string in one character at a time

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    1

    Question need to read a string in one character at a time

    I am writing code to verify a password. And I need to read in the input both as a string and as individual characters to run various checks on the characters. Some of the strings do contain spaces so I cannot use the cin but I'm not sure how to use getline and/or the getchar commands.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >to run various checks on the characters
    Just read the string as a whole and walk through it to perform your checks.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    4

    Getline() is your friend

    I almost never use just plain cin for anything. cin.getline() works much better for a variety of reasons. Not the least of which is that it won't end at a space - it'll also let you assign how long of input you'll accept, which will halt buffer overflows.

    You use it like this:

    #include <iostream>

    using namespace std;

    int main(){
    char text[256];

    cout << "Enter a line of text: ";
    cin.getline(text, 255);

    }

    The getline() method takes two arguments. The first is the char array you want to store the data in. The second is how long you'll let the text be. Make it one less than the array size so you'll have room for a terminating character. Finally, getline() can take a third arguement which is the character you want it to end on. It defaults to a newline, but you can have it be anything. For example if you wanted it to end on a '>' you'd use it like this:

    cin.getline(text, 255, '>');

    Hope this helps!

  4. #4
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    or
    Code:
    #include <iostream>
    
    string input;
    
    int main()
    {
         cin>>input;
         char Password[sizeof(input)];
         for (int i=0;i<sizeof(input);i++)
         {
              Password[i]=input[i];
         }
         //now you can check individual characters
    }
    that works fine too, i like it better becuase it allows for a variable size of password.
    PHP and XML
    Let's talk about SAX

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Somehow, I doubt that works...
    you forgot a header file, namespace scoping... plus sizeof(input) won't work.. maybe input.length()...

    char Password has to be dynamically allocated, its size is not known at runtime. You're not leaving room for the null terminator, even if char Password[sizeof(input)]; did work.

    You can't use operator >> with a string type, you need to use getline(cin, string). Oh, and you didn't return a value from main!

  6. #6
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001

    i haven't slept in a long time
    but with those tweaks it does work
    PHP and XML
    Let's talk about SAX

  7. #7
    try this... it doesn't check every character but it is case sencitive..

    Code:
    #include <iostream>
    #include <windows.h>
    #include <conio.c>
    #include <string>
    #define PASSWORD "pooh"
    
    int main()
    {
     char pass[50];
     char name[50];
     int age;
     cout<<"Name:";
     cin>>name;
     cout<<"\nAge:";
     cin>>age;
     clrscr();
     cout<<"Name: "<<name<<"\nAge: "<<age;
     Sleep(3000);
     clrscr();
     Sleep(1000);
     cout<<"A small hint...";
     cout<<"\n\nThe password is: "<<strlen(PASSWORD)<<" characters long";
     Sleep(2000);
     clrscr();
     cout<<"Password: ";
     cin>>pass;
     if(strcmpi(PASSWORD, pass) == 0)
     {
      cout<<"\n\nCorrect...";
      Sleep(2500);
      return 0;
     }
     else
     {
      while(strcmpi(PASSWORD, pass) != 0)
      {
      cout<<"\n\nIncorrect...";
      Sleep(2500);
      clrscr();
      cout<<"Password: ";
      cin>>pass;
      }
      cout<<"Correct...";
      Sleep(2500);
      exit(1);
     }
     
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. Replies: 1
    Last Post: 05-30-2003, 02:31 AM
  4. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM
  5. Replies: 1
    Last Post: 07-24-2002, 06:33 AM