Archived

This topic is now archived and is closed to further replies.

Greenfly80

Whats wrong with this code?

Recommended Posts

I started learn C about 3 days ago and thought it was quite simple until i go to TRUE and FALSE. Anyway i thought i would make i simple program but can''t get it to run. I think it is the if statement. Sorry if this is so simple but i''ve only just started. #include <iostream.h> int main() { char password; //Stores the data password password=open; int passcode;//stores the data passcode passcode=10; cout<<"Please enter password and passcode to enter:"; cin>>password>>passcode; if(password&&passcode) cout<<"You have entered the correct password and passcode"; else cout<<"You have entered the password and passcode incorectly"; return 0; } //This is a simple program that asks for 2 bits of infomation that it checks against stored data which then allows the user to enter or not. This is the begining of a password prog.

Share this post


Link to post
Share on other sites
A character holds a number from -127 to 128. A number stored in a char corresponds to the ASCII code of a specific character.

You create a single char variable. As such, it can only hold a single char. In c/c++, you use chars like number variables; the only difference is they hold less, and when you tell cout to output them it outputs the associated character to the screen.

You can wrap one character in single quotes to get the number. You can then assign this number to a char (or any other numeric variable for that matter).

A string is a collection of characters. How do you store a collection of like objects in C/C++? As an array. In order to hold an entire password, you need to use an array. If you use a single char variable, you can only store a single character.

To assign a string to an array of characters you can put the string in double quotes. You can only do this when initializing it. That means that the following is legal:

char mystring[6] = "Hello";

...but this is not:

char mystring[6];
mystring = "Hello"; //Illegal


Now you may have noticed that, although the string "Hello" contains 5 characters, I allocated room for 6. This is because the last character is NULL. NULL is a special value that means different things in different situations. In a string, it indicates the end of the string.

To access an individual element in a character array, you use it the same way you would any other array. If you were using the first array that contains the text "Hello" then mystr[0] == ''H'', mystr[1] == ''e'', and so on. mystr[5] == NULL.

You can pass an array to cout. Cout knows that each number in it represents a character, and it knows to stop when it reaches the first NULl character. So you can output an array to the screen this way.

Chances are that by the time I finished typing this someone already replied with a quick fix. I however wanted to explain what you were doing what you needed to do, and what the difference between the two were. I hope it helps. For more information, you can go to the Tutorials section of www.cprogramming.com .

Also note that there''s an alternative way to do strings, using the "string" class, which is part of a set of ready-made components called the STL. I personally think you should understand the traditional character array approach first though. Character arrays are often referred to as CStrings.

Share this post


Link to post
Share on other sites
Hey,
here's a working version:


int main()
{

char password[255];
char *correctPassword = "open";

int passcode;
int correctPasscode = 10;

cout<<"Please enter password and passcode to enter:";

cin >> password >> passcode;

if(strcmp(password, correctPassword) == 0 && passcode == correctPasscode)
cout<<"You have entered the correct password and passcode";

return 0;
}


Now an explanation:
1. You'll need two different variables for each thing. One for storing the correct value and one for the value the user enter. In my example I have password and correctPassword. What you did was overwriting the correct password with whatever the user entered. That's not very good.

2. You basiclly did this:
char password=open;

that wont work. A single char can only hold one character. I made an array of chars so it can hold up to 255 chars, like this:
char password[255];

