0

Getting issue while solving this problem for assignment of C programming:

Need to create an app for college where there are 500 undergraduate students from which 250 are in School of Engineering (SOE) and 250 are in School of Science (SOE) , where suppose if a student belongs to Computer engineering (CE) batch 2020_21: supposed output needs too look like: SOEUNGCE0001 - SOEUNGCE0040

departments in school of engineering are:

  1. chemical engineering -CHE
  2. computer engineering -CE
  3. mechanical engineering- ME
  4. Environmental engineering -ENE
  5. Electrical Engineering - EE

and in school of science:

  1. Architecture -AR
  2. computer science-CS
  3. Biotechnology-BT
  4. Pharmacy-PH

I tried printing with switch case statements but it's wasn't working with strings. also the below program is having some issue i.e. it doesn't take some inputs. HELP ME SOLVE THIS.

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

typedef struct{
    char name;
    int roll_no;
    char department;
    char school;
}students;

int main()
{
    students student;

    //inputs
    printf("Enter Name : " );
    scanf("%s", &student.name);
    printf("\nRoll Number : ");
    scanf("%d", &student.roll_no);
    printf("\nDepartment : ");
    scanf("%s",&student.department);
    printf("\nSchool : ");
    scanf("%s",&student.school);

    if (student.department == 'Chemical')
    {
        printf("Name : %s \t",student.name);
        
        printf("\n%sUNG%s00%d", student.school, "CHE", student.roll_no);
    }

    return 0;
}
TheNisham
  • 13
  • 2
  • 4
    `student.department == 'Chemical'`: That's not how you compare strings in C, you can use `strcmp()` for example to compare two strings. Also string literals in C should be within `" "`. – JASLP doesn't support the IES Aug 05 '21 at 15:13
  • 1
    *it's wasn't working with strings* - `char name;` etc are not strings. – Weather Vane Aug 05 '21 at 15:14
  • why can't I give full name as input and how do I use switch case for (string) department and school to give unique registration number as output? – TheNisham Aug 06 '21 at 07:02
  • when compiling, always enable the warnings, then fix those warnings. ( for `gcc`, at a minimum use: -Wall -Wextra -Wconversion -pedantic -std=gnu11 ) Note: other compilers use different options to produce the same results. The following lines do not compile: `if (student.department == 'Chemical') { printf("Name : %s \t",student.name); printf("\n%sUNG%s00%d", student.school, "CHE", student.roll_no);` – user3629249 Aug 06 '21 at 22:20
  • @TheNisham If you want to have input with spaces, use `fgets()` instead (or you can also use `scanf("%[^\n]",buf);`, but it's better to use `fgets()` since it has buffer overflow protection). You cannot use switch with strings in C. – JASLP doesn't support the IES Aug 07 '21 at 03:55

2 Answers2

1

You have 3 mistakes:

  1. Logical Error: since name, department and school are strings, so they must be arrays of characters i.e char name[100]. So the structure should be modified as the following:
typedef struct{          
    char name[100];      
    int roll_no;         
    char department[100];
    char school[100];    
}students;               

2- After editing the structure you will get warning from the compiler since scanf takes the address of the element to be scanned and student.name points to the first character of the string name. So Edit the scanf arguments to be :

scanf("%s", student.name); 
.
scanf("%s", student.department); 
.
scanf("%s", student.school);  

Note: You can also use fgets instead of scanf to avoid buffer overflow but you have to consider that fgets includes the newline character, \n, within the string, so you have to replace the \n with \0. See tricks for how to replace the \n with \0

3- The last error is in comparing strings, you should use the strcmp function to compare between two strings, the function returns 0 if the two strings are typical. Also the string literals sholud be put inside double quotation marks "". So you should modify the last block to be :

if (!strcmp(student.department, "Chemical"))                        
{                                                                   
    printf("Name : %s \t",student.name);                            
                                                                    
    printf("\n%sUNG%s00%d", student.school, "CHE", student.roll_no);
}                                                                   

NAND
  • 663
  • 8
  • 22
0

Try something like that:

typedef struct{
    char name[100];
    int roll_no;
    char department[100];
    char school[100];
} students;

int main() {
    students student;
    char input[100];
    //inputs
    printf("Enter Name : " );
    fgets(student.name, 50, stdin);
    printf("\nRoll Number : ");

    fgets(input, 50, stdin);
    sscanf(input,"%d", &student.roll_no);

    printf("\nDepartment : ");
    fgets(student.department, 50, stdin);
    printf("\nSchool : ");
    fgets(student.school, 50, stdin);

    if (strcmp(student.department, "Chemical") == 0)
    {
        printf("Name : %s \t",student.name);

        printf("\n%s %d", student.school, student.roll_no);
    }
    return 0;
}
G. Emadi
  • 230
  • 1
  • 8
  • why can't I give full name as input and how do I use switch case for (string) department and school to give unique registration number as output? – TheNisham Aug 06 '21 at 07:00
  • 1
    I updated the code by changing `scanf`! `scanf` does not handle space properly. For switch case because your string is dynamic it is better to handle it with if-else. You can see this https://stackoverflow.com/questions/4014827/how-can-i-compare-strings-in-c-using-a-switch-statement too. @TheNisham – G. Emadi Aug 06 '21 at 07:28
  • 1
    @G.Emadi Do NOT forget that `fgets` includes the new line character within the string. so if the user entered `Chemical` the `\n` will be included. So `Chemical\n` is not equal to `Chemical`. – NAND Aug 07 '21 at 02:55