0

What is wrong about the code? The values are not saved correctly in the arrays.

/*Deklaration und Initialisierung*/
float addition1=15, addition2=9, subtrahend1=5, subtrahend2=2;
double multiplikation1=9, multiplikation2=21, divident1=10, divident2=2;

const char passwort[]="15sdDv4";
const char username[]="KevKevin";
char tippedpasswort[8], tippedusername[8];
fflush(stdin);

printf("******************* Identifikation ************************\n\n Benutzername: ");

fgets(tippedusername,9,stdin); 
printf(" Passwort: ");
fflush(stdin);
fgets(tippedpasswort,9,stdin);

printf("Tippeduser: %s\nTippedpassword: %s\nUser: %s\nPassword: %s\n",tippedusername, tippedpasswort, username, passwort);
system("pause");

if (strcmp(tippedusername,username) != 0 || strcmp(tippedpasswort,passwort) != 0) {
    printf("\nMELDUNG: Passwort oder Benutzername leider falsch!\n");
    return 0;
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
wsdt
  • 95
  • 1
  • 8
  • Wäre euch sehr dankbar, wenn ihr eine Lösung für mich hättet, stell mich glaub ich ziemlich blöd an. – wsdt May 07 '15 at 06:15
  • The post and the answer MUST be in english plz. Or i will have to vote to close the post – Phong May 07 '15 at 07:09

1 Answers1

2

I guess this is German language, right? Alas, I don't understand.

Point 1

Referring C11 standard, chapter 7.21.5.2, (emphasis mine)

If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.

fflush(stdin); is undefined behaviour.

Point 2

I think you got the fgets() wrong. As per the man page

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte (\0) is stored after the last character in the buffer.

The one less is due to the requirement of putting the null, so, you should supply the exact allocated size, not +1 to it.

You've got to change

fgets(tippedusername,9,stdin);

to

fgets(tippedusername,8,stdin);

or

fgets(tippedusername,sizeof tippedusername,stdin);

same for tippedpasswort case.

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261