Sinisterly
What is wrong with my C++ code? (Test login) - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: C, C++, & Obj-C (https://sinister.ly/Forum-C-C-Obj-C)
+--- Thread: What is wrong with my C++ code? (Test login) (/Thread-What-is-wrong-with-my-C-code-Test-login)



What is wrong with my C++ code? (Test login) - N1v3k - 11-23-2011

Hey guys, I recently started learning C++ and i tried making this simple login program but I can`t get char arrays with if statements correct. Please help.
Thank you,

Code:
#include <iostream>

int main(){
    char Array[20];
    char Array2[20];
    char yesorno;
    int boom = 1;
    do {
    std::cout << " Username:";
    std::cin >> Array;
    std::cout << " Password:";
    std::cin >> Array2;
    if (Array == "N1v3k" || Array2 == "myub3rlongpasswordhere"){
        std::cout << "" << std::endl;
        std::cout << "Successful login, welcome." << std::endl;
    }else{
        std::cout << "" << std::endl;
        std::cout << "Wrong username and/or password, would you like to try again?(y/n)" << std::endl;
        std::cin >> yesorno;
        if (yesorno == 'y'){
            std::cout << "" << std::endl;
            std::cout << "" << std::endl;
            std::cout << "" << std::endl;
            std::cout << "" << std::endl;
        }else{
        ++boom;
        }
    }
    } while (boom == 1);
}

-N1v3k


RE: What is wrong with my C++ code? (Test login) - puma - 11-24-2011

if (Array == "N1v3k" || Array2 == "myub3rlongpasswordhere")

if (strcmp(Array, "N1v3k") == 0 && strcmp(Array2,"myub3rlongpasswordhere") == 0)

Also you should probably be using the string class instead of character arrays. They're easier to work with and no point in using c strings when you're using c++. I know you're using it to learn so I'll tell you hard coding a password like that is a bad idea. Best of luck to learning c++!

EDIT:

Okay, there are times cstrings are useful, but this isn't one of them.


RE: What is wrong with my C++ code? (Test login) - N1v3k - 11-25-2011

Thankyou Puma, didn`t understand much since I`m just a beginner but I`m sure I will learn. Thank you!



@bijoy What do you mean for a nice code? The code is as basic as it could and is pointless...


RE: What is wrong with my C++ code? (Test login) - puma - 11-25-2011

(11-25-2011, 02:50 PM)N1v3k Wrote: Thankyou Puma, didn`t understand much since I`m just a beginner but I`m sure I will learn. Thank you!



@bijoy What do you mean for a nice code? The code is as basic as it could and is pointless...

I'll explain it a bit then Smile.

strcmp is a string compare function.

http://www.cplusplus.com/reference/clibrary/cstring/strcmp/ <== reference

If I remember correctly you can use == on strings, single chars, and numeric data types, but not character arrays or c strings in general.

That being said it's a bad idea and shouldn't be done. The == equality operator should really only be used on numeric data types. For comparing cstrings use strcmp and for comparing c++ strings use .compare().


Hope that helps you


RE: What is wrong with my C++ code? (Test login) - Lenalee - 03-07-2012

Why not add
Code:
using namespace std;

It looks messy for me looking at those std::'s


RE: What is wrong with my C++ code? (Test login) - The Alchemist - 03-07-2012

puma is absolutely right...


RE: What is wrong with my C++ code? (Test login) - Frooxius - 03-07-2012

You can use == operator with null terminated strings (referred to as "C strings" here), which is basically an array of characters, with a null character indicating the end, but it doesn't have the meaning you would probably expect.

When a compiler encounters a string literal (simply, any specification of a string like "mystring"), it will the contents in a string table and the literal will be basically evaluated as a pointer to character array (char *) and you can use == operator on two pointers, however it will be true only if both compared strings are stored at the same address of the memory.

That of course means, that they're will be always the same, however, it doesn't mean that in every case that two null terminated strings are same, that they are at the same position in memory. There, you created an array for your own string and you are comparing it to a string stored in a string table, so even in case though both strings are same, they are stored in different places of the memory, therefore == operator will tell you that they're not the same.

Puma already gave you the working code, which uses the strcmp function to compare two strings, I would just simplify it like this:

Code:
if (!strcmp(Array, "N1v3k") && !strcmp(Array2,"myub3rlongpasswordhere"))
! inverts the value and strcmp returns zero (false) if they are equal, so you can simply invert it, so it becomes true and in case they are not identical, true becomes false.

However, there's another thing that I would point out on your code and that's variable names:
DON'T name variables as Array or Array2, it's a very bad habit and it will lead to unreadable, incomprehensible code. Imagine if you write something larger and then you'll have Array38 and Array45 there. Would you remember what are they for and why are they there?

Their name should briefly describe what they are for, what's their purpose, also it's good idea to follow a convention and start their names always with a lowercase letter. So instead of

Code:
char Array[20];
char Array2[20];
char yesorno;
Make it
Code:
char username[20];
char password[20];
char tryagain;

Additionally, here's another very bad thing:
Code:
std::cin >> Array;
Although it usually works for you, this is a very bad thing to do. You made the array contain 20 elements, minus the null character, it can store 19 characters. So what if user types 25? 30? More?
Remember that C++ does very little safety checks for you, so it won't even test if you're accessing the array beyond its end. C++ is very close to machine code, so the cin object just gets a pointer to some location in memory, where is the array stored, however it doesn't get any information about size, so it simply writes data there as long as it can. And in case user types 25 characters, it will actually write some data beyond your array, so it overwrites some other data in the memory, which can cause very weird behavior, crash your program and sometimes crash even some less stable OS (but you're unlikely to encounter that anytime soon or ever).


Proper way to do this is following:
Code:
cin.get(Array, 20);
It stores only up to 20 characters (including the null character, so 19 regular characters) in the memory area pointed by Array, so if user writes more, it won't crash the program.

This part doesn't make that much sense, I mean, the "", which is basically an empty string, so you're passing no characters to the output, so what's the point of that?

Code:
std::cout << "" << std::endl;
std::cout << "" << std::endl;
std::cout << "" << std::endl;
std::cout << "" << std::endl;
Why not simply:
Code:
std::cout << std::endl << std::endl << std::endl << std::endl;
Or even simpler:
Code:
std::cout << "\n\n\n\n";
Character "\n" is an escape character that represents a new line.

Puma & N1v3k: First, they're not "C++ strings", C++ has built in null terminated strings (also C strings) just like C, however what are you referring to are STL strings, they are part of the STL library, that became part of C++ standard, however it's still a library, support for these strings is not integrated into C++ itself, they actually use Cstrings internally and secondly you can use relational operators (==, >=, <=, <, >) with them, you don't need to use quite ugly .compare method for simple comparing. C++ allows this because of operator overloading, which allows to change meaning of an operator with respect of some class.

So following code will work just fine and dandy:
Code:
string myString0, myString1;
cin >> myString0;
cin >> myString1;
if(myString0 == myString1)
   cout << "They're the same, yaaay! :3";
else
  cout << "Aww, they're different >.>";

However, this IS a reason to use Cstrings. STL strings add additional overhead, various runtime checks, plus they are allocated dynamically from the heap and have much more memory footprint. Using them when they're not needed, especially in C++ which is often chosen as high performance language, is quite a dirty and bad habit. Plus like I said, these strings are provided as an library, albeit they are part of the standard, they are not part of the language itself, null terminated strings however are.

Additionally, null terminated strings are very often used to store arbitrary binary data, working as an buffer, because the char type corresponds to a single byte.