-1

I'm super new to programming and I'm trying to figure out why my program isn't printing out invalid name when I am typing something that is not a-z and A-Z.

Here is my code

#pragma warning(disable : 4996)
#include <stdio.h>

int main()
{

    char getFirstNameFromApplicant[50]; //declare variable

    printf("Please enter your first name: \n"); 
    scanf("%s", &getFirstNameFromApplicant); //get user input

    bool firstNameIsValid = true; //true or false statement
    for (int i = 0; getFirstNameFromApplicant[i] != '\0'; i++) { // int i = 0 means position of index, var[i] is the ith position, != '0' will be the end, i++ increments counter
        if ((getFirstNameFromApplicant[49] >= 'a' == 0 && getFirstNameFromApplicant[49] <= 'z' == 0) || (getFirstNameFromApplicant[49] >= 'A' == 0 && getFirstNameFromApplicant[49] <= 'Z' == 0)) // var is greater or equal to 'a' or higher is false etc...
            firstNameIsValid = false;
        break;
    }
    if (firstNameIsValid = true)
        printf("name is valid");
    else
        printf("name is invalid");
}

any help is appreciated.

I tried all sorts of approaches for true and false and moving things around but I just can't find the right answer.

Retired Ninja
  • 4,785
  • 3
  • 25
  • 35
Phi Tran
  • 9
  • 1
  • 2
    `scanf("%s", &getFirstNameFromApplicant);` should be `scanf("%s", getFirstNameFromApplicant);` without the `&`. What is the loop for if you're only checking the last character of the array every time? Seems like `getFirstNameFromApplicant[i]` should be used in there. The [isalpha](https://en.cppreference.com/w/c/string/byte/isalpha) function would also be helpful. – Retired Ninja Feb 22 '23 at 06:56
  • Does this answer your question? [Determine if char is a num or letter](https://stackoverflow.com/questions/8611815/determine-if-char-is-a-num-or-letter) – Abderrahmene Rayene Mihoub Feb 22 '23 at 07:05
  • i considered the isalpha function but the professor hasn't taught us that part yet so I avoided using it. Thank you for your help! – Phi Tran Feb 22 '23 at 07:28
  • You could always write your own function to check if the character is alphabetic or not. It makes the code easier to read if you have something like `if (!isAlphabetic(variable)) { ... }` instead of a bunch of different conditions. In this case since you're only checking in one place it may not help a lot, but if you expanded your program adding utility functions like that can help a lot. Easier to read and you only have to debug the function once. :) – Retired Ninja Feb 22 '23 at 19:32

2 Answers2

0

Take a look into the complete code here

#include <stdio.h>

int main() {
  char c;
  printf("Enter a character: ");
  scanf("%c", &c);

  if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
    printf("%c is a letter.\n", c);
  } else {
    printf("%c is not a letter.\n", c);
  }

  return 0;
}
0

I would suggest some iprovements over the answer above.

  1. Use is alpah from ctype.h to evaluate wether the char is a letter or not.
  2. Limit the length of input from scanf.You can do this by adding the number of characters you want the function to read. This will help prevent buffer overflow accidents.
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h> /* isalpha */

int main() 
{
  char getFirstNameFromApplicant[50] = {0};
  printf("Please enter your first name: \n");
  scanf("%49s", getFirstNameFromApplicant); /* notice the %49s, which limits it to 49 chars to allow a place for '\0' */

  bool firstNameIsValid = true;
  for (int i = 0; getFirstNameFromApplicant[i] != '\0'; i++) 
  {
    if (!isalpah(getFirstNameFromApplicant[i])) 
    {
      firstNameIsValid = false;
      break;
    }
  }

  if (firstNameIsValid)
    printf("name is valid");
  else
    printf("name is invalid");

  return 0;
}
Uriel.Gi
  • 69
  • 1
  • 6