Challenge #1 + #2 + questions

+1 Pawel Dziurewicz · November 4, 2015
First of all , Bucky.. I love your tutorials :)  I wish to see more challenges/exercises. This is the only way to learn how to program.. :)

What would be the best way to retry the input in case when password o not meet the criteria? I was trying to use goto however then my program was working incorrectly in spite of fact i was clearing both array and control bits. like it was jumping to the label, an then it was always returning else from my elseif statements. So i was getting input, checking it and it was always else even if I used required Capita, Digit and/or special.

Cheers guys!
Pawel

code with goto:

well in the meantime i decided to try with while loop instead of goto... however result is the same... variables to not clear ;/ and im getting wrong outputs...

#include
#include
#include
#define true 1
#define false 0
#define CLEAR(x) memset(x,'\0',20)

int main()
{
// haslo == password in polish  I was to lazy to change it after i finished

    typedef int bool;
    int letter = 0;
    bool capitalLetter = 0;
    bool specialSign = 0;
    bool digit = 0;
    char haslo[20]="";


 //   printf("Please set Your password. (max 20 characters)\n");
 //   scanf(" %s", haslo);

//array parsing

  //  size_t len = strlen(haslo);

    while((capitalLetter == false) || (digit == false) || (specialSign == false)){
    printf("Please set Your password. (max 20 characters)\n");
    scanf(" %s", haslo);

    size_t len = strlen(haslo);

    for(letter ; letter < strlen(haslo) ; letter++){
        if (isupper(haslo[letter])){
            capitalLetter = true;
            continue;
           // printf("Twoje haslo to: %d", capitalLetter);
        }
        if (isdigit(haslo[letter])){
            digit = true;
            continue;
           // printf("Twoje haslo to: %d", digit);
        }
        if ((!isdigit(haslo[letter])) && (!isalpha(haslo[letter]))){
            specialSign = true;
            continue;
           // printf("Twoje haslo to: %d", specialSign);
        }
    }

// Password validation and output
    if((capitalLetter == true) && (digit == true) && (specialSign == true)){
        printf("\nYour password :'%s' is correct.\nYou have: a Capital, a Digit and a Special sign. Good job!\n", haslo);
    }else if((capitalLetter == true) && (digit == true) && (specialSign == false)){
        printf("\nYour password :'%s' didn't meet the minimum security criteria. You used Capital, Digit but you didn't use a special character. Please use Special Character next time.. \n\n", haslo);
        CLEAR(haslo);
        capitalLetter = false;
        digit = false;
        specialSign = false;
    }else if((capitalLetter == true) && (specialSign == true) && (digit == false)){
        printf("\nYour password :'%s' didn't meet the minimum security criteria. You used Capital, Special but you didn't use a Digit character. Please use a Digit next time.. \n\n", haslo);
        CLEAR(haslo);
        capitalLetter = false;
        digit = false;
        specialSign = false;
    }else if((specialSign == true) && (digit == true) && (capitalLetter == false)){
        printf("\nYour password :'%s' didn't meet the minimum security criteria. You used Digit, Special but you didn't use a Capital letter. Please use a Capital next time.. \n\n", haslo);
        CLEAR(haslo);
        capitalLetter = false;
        digit = false;
        specialSign = false;
    }else{
        printf("\nYour password :'%s' didn't meet the minimum security criteria. You are intelligent in that very special other way... Please set at least one Capital, Digit and a Special character \n\n", haslo);
        CLEAR(haslo);
        capitalLetter = false;
        digit = false;
        specialSign = false;
    }
    }
// bits verification:
  /*
    printf("Capital : %d \n", capitalLetter);
    printf("Digit : %d \n", digit);
    printf("Special : %d \n", specialSign);
  */

    return 0;
}


Well just finished #2. I wont spam, so ill add this to this thread.
Now, why am i always getting the same values from rand? It depands on how many iterations ill do in for loop. If its is 3, i always get 17 in first sym and 16 in 2nd sum.
What i can see is that sum of 2nd diceSUM depands on how many rolls were made in first sum. Fisrt sum is always same in my case,,
why?


#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#define true 1
#define false 0


int main()
{
    int diceRoll = 0;
    int diceRollSum = 0 ;
    int choice;
    int newDiceSum = 0;

    int i;

    for(i=0;i<3;i++){
        diceRoll = ( rand()%6 ) + 1;
        diceRollSum += diceRoll;
   //     printf("%d \n",diceRoll);
    }

    printf("Sum of Your 3 rolls is: %d \n",diceRollSum);
    printf("Will the sum of next 3 roll be : \n H - higher,\n L - lower,\n S - same as now.\n");

    int upperChoice;



    do{
        choice = getchar();
        upperChoice = toupper(choice);
        putchar(upperChoice);
    //  printf("Wklepales: %c \n", upperChoice);
    }while((upperChoice != 'H') && (upperChoice != 'S') && (upperChoice != 'L'));


    for(i=0;i<3;i++){
        diceRoll = (rand()%6) + 1;
        newDiceSum += diceRoll;
   //      printf("%d \n",diceRoll);
    }
    printf("Sum of Your 3 rolls is: %d \n",newDiceSum);

    if((diceRollSum < newDiceSum) && (upperChoice == 'H')){
        printf("You are right, 2nd SUM is Higher\n");
    }else if((diceRollSum > newDiceSum) && (upperChoice == 'L')){
        printf("You are right, 2nd SUM is Lower\n");
    }else if((diceRollSum == newDiceSum) && (upperChoice == 'S')){
        printf("You are right, both are Same\n");
    }else{
     printf("You are not right, try again, First SUM is %d , and 2nd SUM is %d \n", diceRollSum, newDiceSum);
    }
  //  printf("%c",choice[0]);


    return 0;
}


Post a Reply

Replies

Oldest  Newest  Rating
0 c student · November 5, 2015
i'm going to ignore the case with goto

took me a while to debug but it seems that you have failed to reinitialize your counter variable "letter" after each loop, therefore it would always start checking at the position where it left off in the last round.


some c styling you should consider:
1. don't use goto.
2. don't use goto.
3. don't use goto.
4. don't use goto.
5. don't use goto.
6. #defines in all capitals
7. don't use goto.

also, fix your code for #2.  i'm going to assume your problem with rand is because you are always using the same numbers to seed it therefore always giving you the same numbers every time.
0 Pawel Dziurewicz · November 6, 2015
Well i knew goto sucks :) This is why i redesigned and used while.

#defines in all capital - will do in future.

fixed code 2 paste..  weird...
0 Pawel Dziurewicz · November 6, 2015
Well i knew goto sucks :) This is why i redesigned and used while.

Cool! you were right! "letter" variable was never cleared after first iteration of while! Setting letter to 0 in for loop solved the issue.

for(letter = 0 ; letter < strlen(haslo) ; letter++){




#defines in all capital - will do in future.

fixed code 2 paste..  weird...
0 Pawel Dziurewicz · November 6, 2015
Ok... I know now what was wrong.. as you said I was not seeding.
I added (both works):


#include 




    time_t seconds;
    time(&seconds);
    srand((unsigned int) seconds);i added 



srand ( time(NULL) );
  • 1

C

107,258 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator