-3

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;
}
Chirag Jain
  • 11
  • 1
  • 3
  • 2
    `char idc` Remember that `char` is a single character. Maybe you want `const char*` or better yet use `std::string`. – drescherjm Apr 27 '16 at 15:32
  • 1
    in fact it is hard to believe that this code compiles, strcmp should not accept a char as an argument – pm100 Apr 27 '16 at 15:36
  • 1
    ***I am required to make in Turbo C++*** Make sure if you go into programming that you devote some time to learn on a modern `c++` compiler. Turbo c++ was good 25 years ago but that was before the `c++` standardization. A lot has changed since then. – drescherjm Apr 27 '16 at 15:36
  • Also do not forget about the null terminator. If you expect the password to be password then the size of `pass` needs to be 9 to include that null terminator – NathanOliver Apr 27 '16 at 15:36
  • I can't imagine what the compiler might do with `char idc={"PLAYNOW"};`, but I'm certain it won't do what you want. I'd try `const char* idc={"PLAYNOW"};` instead. Also allocate more space for null terminators in `id` and `pass`. – Logicrat Apr 27 '16 at 15:48
  • `p==o` should be `p==0` Please try and get your code to compile. – Martin Bonner supports Monica Apr 27 '16 at 15:53
  • Also, please try and get your school to use gcc or clang rather than Turbo C++. The cost is no higher, and they compile modern C++ rather than an ancient dialect. – Martin Bonner supports Monica Apr 27 '16 at 15:55
  • @Logicrat: I'd prefer to use `const char idc[] = {"PLAYNOW"};` (it saves the memory cost of an extra pointer.) – Martin Bonner supports Monica Apr 27 '16 at 15:56

3 Answers3

2

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::strings 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;
}
Bob__
  • 12,361
  • 3
  • 28
  • 42
1

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)
Community
  • 1
  • 1
thuc do van
  • 131
  • 1
  • 7
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?

0101010
  • 61
  • 1
  • 4