简体   繁体   中英

Program in C++ to store ID and Password

This is a part of the program which I am required to make in Turbo C++; Here, if I give input of id as "PLAYNOW" and pass as "PASSWORD", variable p is storing the value 0 but i isn't storing. id variable is storing some junk number at the end of PLAYNOW and I am not able to figure out why. Please help. Please ignore any header files not added and the way I have taken input of password. Thank you!

#include<conio.h>
#include<iostream.h>
#include<string.h>
#include<process.h>

int main()
{
    char id[7],pass[8];
    cout<<"Enter id: ";
    cin.getline(id,7);
    cout<<"Enter pass: ";
    cin.getline(pass);
    char idc={"PLAYNOW"};
    char passc={"PASSWORD"};
    int i=strcmp(id,idc);
    int p=strcmp(pass,passc);

    if(i==0&&p==o)
        cout<<"Welcome. ";
    else
        exit(0);
    getch();
    return 0;
}

One problem in OP's code is the insufficient amount of memory allocated for the variables. For example, with those lines:

char id[7];
cin.getline(id,7);

The function getline can read and store from the input stream up to 6 chars in the null terminated char array id , but then the program has to compare that string to PLAYNOW , which is 7 chars long.

This leads to next problem, when getline leaves unread chars in the stream, the failbit is set, preventing any further readings.

To fix those, even with the old standard, OP can do something like this:

const int ssize = 32;      // enough space to store id or password
const int ssmax = 1024;
// big value...    ^^ try  std::numeric_limits<std::streamsize>::max()  instead

char id[ssize],
     pass[ssize];

cout << "Enter id: ";
cin.getline(id, ssize);          // extract (ssize - 1) chars from input
if (  cin.fail() ) {
    cin.clear();               // if user entered more then ssize chars
    cin.ignore(ssmax, '\n');   // clear the stream
}

The same for pass (OP didn't pass 8 to the second getline, too).

Then, the declarations of the strings which contain the expected ID and password are wrong. Use those:

char idc[] = "PLAYNOW";
char passc[] = "PASSWORD";

The last lines could be rewritten too:

if ( strcmp(id, idc) != 0  ||  strcmp(pass, passc) != 0 )
    exit(0);

cout << "Welcome. ";
cin.get();

return 0;     // end of main()

BTW, I'm quite sure that std::string s belonged to C++98, so this should work too:

#include <iostream>
#include <string>

int main() {
    std::string id, pass;    
    std::cout << "Enter id: ";
    std::getline(std::cin, id);          
    std::cout << "Enter pass: ";
    std::getline(std::cin, pass);            

    std::string idc("PLAYNOW");
    std::string passc("PASSWORD");

    if ( idc != id  ||  passc != pass )
        exit(0);

    std::cout << "Welcome. ";

    return 0;
}

if id and pass are "PLAYNOW" and "PASSWORD", id has length is 8 , and pass has length is 9 (NULL character at end of string).

I changed as below and result print in cmd is Welcome.


char id[8],pass[9];

...

cin.getline(id,8);

char idc[] =  "PLAYNOW" ;

char passc[] =  "PASSWORD" ;

...

cin.getline(pass,9);

...

if(i==0&&p==0)

Well, beyond numerous mistakes that I simply can't begin to list, I see that you are reading from a char stream without giving the stream size. Try:

cin.getline(pass, 8);

I would seriously consider using a compiler that is a bit newer, and focusing more on readability and convention, as it's clear that there are better ways to do what is ultimately being done here. Especially with the line I pointed out, this is very ad hoc. What if the string is longer/shorter than 8 characters?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM