Thread: compiler problem or program problem

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    7

    compiler problem or program problem

    I am using both Xcode(Mac book) and DEV C++.
    One program is no problem in DEV C++, but some problem in Xcode.
    As a command auto executed in Xcode only.


    I simplified for this program to try to find what the problem is.

    This program purpose is the calculation for booking the signal room or double room, staying for the number of nights and number of people lived.

    [CODE]

    #include <stdio.h>


    int main() {

    int number; /* How many people are booked the hotel. */
    char room; /* Room type both D (double room) or S (single room) */

    /* Read a number of people who needed to book the hotel. The number of peoples
    are not accepted negative value */

    room =0;
    number =0;

    while(1){

    printf("Enter the number of people:");
    scanf("%d", &number);
    fflush(stdin);

    if(number < 1){
    printf("Input not correct\n");
    continue;
    }
    break;
    }

    /* Verify the selection of room type both are 'D' or 'S',
    The entry must be 'D' or 'C', otherwise, the program will be looped.*/
    while(1){

    printf("Enter the room type:");
    scanf("%c", &room);

    if(room != 'S' && room !='D'){

    printf("You have entered a wrong room type!\n");
    continue;
    }

    break;
    }

    return 0;
    }
    [
    /CODE]


    Following is a full version :

    Code:
    #include <stdio.h>
    
    
    int main() {
    int nights; /* How many nights will be booked by each people or a pair. */
    int number; /* How many people are booked the hotel. */
    char room; /* Room type both D (double room) or S (single room) */
    int nightsTotal; /* The total night will be booked that calculated by the amount
    of people, type of room and how many nights will stay.*/
    int rooms; /* The total of the room are needed */
        
    /* Read a number of people who needed to book the hotel. The number of peoples
    are not accepted negative value */
        
        
    rooms =0;
    rooms =0;
    number =0;
        
    while(1){
            
    printf("Enter the number of people:");
    scanf("%d", &number);
    fflush(stdin);
            
    if(number < 1){
    printf("Input not correct\n");
    continue;
    } 
    break;
    }
        
        
        
    /* Verify the selection of room type both are 'D' or 'S',
    The entry must be 'D' or 'C', otherwise, the program will be looped.*/
    while(1){
            
    printf("Enter the room type:");
    scanf("%c", &room);
            
    if(room != 'S' && room !='D'){
                
    printf("You have entered a wrong room type!\n");
    continue;
    }
            
    break;
    }
        
    /* Read the number of nights will be booked, at lease 1 night */
    while(1){
            
    printf("Enter the number of nights:");
    scanf("%d", &nights);
    fflush(stdin);
            
    if(nights >=1)
    break;
    }
        
    printf("Room booking received.");
    fflush(stdin);
        
    /* calculation of the nights are needed for a single room or double,
    Single room is booked only for one people, but one people can be bookedthe double room, as some people who like living in a double room alone.*/
    if (room == 'S'){
    rooms =1 * number;
    }
    else if(room == 'D'&& number >2)
    rooms = (number / 2)+(number % 2);
        
    nightsTotal = nights * rooms;
        
    /* printout the total night for all booked room; e.g.: 3 people live in double room
    1 night, that will be displayed 2 nights.*/
    printf("Room type %c for %d people for %d nights.", room, number, nightsTotal);
        
        
    return 0;
    }




    thanks
    Attached Images Attached Images compiler problem or program problem-2018-11-28-10-43-42-jpg 
    Last edited by kelu; 11-28-2018 at 03:43 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Read the FAQ on why fflush stdin is bad, and how to do it properly so you're not dependent on implementation specific effects.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code problem or compiler problem....?
    By miloki in forum C Programming
    Replies: 4
    Last Post: 03-05-2015, 12:48 AM
  2. weird problem, compiler problem?
    By sharonch in forum C++ Programming
    Replies: 1
    Last Post: 05-27-2014, 12:08 AM
  3. Compiler program problem
    By shellback in forum C Programming
    Replies: 9
    Last Post: 03-19-2011, 03:58 AM
  4. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  5. Dev c++ compiler problem
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 04-17-2002, 08:26 AM

Tags for this Thread