-1

Below is my C code, one problem is that when I try to run it, it wont continue to printf the next statement after the switch and after I put in the input. It's a lecture hall/room booking system.If you saw, there is step 3 after the if else switch statement. It wont print out the step 3.

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

int main()
{
    char applicant_name[100], applicant_faculty[100], applicant_date[40], applicant_id[50], applicant_mobile_number[20];
    int number_of_students, lecture_hall_book, lecture_room;
    char lecture_hall[20],  booking_date[20];


    printf("\n=======================================================================================\n");
    printf("                                Author : Fathul Amin\n");
    printf("                                Matric ID : CB19070\n");
    printf("=======================================================================================\n\n");
    printf("                             Universiti Malaysia Pahang\n\n");
    printf("     Welcome to the Faculty of Computing (FCOM) Lecture Hall/Room Booking System\n\n");
    printf("               For your information, this system was created to support\n");
    printf("             government initiative to reduce paper usage by replacing the \n");
    printf("                   manual application form to a computerized system \n\n\n");
    printf("                      There will be 3 STEP to apply for a booking\n\n");
    printf("\t [STEP 1] Application Information\n");
    printf("\t [STEP 2] Determine Lecture Hall/Room based on Number of Students\n");
    printf("\t [STEP 3] Lecture Hall/Room Booking Date\n\n");
    printf("\t [STEP 4] Lecture Hall/Room Booking Time\n\n");
    printf("\n=======================================================================================");
    printf("\n                [STEP 1]  Below is an example of information needed");
    printf("\n=======================================================================================\n\n");
    printf("      APPLICANT INFORMATION\n\n");
    printf("       NAME   : _____________               STAFF/STUDENT ID : ______________\n\n");
    printf("      FACULTY : _____________                 MOBILE NUMBER  : ______________\n\n");
    printf("       DATE   : _____________ (xx/xx/xxxx)\n");
    printf("\n=======================================================================================\n\n");
    printf("                         Enter Your Name  : ");
    fgets(applicant_name, 100, stdin);
    printf("                      Enter Your Faculty  : ");
    fgets(applicant_faculty, 100, stdin);
    printf("                      Enter Today's Date  : ");
    fgets(applicant_date, 40, stdin);
    printf("             Enter Your Staff/Student ID  : ");
    fgets(applicant_id, 50, stdin);
    printf("                Enter Your Mobile Number  : ");
    fgets(applicant_mobile_number, 20, stdin);
    printf("\n\n=======================================================================================\n");
    printf("                             [STEP 2]  Number of Students");
    printf("\n=======================================================================================\n\n");
    printf("\t\t\t      Enter Number of Students: ");
    scanf("%i", &number_of_students);
    printf("\n=======================================================================================\n\n");
    if(number_of_students > 50 && number_of_students<101){
        printf("\t\tLecture Hall :  [1] YDK1\n");
        printf("\t\t\t\t[2] YDK2\n");
        printf("\t\t\t\t[3] YDK3\n");
        printf("\t\t\t\t[4] YDK4\n");
        printf("\t\t\t\t[5] YDK5\n\n");
        printf("     Which Lecture Hall do you want to book? (1-5): \n");
        scanf("%i", &lecture_hall_book);
        switch(lecture_hall_book){
        case 1 : printf("YDK1 Confirmed");
                 strcpy(lecture_hall, "XBK1");
                 break;
        case 2 : printf("YDK2 Confirmed");
                 strcpy(lecture_hall, "XBK2");
                 break;
        case 3 : printf("YDK3 Confirmed");
                 strcpy(lecture_hall, "XBK3");
                 break;
        case 4 : printf("YDK4 Confirmed");
                 strcpy(lecture_hall, "XBK4");
                 break;
        case 5 : printf("YDK5 Confirmed");
                 strcpy(lecture_hall, "XBK5");
                 break;
        }
    } else if(number_of_students<50) {
        printf("\t\t\tLecture Room :   [1] XBK1\n");
        printf("\t\t\t\t\t [2] XBK2\n");
        printf("\t\t\t\t\t [3] XBK3\n");
        printf("\t\t\t\t\t [4] XBK4\n");
        printf("\t\t\t\t\t [5] XBK5\n");
        printf("\t\t\t\t\t [6] XBK6\n");
        printf("\t\t\t\t\t [7] XBK7\n");
        printf("\t\t\t\t\t [8] XBK8\n");
        printf("\t\t\t\t\t [9] XBK9\n");
        printf("\t\t\t\t\t[10] XBK10\n\n");
        printf("  Which Lecture Room do you want to book? (1-10): ");
        scanf("%i", &lecture_hall_book);
        switch(lecture_hall_book){
        case 1 : printf("\n\n\t\t\t\t       XBK1 CONFIRMED");
                 strcpy(lecture_room, "XBK1");
                 break;
        case 2 : printf("\n\n\t\t\t\t       XBK2 CONFIRMED");
                 strcpy(lecture_room, "XBK2");
                 break;
        case 3 : printf("\n\n\t\t\t\t       XBK3 CONFIRMED");
                 strcpy(lecture_room, "XBK3");
                 break;
        case 4 : printf("\n\n\t\t\t\t       XBK4 CONFIRMED");
                 strcpy(lecture_room, "XBK4");
                 break;
        case 5 : printf("\n\n\t\t\t\t       XBK5 CONFIRMED");
                 strcpy(lecture_room, "XBK5");
                 break;
        case 6 : printf("\n\n\t\t\t\t       XBK6 CONFIRMED");
                 strcpy(lecture_room, "XBK6");
                 break;
        case 7 : printf("\n\n\t\t\t\t       XBK7 CONFIRMED");
                 strcpy(lecture_room, "XBK7");
                 break;
        case 8 : printf("\n\n\t\t\t\t       XBK8 CONFIRMED");
                 strcpy(lecture_room, "XBK8");
                 break;
        case 9 : printf("\n\n\t\t\t\t       XBK9 CONFIRMED");
                 strcpy(lecture_room, "XBK9");
                 break;
        case 10 : printf("\n\n\t\t\t\t      XBK10 CONFIRMED");
                  strcpy(lecture_room, "XBK10");
                  break;
        default : printf("You didnt enter anything");
        }
    } else {
        printf("            There is no Lecture Hall that can fit students more than 100\n");
        printf("         Maximum is 100 students for a lecture hall and 50 for a lecture room");
      }
    if()
    printf("\n=======================================================================================\n\n");
    printf("\n                     [STEP 3]  Lecture Hall/Room Booking Date");
    printf("\n=======================================================================================\n\n");
    printf("                           Enter Booking Date: ");
    fgets(booking_date, 20, stdin);
    printf("\n\n=======================================================================================\n");
    printf("                       [STEP 4]  Lecture Hall/Room Booking Time");
    printf("\n=======================================================================================\n\n");
    printf("\n\t\t\t\tEXAMPLE (24 Hour Format)");
    printf("\n\t\t\t\t     From : 0830");
    printf("\n\t\t\t\t       To : 1030");
    printf("\n\t\t\t\t   08 ---> Hours In");
    printf("\n\t\t\t\t   30 ---> Hours Out");
    printf("\n\t\t\t\t   10 ---> Minutes In");
    printf("\n\t\t\t\t   30 ---> Minutes Out");
    return 0;
}
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
MUFAMIN
  • 11
  • 1
  • 1
    `if()` looks like an error/typo. `It wont print out the step 3.` please post the input you are providing to your program, show the output you are getting from your program and please show the output you were expecting to get from your program. – KamilCuk Feb 19 '20 at 13:29
  • 4
    Welcome to SO. For a minimum example to demonstrate your problem you could cut most of your prints and most of your cases. – Gerhardh Feb 19 '20 at 13:29
  • Which compiler/environnement/IDE do you use? Is it TurboC? – Jabberwocky Feb 19 '20 at 13:45
  • Please create an MCVE ([Minimal, Complete, Verifiable Example](https://stackoverflow.com/help/mcve)) (or MRE or whatever name SO now uses) or an SSCCE ([Short, Self-Contained, Correct Example](http://sscce.org/)). There is a lot of code there that is simply not necessary to demonstrate your problem — most of the `printf()` statements, in particular. – Jonathan Leffler Feb 19 '20 at 14:44
  • 'Tis odd that you confirm a `Y` lecture hall but copy an `X` lecture hall into the chosen lecture hall: `printf("YDK1 Confirmed"); strcpy(lecture_hall, "XBK1");` — be careful when you copy'n'paste. You could use an array instead of a switch, or you could format the information from the number and the prefix. – Jonathan Leffler Feb 19 '20 at 14:48

1 Answers1

6

The lecture_room variable is of type int. The strcpy(...) function expects char buffer as destination. You have provided int instead, so strcpy(...) will write strings into location with memory address equal to the lecture_room value. Thus, into random memory location, leading to random effects (e.g. stackoverflow, values overwrite and so on).

Dr. Andrey Belkin
  • 795
  • 1
  • 9
  • 28
  • 2
    Well caught! I believe the compiler should have flagged this as a warning. The OP should turn warnings of his compiler on. – Paul Ogilvie Feb 19 '20 at 13:35
  • 3
    Passing an `int` to a function expecting a `char*` isn't valid C. A diagnostic message by the compiler is mandatory. And if despite that, the compiler produced some binary goo out of that code, it's a bad compiler. What the binary goo will do once executed is anyone's guess. In case of gcc specifically, make sure to use it in C language mode `gcc -std=c11 -pedantic-errors`. It's a bad compiler until you don't tell it to behave - when it does, it's the best compiler on the market. – Lundin Feb 19 '20 at 13:44
  • @PaulOgilvie A compiler _must_ give a message for this C language violation, regardless of warning settings. – Lundin Feb 19 '20 at 13:46
  • @Lundin, my old mental C compiler model says you can pass anything. Need to upgrade my brain. However, compiler can only give message if prototypes have ben provided. Otherwise compiler is in the blind. Or will compiler require prototypes? – Paul Ogilvie Feb 19 '20 at 13:53
  • Summary here: [“Pointer from integer/integer from pointer without a cast” issues](https://stackoverflow.com/questions/52186834/pointer-from-integer-integer-from-pointer-without-a-cast-issues). – Lundin Feb 19 '20 at 14:08