Gå til innhold

"Passord safe program" ? C hjelp


Anbefalte innlegg

Hei prøver og lage en simpel passord safe :p

 

#include <stdio.h>
int main()
{
char name[20];
char color[20];
printf("What is your name?");
scanf("%s",name);
if scanf("%s",name) = David;
printf("What is your favorite color?");
else printf ("get away from the pc!");	
scanf("%s",color);
if scanf("%s",color) = blue
printf("%s’s favorite color is %s\n",name,color, oh the password is david);
return(0);
}

 

Så får jeg error slik

 

john@ubuntu:/home/prog/C$ gcc safe.c -o safe
safe.c: In function ‘main’:
safe.c:8: error: expected ‘(’ before ‘scanf’
safe.c:8: error: ‘David’ undeclared (first use in this function)
safe.c:8: error: (Each undeclared identifier is reported only once
safe.c:8: error: for each function it appears in.)
safe.c:10: error: ‘else’ without a previous ‘if’
safe.c:12: error: expected ‘(’ before ‘scanf’
safe.c:12: error: ‘blue’ undeclared (first use in this function)

 

Noen som vett hvordan man skal gjøre dette ?

 

Takk ;D

Lenke til kommentar
Videoannonse
Annonse

Denne inneholder minst 3 feil: if scanf("%s",name) = David;

Les litt om strncmp. http://www.cplusplus.com/reference/clibrary/cstring/strncmp/

 

Ok :D endret litt her nå

 

#include <stdio.h>
int main()
{
char name[20];
char color[20];
printf("What is your name?");
sc
anf("%s",name);
if strncmp scanf("%s",name) == "David";
else printf ("get away from the pc!");	
printf("What is your favorite color?");
else printf ("get away from the pc!");	
scanf("%s",color);
if strncmp scanf("%s",color) == "blue");
printf("%s’s favorite color is %s\n",name,color, "oh the password for facebook is david");
return(0);
}

 

Men så kom errorene :(

 

john@ubuntu:/home/prog/C$ gcc safe.c -o safe
safe.c: In function ‘main’:
safe.c:7: error: ‘sc’ undeclared (first use in this function)
safe.c:7: error: (Each undeclared identifier is reported only once
safe.c:7: error: for each function it appears in.)
safe.c:8: error: expected ‘;’ before ‘anf’
safe.c:9: error: expected ‘(’ before ‘strncmp’
safe.c:9: error: ‘strncmp’ undeclared (first use in this function)
safe.c:12: error: ‘else’ without a previous ‘if’
safe.c:14: error: expected ‘(’ before ‘strncmp’
safe.c:14: error: expected statement before ‘)’ token

Lenke til kommentar

Den var jo svært enkel å se. Du har uforvarende lagt inn et linjeskift midt i sc anf

 

Du trenger forresten ikke å lese inn 'name' to ganger.

Les det først, så tar du if (strncmp (name, "David", 5) == 0)

Du har rotet kraftig med syntaksen her.

 

Prøv deg fram litt av gangen, det tar sin tid å lære disse tingene. :)

 

 

 

 

 

 

Lenke til kommentar

Programmet er "ferdig, litt (STORE) bugger"

 

#include <stdio.h>
int main()
{
char name[20];
char color[20];
printf("What is your name?");
scanf("%s",name);
if (strcmp(name,"david") == 0);
else printf ("get away from the pc!");
printf("What is your favorite color?");
scanf("%s",color);
if (strcmp(name,"blue") == 0);
else printf("get away from the pc!");
printf("%s’s favorite color is %s\n",name,color, "oh the password for facebook is david");
return(0);
}

 

Så skjer dette :(

 

john@ubuntu:/home/prog/C$ ./safe
What is your name?david
What is your favorite color?blue
get away from the pc!david’s favorite color is blue
john@ubuntu:/home/prog/C$ ./safe
What is your name?notthis
get away from the pc!What is your favorite color?whatareyounotstopping?          
get away from the pc!notthis’s favorite color is whatareyounotstopping?

Lenke til kommentar

Du leser til 'color' men bruker 'name'.

scanf("%s",color);

if (strcmp(name,"blue") == 0);

 

 

Tenk dessuten litt på logikken med if (true) {ingenting} - else {statement}.

Prøv heller if (not true) {statement}

 

Ok da er programmet helt ferdit (Tror jeg) :D

 

 

 /***************************/
/**Program made by Nesiory**/
/***************************/
#include <stdio.h>
int main()
{
   char name[20];
   char color[20];
   char key[20];

/**Question A**/
   printf("What is your name?");
   scanf("%s",name);
   if (strcmp(name,"david") != 0)   		/** Change (name,"ThisChange") to change name **/
   {
     	puts("Get away from the pc!");
     	sleep(5);
return(0);
   }

/**Question B**/
   printf("What is your favorite color?");
   scanf("%s",color);
   if (strcmp(color,"blue") != 0)  		/** Change (color,"ThisChange") to change color **/
   {
puts("Get away from the pc!");
sleep(5);
return(0);
   }

/**Question C**/
   printf("What is the master key?");
   scanf("%s",key);
   if (strcmp(key,"david") != 0)  		/** Change (Key,"ThisChange") to change master key **/
   {
puts("Get away from the pc!");
   	sleep(5);
   	return(0);
   }

/**Sulution**/
/** Change (Password is "ThisChange"\n",name,color) to change the final password **/
   printf("%s’s favorite color is %s, oh i forgot...the password is david\n",name,color );
   return(0);
}

 

 

Endret av nesiory
Lenke til kommentar

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
  • Hvem er aktive   0 medlemmer

    • Ingen innloggede medlemmer aktive
×
×
  • Opprett ny...