C challenge #1

+1 Eric Hines · January 11, 2016
I'm up to Bucky's tutorial #33. 

Here's my code, but only part of it works. I get the else statement to print. I can't seem to get the password to work....

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

int main()
{
int upperletter;
int specialchar;
int number;
upperletter;
specialchar;
number;

printf("Enter your password. The first entry must be an upper case letter. The second must be a special character. The last must be a number.\n");
scanf("%c,%c,%c\n", &upperletter,&specialchar,&number);


if(isupper(upperletter)&&isdigit(specialchar)&&isdigit(number))
{
    printf("Thank you. %c%c%c is a valid password.\n",upperletter,specialchar,number);
}
else
{
    printf("Your password does not meet the requirements.\n");
}
return 0;
}

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
0 Linguist Llama · January 18, 2016
Eric: I'm happy to engage with you, when you demonstrate that you wish to engage with me. Please answer these questions I asked you earlier:

1. Given an array of 1 character, the size is 1 but the only character is array[0]. Should you loop <= 1 or just < 1?
2. Given an array of 20 characters, the size is 20 but the only characters are [0..19] (count them; the first index is 0 and the last index is 19). Should you loop <= 20 or just < 20?
3. Given an array of 20 characters, if the user enters only 5 characters should you loop < 20 or < 5?
4. Do you remember when I wrote "isupper or isdigit (or any of the other <ctype.h> functions) are only safe to use on unsigned char values and EOF"?



In addition to those questions: Have you had a chance to look at K&R?
0 Eric Hines · January 18, 2016
Language Lama,

 To whom are you referring exactly?
0 Linguist Llama · January 18, 2016
Do you wonder why this code needs to be more efficient? Is it not processing passwords as quickly as it needs to? Unfortunately, you've missed the point of the exercise.

Are you trying to learn C? If so, then solve the exercises and move on.

Are you trying to learn how to improve the efficiency of a system? I strongly recommend taking a course on system analysis, if that's the case.
0 Eric Hines · January 17, 2016
So I've got a different project I'm working on. I'm using the boolean to turn the LED on/off. 

But instead of that I'd like for it to blink using a loop. 

I'd like to retain the ability to use a single button to start the loop and when pressed a second time to stop the loop.

void loop()
{
  if (irrecv.decode(&results))
  {
    if (results.value == buttonA)
   {
     if (millis() - last1 > 200) 
     {
      lightState1 =!lightState1;
      digitalWrite(led1,lightState1);
      last1 = millis();
      }
    }
          
    if (results.value == buttonB)
    {
      if (millis() - last2 > 200)
      {
      lightState2 =!lightState2;
      digitalWrite(led2,lightState2);
      last2 = millis();  
      }
    }

    if (results.value == buttonC)
      {
       if (millis() - last3 > 200) 
       {
       lightState3 =!lightState3;
       digitalWrite(led3,lightState3);
       last3 = millis();
       }
      }
irrecv.resume();  
  }
}
0 Laura Lee · January 17, 2016
There are far more efficient ways of writing the code Shlesh. But according to Linguistic Llama, I have implicitly signed an N.D.A in which I can not offer anything more than the statement above.
0 Shlesh Tiwari · January 14, 2016
Hi all, 
I am new to programming, so I would like to know if there is a more efficient way of writing the code. It works, but I was just wondering. 

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char password[20];
    int i;
    int upper = 0;
    int lower = 0;
    int num = 0;
    int special = 0;
    printf("Enter a password (max length 20) - Must contain at least one ");
    printf("upper case, lower case, number, and a special character - & , * or $\n");
    scanf(" %s", password);
    for (i=0;i<=20;i++){

        if ( isalpha(password)){
            if ( isupper(password)){
                printf("Upper case\n");
                upper++;
            }else{
                printf("Lower case\n");
                lower++;
            }
        }else if ( isdigit(password)){
            printf("Number\n");
            num++;
        }else if ( (password == '$')||(password == '&') || (password == '*')){
            printf("Special character\n");
            special++;
        }else{

        }
    }
    if ( upper && lower && num && special){
        printf("Password accepted");
    }else{
        printf("Invalid password");
    }
    return 0;
}

