The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Other
> Beginner Programming
|
Newb firstprog.c
Discuss Newb firstprog.c in the Beginner Programming forum on Dev Shed. Newb firstprog.c Beginner Programming forum discussing problems and solutions for just about any issue. Experienced programmers offer their help to those just starting out.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 28th, 2011, 10:23 PM
|
|
Registered User
|
|
Join Date: Jun 2011
Posts: 3
Time spent in forums: 28 m 40 sec
Reputation Power: 0
|
|
Newb firstprog.c
I've written and compiled the first program I've literally created without my learnin book in front of me. I have seemingly one bug in it... or one and a half i would almost say. Mostly i want to know how i did for like a 1 month beginner and if you have more ideas for programs to write to help me learn.
im sorry i dont know the coding for html to make this look neater but if you have comments/criticism on this, lemme know. thanks
pass.c
#include <stdio.h>
#include <string.h>
int main() {
char str_1[7];
char str_2[7];
strcpy(str_1, "Death");
printf("What Is The Password?\n");
scanf("%s\n", str_2);
printf("%s, contemplating.\n", str_2);
if(*str_2 == *str_1) {
printf("success\n");
printf("Change Password\n");
scanf("\%s\n", str_1);
printf("Success");
printf("Pass is now-->\t %s\n", str_1);
}
else {
printf("failure.\n");
printf("You lose.\n");
}
}
And the results seemed to be screwed by the first password as youll notice if you compile or just can read it and know whats going to be wrong. Like i said im a newbie, so hit me with as much criticism as you can. 
|

June 28th, 2011, 11:37 PM
|
 |
Wiser? Not exactly.
|
|
Join Date: May 2001
Location: Bonita Springs, FL
|
|
Quote: | Originally Posted by Typecasted
im sorry i dont know the coding for html to make this look neater but if you have comments/criticism on this, lemme know. thanks |
Wrap your code in [code] [/code] tags and it will preserve your indentation and spacing.
Code:
if(*str_2 == *str_1) {
That if statement is only going to be comparing the first character of both strings. *str_2 and *str_1 is the same as doing str_2[0] and str_1[0]. One might be tempted to try and compare the strings using if (str_2 == str_1) but that also will not work. That would compare the pointer values of str_2 and str_1, not the contents of the strings.
To compare the contents of two strings, you need to loop through both strings and compare letter by letter to see if they are the same. There is a standard function to do this called strcmp. This function returns 0 if the strings are equal, non-zero otherwise.
|

June 29th, 2011, 04:19 AM
|
|
Web Developer
|
|
Join Date: May 2011
Location: India
Posts: 74
Time spent in forums: 12 h 22 m 13 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by kicken Wrap your code in [code] [/code] tags and it will preserve your indentation and spacing.
Code:
if(*str_2 == *str_1) {
That if statement is only going to be comparing the first character of both strings. *str_2 and *str_1 is the same as doing str_2[0] and str_1[0]. One might be tempted to try and compare the strings using if (str_2 == str_1) but that also will not work. That would compare the pointer values of str_2 and str_1, not the contents of the strings.
To compare the contents of two strings, you need to loop through both strings and compare letter by letter to see if they are the same. There is a standard function to do this called strcmp. This function returns 0 if the strings are equal, non-zero otherwise. |
I am also agree with this one, well in fact when I was fresher I have such error ant the above way is the smartest way to recover that.
|

June 29th, 2011, 07:36 AM
|
|
Registered User
|
|
Join Date: Jun 2011
Posts: 3
Time spent in forums: 28 m 40 sec
Reputation Power: 0
|
|
|
Well thank you for your input. I did not know of strcmp, so that can be most useful.
|

June 29th, 2011, 07:42 AM
|
 |
Wiser? Not exactly.
|
|
Join Date: May 2001
Location: Bonita Springs, FL
|
|
Quote: | Originally Posted by Typecasted Well thank you. I did not know of strcmp, so that can be useful. I'm curious though, as to why for the most part it does work with my error. (I mean as long as you type the password, then something random, it still knows you got the password right). |
With your code, anything you type which begins with the letter 'D' will pass and the program will consider it valid. Anything that does not start with 'D' will fail.
Ex, enter 'Doom' as the password and it will work.
|

June 29th, 2011, 01:38 PM
|
|
Registered User
|
|
Join Date: Jun 2011
Posts: 3
Time spent in forums: 28 m 40 sec
Reputation Power: 0
|
|
|
I got that after thinking about it for a sec. Thought i edited that off =p
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|