0

Our corporation office requires an application which will maintain all registered Universities in Chennai and that application should be user friendly in terms of searching a University. Create a structure called “University” with the following attributes: name, license number and area code.

Requirement: License number for a university should be 6 digits and the first 2 digits must be alphabets of Upper case letters and last 4 digits must be number.

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

struct University
{
  char name[100];
  char license[10];
  int area;

}u[10];

void main()
{
  int i, n, r, k = 0, flag = 1, f2 = 1, j, search = 0;
  char s[100];
  printf("Enter the number of records\n");
  scanf("%d", &n);
  printf("Enter the details of %d universities\n", n);

  for (i = 0; i<n; i++)
  {
    printf("Name of the University\n");
    getchar();
    scanf("%s", u[i].name);
    j = strlen(u[i].name);
    if (j <= 1)
    {
      f2 = 0;
      break;
    }
    printf("License Number\n");

    scanf("%s", u[i].license);
    k = strlen(u[i].license);
    if (k<1)
    {
      f2 = 0;
      break;
    }

    if (k<6)
    {
      flag = 0;
    }
    else if ((u[i].license[0] >= 'A' && u[i].license[0] <= 'Z') && (u[i].license[1] >= 'A' && u[i].license[1] <= 'Z') && (u[i].license[2] >= '0' && u[i].license[2] <= '9') && (u[i].license[3] >= '0' && u[i].license[3] <= '9') && (u[i].license[4] >= '0' && u[i].license[4] <= '9') && (u[i].license[5] >= '0' && u[i].license[5] <= '9') && k == 6)
    {
      flag = 1;
    }
    else
    {
      flag = 0;
    }
    printf("Area Code\n");
    scanf("%d", &u[i].area);
    //printf("%d",u[i].area);
    if (u[i].area <= 0)
    {
      f2 = 0;
    }

  }
  if (flag == 0)
  {
    printf("Sorry! You have entered incorrect license number.");
  }
  else if (f2 == 0)
  {
    printf("Unable to continue");
  }
  else
  {
    printf("Enter the name of the University to be searched\n");
    scanf("%s", s);
    for (i = 0; i<n; i++)
    {
      if ((strcmp(u[i].name, s)) == 0)
      {
        search = 1;
      }
    }
    if (search == 1)
    {
      printf("University is licensed one.");
    }
    else
    {
      printf("University is not found.");
    }
  }
}

when I give number of university as 3, then it did not take input for the 3rd university.

Test Case

Input 1

Enter the number of records

3

Enter the details of 3 universities

Name of the University

SRM

License Number

SR1234

Area Code

28

Name of the University

University of Madras

License Number

SP0904

Area Code

18

Name of the University

Bharath University

License Number

BU0101

Area Code

35

Enter the name of the University to be searched

SRM

Output 1

University is licensed one.

  • Please show some examples of input and expected/actual output. – Jabberwocky Aug 07 '17 at 12:20
  • 4
    The whole program logic is wrong and overly complicated. – Jabberwocky Aug 07 '17 at 12:28
  • 1
    Reading input from the user with `scanf` has limited use, because it ignores new-lines. It requires more error checking. Especially, the `%d` format will not consume the stream if the next token isn't a number and the `%s` format will only read strings up to the next space. If your input is "New Delhi", "AA9876" and "123", then the name is "New", the licence is "Delhi" and the area won't be read at all. – M Oehm Aug 07 '17 at 12:36
  • Lose all the flags and instead use regex. – maelswarm Aug 07 '17 at 12:45
  • just tell me how to read the strings with space in a structured array, because i face this problem many times – Prakhar Dubey Aug 07 '17 at 13:06
  • why can't you use `fgets()` that will do the trick? – danglingpointer Aug 07 '17 at 13:21
  • Please, just learn to use google. To find which this question is a duplicate of I googled: "stackoverflow c input string with space" and found many hits for C and C++. You could have tried at least: "c input string with space" and would have got similar hits... – Scheff's Cat Aug 07 '17 at 13:21
  • @scheff i learned it but after using fgets(), u will see the same error in the program – Prakhar Dubey Aug 07 '17 at 15:20

1 Answers1

1

Seems like you are interested in just reading a c-string containing a space. To do that you can use fgets. Here is a toy program:

#include <stdio.h>
#include <string.h>

struct s {
    char name[100];
    int something;
};

int main(void)
{
    struct s myStruct;
    printf("%s", "Enter name: ");
    fgets(myStruct.name, 100, stdin);

    myStruct.name[strlen(myStruct.name) - 1] = '\0'; //This should remove the newline char at the end
    printf("Name is: %s", myStruct.name);
}
babon
  • 3,615
  • 2
  • 20
  • 20