3. If you compare the codes like you do, you'll check if they are true or false, and that's not what you want. To check a string (the chars) you can use the strcmp() function (don't forget to include String.h). It'll return 0 if the strings are equal. To check the int you do like this:
if(passcode == correctPasscode)

That's about it!


-----------------------------
"problems have solutions
a lifetime of fucking things up fixed in one determined flash"
- The Downward Spiral, NIN


Edited by - Rudan on January 30, 2002 5:00:19 PM

Share this post


Link to post
Share on other sites
Damn you TerranFury! You beat me to it!

(Although I fixed a few other mistakes too)


-----------------------------
"problems have solutions
a lifetime of fucking things up fixed in one determined flash"
- The Downward Spiral, NIN

Edited by - Rudan on January 30, 2002 4:49:21 PM

Share this post


Link to post
Share on other sites
I''m surprised nobody else replied in the time it took for me to type my last post! Oh well; anyway, I''ll go into more details in this post.

You can dynamically create arrays using the following method:

int * myarray = new int[12];

The catch is that when you do this you need to remember to free the memory. With the other kind of arrays (static arrays) you didn''t need to do that. With these, though, it is pretty simple. Just remember to do it:

delete [] myarray;

The variable "myarray" is not an integer; it is an "integer pointer." It stores a value that is meaningless to you. This value is the spot in memory where the array begins.

Now, you can dynamically create character arrays, aka cstrings:

char * mystring = new char[6];

Now remember that pointers are values themselves, referring to a spot of memory. Therefore, if you have two CStrings, "x" and "y" and you wanted to see if they contained the same text, your first impulse is to do this:

if(x == y)
...

But this won''t work. This checks to see if x and y point to the same spot in memory. What you want to do is the following:

if(!strcmp(x, y))
...

The strcmp function, along with other old-fashioned C-style string manipulation functions, is found in string.h.

Since pointers simply tell the computer where a string starts, I think you can now appreciate why the NULL character is needed at the end.

Now I''ll post some example code, but using static arrays instead of the pointers we just discussed.

  

#include <string.h>
#include <iostream.h> //This is outdated, but it works. I''ll explain in another post.


int main()
{
char password[32];
int passcode;

cout << "Password:\t";
cin >> password;

cout << "Passcode:\t";
cin >> passcode;

if(!strcmp(password, "Open Sesame") && passcode == 13)
{
cout << "Welcome! You entered the correct information.\n";
}
else
{
cout << "You entered the incorrect password and/or passcode.\n";
}



return 0;
}

Share this post


Link to post
Share on other sites
all in all, i''d suggest getting a teach yourself C++ book. "Teach yourself C++ in 21 days" is a good one:

http://www.amazon.com/exec/obidos/ASIN/067232072X/qid=1012426594/sr=2-2/ref=sr_2_79_2/104-1946320-1989538

you won''t be able to learn C++ without at least a book...plus it''ll help things make sense pretty quickly..

-me

Share this post


Link to post
Share on other sites
Oh, someone DID respond! Hi Rudan.

Now I''ll go into conditionals, since that was the title of your post.

To test to see if two numbers are equal you use ==. To se if they are not, you can use !=. Similarly, there are greater than and less than operators: <, >, <=, >=.

Use them as following:

(x == y)

This stament evaluates to TRUE or FALSE. You can look for certain combinations of TRUEs and FALSEs using the operators && (and) and || (or).

Here''s an example:

x == 2 || x > 9

That whole thing evaluates to TRUE or FALSE. Hopefully you can see what I mean.

An IF statement performs a section of code if the entire statement within it evalues to TRUE.

I probably want''t too awful clear here, but I hope I helped anyway. I''ll recommend again that you go to cprogramming.com; it''ll help with this topic as well.

Share this post


Link to post
Share on other sites
And I''m back with more!

Any nonzero number evaluates to true. Zero is false. Thus:

  

if(2+3) // 2+3 = 5 != 0

{
cout << "This code will always execute.\n";
}

if(2 + 2 - 4) //2+2-4 = 0 = false

{
cout << "This text will never pop up.\n";
}



Of course, you would never want to write any code like that. I''m just doing it for demonstration purposes.

Finally, comparison tests that are true or false return 1 or 0. Remember, though, that 1 is not the only number that evaluates to true.

  
if( ((x == 2)+(y ==3)+(z==4)) > 1 )
{
//If more than one of the conditions is true...

}


That last one is tricky stuff, though, and it is rarely needed. You can quickly create some pretty unreadable code by using gimmicks like that!

Share this post


Link to post
Share on other sites
Hi

Little more explanation of strcomp:

Value Relationship of string1 to string2 after comparation with strcmp(str1,str2):

< 0 string1 less than string2
0 string1 identical to string2
> 0 string1 greater than string2

~Trob

Share this post


Link to post
Share on other sites
quote:
Original post by Rudan
(Although I fixed a few other mistakes too)

And introduced a few others.

Use std::string (<string>) if you''re using C++. It''s by far more convenient (ie, it''s time to ditch strcmp, etc people).

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
quote:
A character holds a number from -127 to 128

The C++ standard does not specify whether char defaults to signed or unsigned; some compilers (e.g. VC++ 7) let you choose the behaviour with a compiler switch.

Share this post


Link to post
Share on other sites
Whats wrong with U?
If one of your operands are zero, the statement will be false.
If both are non-zero it will be true(!).
might you have intended & instead of &&?

(GK)

Share this post


Link to post
Share on other sites
Whats wrong with U?
If one of your operands are zero, the statement will be false.
If both are non-zero it will be true(!).
might you have intended & instead of &&?

(GK)



Message from above:
Damn, my hair is grey!

Share this post


Link to post
Share on other sites
quote:
Original post by Oluseyi
And introduced a few others.

Use std::string () if you''re using C++. It''s by far more convenient (ie, it''s time to ditch strcmp, etc people).

I guess you''re right. strcmp and the likes have always worked fine for me so I haven''t bothered to check out anything else.



-----------------------------
"problems have solutions
a lifetime of fucking things up fixed in one determined flash"
- The Downward Spiral, NIN

Share this post


Link to post
Share on other sites