Thanks in advance. 
0 Linguist Llama · January 13, 2016
1. Given an array of 1 character, the size is 1 but the only character is array[0]. Should you loop <= 1 or just < 1?
2. Given an array of 20 characters, the size is 20 but the only characters are [0..19] (count them; the first index is 0 and the last index is 19). Should you loop <= 20 or just < 20?
3. Given an array of 20 characters, if the user enters only 5 characters should you loop < 20 or < 5?
4. Do you remember when I wrote "isupper or isdigit (or any of the other <ctype.h> functions) are only safe to use on unsigned char values and EOF"?

char password[3];
printf("Enter your password. (must be 3 characters, one uppercase, one lowercase, one number)");
scanf("%s",password);



This is wrong. When the user enters three characters, four characters will be written into password... but password can only store three. That's called a buffer overflow. Maybe your code doesn't crash now, but one day you'll be writing something complex and occasionally you'll notice a crash or some strange values... It'll also be nigh impossible to debug, because the code failure will likely occur at a strange, seemingly irrelevant place.

What's the fourth character?

This is a question that the resource you use should have explained already.

K&R 2E has existed for decades, and the people who read it don't tend to have this kind of basic misunderstanding. I'm not trying to be offensive. It's certainly not a book for beginners, but I think you'll get through it. Make sure it's the 2nd edition. Do the exercises as you come across them; don't move on leaving unfinished business. If you have any problems with the exercises, ask here. In fact, if you post your solution to the exercises here we can tell you how you went...

It's a thin book, only 189 pages excluding the reference at the end of the book, but it's dense information. Read it carefully; it tends to take people months to soak it all in.

If cost is an issue, see if you can borrow it from a library. If that's not an option, ask here and we'll see if we can, as a community, find the most accurate, freely available resource on the internet.

Let's raise the bar!
0 Eric Hines · January 13, 2016
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>

int main()
{
int a,b,c;
int i;
char password[5];

printf("enter password. must contain upper letter, number and, $ sign");
scanf(" %s", password);

for(i=0; i<=5; i++)
{
    if(isupper(password))
    {
        a=1;
    }
    if(isdigit(password))
    {
        b=1;
    }
    if(isdigit(password))
    {
        c=1;
    }
}
if(a==1&&b==1&&c==1)
{
    printf("thank you");
}
else
{
    printf("try again");
}
return 0;
}



It's working now.... how frustrating
0 Eric Hines · January 13, 2016
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>

int main()
{
char password[3];
int i=0;
int upper=0;
int num=0;
int dollarsign=0;

printf("Enter your password. (must be 3 characters, one uppercase, one lowercase, one number)");
scanf("%s",password);


for(i=0;i<=3;i++)
{
    if(isupper(password))
    {
        upper=1;
    }
    if(isdigit(password))
    {
        num=1;
    }
    if(isdigit(password=='$'))
    {
        dollarsign=1;
    }
}
if(upper==1 && num ==1 && dollarsign ==1)
{
    printf("Thank you for entering your password. \n");
}
else
{
    printf("Your password does not meet the minimum requirements. \n");
}
return 0;
}


I thought I wrote it correctly, but I'm just getting the same thing; your password doesn't meet the min requirements. The password I entered was A1$
0 Eric Hines · January 13, 2016
Thanks again,

 I do agree with you; there IS a lot I don't know and perhaps finding another source for answers may be best (perhaps even a class) because a lot of my questions are too basic. I'd just end up chasing a white rabbit around and would end up annoying you, so perhaps something a bit more structured like a class would be appropriate.

 And as you mentioned also, one answer just leads to more questions. I may just keep doing as much research on my own as possible and if I'm just absolutely stuck I could resort to the forums. Or maybe if I need help with a general idea of how something works I could stop by here. 


......by the way. I think I've found out the answer of how to go about (at least one way) checking each password character to verify the conditions which I set are met. I have seen a lot of other people using for loop with nested ifs. This is what I'm seeing and this is my interpretation of why it works. (my main concern is whether I'm understanding the a++ rule......

a=0;
for(password[a]; a<=20 ; a++ )                                 
{                                                        
if(isupper(password[a]))         
{                                                    
upperflag = 1; 

.

.

.

.etc                            
                                                     
    it's saying; begin checking with the first character of the password, end with the last character of the password, after each circuit of the loop, increment by one to check the next character in line. In other words, am I right that the for loop can only check one character at a time and it must see if it meets one of the if statements? And if it does, it is assigned a value of 1 which when tested later on and a

 "there are books that have stood the test of time"

 are there any you recommend for someone like me who is just beginning?
  • 1
  • 2

C

107,049 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator