Beginner Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsOtherBeginner Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 28th, 2011, 10:23 PM
Typecasted Typecasted is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2011
Posts: 3 Typecasted User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 28 m 40 sec
Reputation Power: 0
Question 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.

Reply With Quote
  #2  
Old June 28th, 2011, 11:37 PM
kicken's Avatar
kicken kicken is offline
Wiser? Not exactly.
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: May 2001
Location: Bonita Springs, FL
Posts: 5,654 kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)  Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6
Time spent in forums: 2 Months 2 Weeks 2 Days 5 h 1 m 44 sec
Reputation Power: 3436
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.
__________________
Recycle your old CD's, don't just trash them


Spidermonkey Tutorial;

If I helped out out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud

Reply With Quote
  #3  
Old June 29th, 2011, 04:19 AM
danieljames123 danieljames123 is offline
Web Developer
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2011
Location: India
Posts: 74 danieljames123 New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 12 h 22 m 13 sec
Reputation Power: 0
Facebook
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.
__________________
psd to magento | magento developers

Reply With Quote
  #4  
Old June 29th, 2011, 07:36 AM
Typecasted Typecasted is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2011
Posts: 3 Typecasted User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #5  
Old June 29th, 2011, 07:42 AM
kicken's Avatar
kicken kicken is offline
Wiser? Not exactly.
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: May 2001
Location: Bonita Springs, FL
Posts: 5,654 kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)  Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6
Time spent in forums: 2 Months 2 Weeks 2 Days 5 h 1 m 44 sec
Reputation Power: 3436
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.

Reply With Quote
  #6  
Old June 29th, 2011, 01:38 PM
Typecasted Typecasted is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2011
Posts: 3 Typecasted User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsOtherBeginner Programming > Newb firstprog.c

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap