Question:
c++ Help [with code]?
Anon
2010-09-04 18:09:25 UTC
http://pastebin.com/pXYJAnkx

Code ^

Ok So my problem. The program compiles fine, when i run i type a name and a password [i then confirm with the correct password]

it closes, i check to see if the file has been made with the info in and it hasnt.
its as if its skipped the process, what have i done wrong?
Four answers:
Ilie
2010-09-04 18:47:18 UTC
Hi,



The correct solution is included in this answer, just read the little explanation before it so it will clear things up. :)



Although the suggestion in the first answer is correct, the details are wrong. char arrays are not pointers although the names without the [] operator resolve indeed to addresses. As proof, you can use the sizeof operator on an array and see that it will return the size of the array in bytes, instead of the size of a pointer. Furthermore, you can change where pointers point to - that cannot be done with arrays. Last but not least, if you initialize a pointer with a string literal and try to modify that the result is undefined. That is not true for arrays. You do NOT want to do what the second answer suggests because that's C-style C++ which is a very poor thing to do and which will quickly lead you into design problems once your project gets a bit more complex.



You want to use something like newPassword.compare(PasswordConfirm) != 0, where newPassword and PasswordConfirm are of type string. (Don't forget to #include -- never since that's only good for quickly porting C programs that use string.h.)



Here are a few other suggestions:



1) When an IF statement only has 2 branches, there's no need for an ELSE-IF, only an ELSE. Although from a functional point of view there is no difference, your program will suffer from a performance penalty which might be more obvious in very tight loops.



2) In C++, if main reaches its closing bracket, it returns 0 by default so there's no need to make the source code harder to read by adding the two extra "return 0;" statements. If you want to have less intendation, you can instead just check if the strings are different, print an error message and *return*, close the bracket and then continue the program normally, outside any IF's or ELSE's.



3) There's no need to use the \n escape sequence for a newline character, you have std::endl for that in C++. That will make the source code more readable because people don't need to remember what \n does.



4) Using things like system (.e.g, in system("CLS")) is usually considered bad practice because it is not portable. However, the specifications of your program are your decision to make.



Cheers,

Bogdan
oops
2010-09-04 18:27:34 UTC
The problem is that you are using c-style strings and trying to compare them like this:



if (newPassword == PasswordConfirm)



That doesn't work, because c-style strings are just char arrays. And char arrays are just pointers. So when you do the above test, what you are actually doing is comparing the memory addresses of the two strings. That test won't pass unless they are actually the same array, the contents of the arrays don't even come into the picture. There are two solutions for this:



The right solution: Stop using c-style strings. Use the c++ standard string class. http://www.cplusplus.com/reference/string/string/ . include , and replace all instances of "char whatever[12];" with "string whatever;"



The wrong solution: Continue using c-style strings, but if you need to make comparisons, use the strcmp function. http://www.cplusplus.com/reference/clibrary/cstring/strcmp/
modulo_function
2010-09-04 18:42:09 UTC
Good that you included your code. That made it easy for me to work on it.



You should use strcmp(..) to compare strings not the logical operator ==



if( strcmp(newPassword,PasswordConfirm) == 0 )

instead of your:

if (newPassword == PasswordConfirm)



add

#include

to get the function



After making these changes it creates the text file properly!!
?
2016-11-16 12:13:31 UTC
it incredibly is actual that in case you create an empty C++ console undertaking and manually rename your cpp document to furnish it .c extension, seen Studio *will* execute the Microsoft C compiler, besides the undeniable fact that it incredibly is a doubly-out of date 1989 C compiler. Microsoft abandoned C an prolonged time in the past. in case you like to stay with this IDE and assemble C, exchange the underlying compiler. Intel C/C++ is a sturdy determination.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...