program gets stuck at random function

This is a discussion on program gets stuck at random function within the C++ Programming forums, part of the General Programming Boards category; dear online friends, please check out this hotel management program and advise what i should do to get the output ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    7

    program gets stuck at random function

    dear online friends,
    please check out this hotel management program and advise what i should do to get the output after the random func.
    all suggestions are welcome

    Code:
    #include<iostream.h>
    #include<stdio.h>
    #include<iomanip.h>
    #include<string.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<time.h)
    struct customer
    {int pr;
    char fname[20];
    char lname[20];
    char nation[15];
    long mob;
    long totalbill;
    };
    void randm (int y)
    {
    int num[20], u=y*100,flag=0,w;
    long z;
    for (w=0;w<20;w++)
    {
    do{
    z=rand()%(20);
    z+=u;
    for(int v=0;v<=u;v++)
    if (z==num[v])
    flag=1;
    }while(flag!=0);
    }
    if(flag==0)
    {num[u]=z;
    cout<<"room number is "<<z<<endl;
    }
    }
    void main()
    {clrscr();
    customer cs[200];
    int i,h,d,t,r,e,f,b,x,g;
    cout<<"starting program"<<endl;
    cout<<"enter 1 to start booking"<<endl;
    cout<<"enter 3 to exit"<<endl;
    cin>>x;
    cout<<endl;
    do{
    for(i=0;i<200;i++)
    {
    cs[i].totalbill=0;
    cout<<"__________________________________________________________________________"<<endl;
    cout<<"                                      Welcome to Hotel Naraveru             "<<endl;
    cout<<"___________________________________________________________________________"<<endl;
    cout<<"Get ready to enter details"<<endl;
    cout<<"What type of booking do u want to make?"<<endl;
    cout<<"enter 1 for party hall"<<endl;
    cout<<"enter 2 for room booking"<<endl;
    cout<<"enter 3 to exit"<<endl;
    cin>>cs[i].pr;
    switch (cs[i].pr)
    {case 1:cout<<"enter first name"<<endl;
    gets (cs[i].fname);
    cout<<"enter last name"<<endl;
    gets (cs[i].lname);
    cout<<"enter the mobile number "<<endl;
    cin>>cs[i].mob;
    cout<<endl;
    cout<<"enter 1 for grand hall costts 1050 for one day"<<endl;
    cout<<"enter 2 for small hall costs 550 for one day "<<endl;
    cin>>h;
    if(h==1)
    {cout<<"please enter number of days you want the hall for"<<endl;
    cin>>d;
    cout<<endl;
    cs[i].totalbill=d*1050;
    cout<<"do you  want beverages from the hotel costs 150 enter 1 for yes or 2 for no"<<endl;
    cin>>b;
    cout<<endl;
    if (b==1)
    {t=150*d;
    cs[i].totalbill+=t;
    }
    cout<<"booking confirmed"<<endl;
    cout<<"dear customer so total bill=  "<<cs[i].totalbill<<endl;
    }
    if (h==2)
    {cout<<"please enter number of days you want the hall for "<<endl;
    cin>>d;
    cout<<endl;
    cs[i].totalbill=d*550;
    cout<<"do you want beverage from the hotel costs 150 enter 1 for y or 2 for n"<<endl;
    cin>>b;
    cout<<endl;
    if(b==1)
    {t=b*d;
    cs[i].totalbill+=t;
    }
    cout<<"booking confirmed"<<endl;
    cout<<"dear customer so total bill=  "<<cs[i].totalbill<<endl;
    }
    break;
    case 2:cout<<"enter first name"<<endl;
    gets (cs[i].fname);
    cout<<endl;
    cout<<"enter last name"<<endl;
    gets (cs[i].lname);
    cout<<endl;
    cout<<"enter mobile number"<<endl;
    cin>> cs[i].mob;
    cout<<endl;
    cout<<"enter nationality "<<endl;
    gets (cs[i].nation);
    cout<<endl;
    cout<<" enter 1 for standard room costs 850 per day"<<endl;
    cout<<" enter 2 for deluxe room costs 1250 per day"<<endl;
    cout<<" enter 3 for super deluxe room costs 2250 per day"<<endl;
    cout<<" enter 4 for king room costs 4500 per day"<<endl;
    cout<<"enter the number for your choice of room "<<endl;
    cin>>r;
    switch(r)
    {case 1: cout<<"so you have chosen standard room"<<endl;
    cout<<"enter number of days"<<endl;
    cin>>d;
    cout<<endl;
    cs[i].totalbill= d* 850;
    cout<<"enter the floor number 1,2,3 or 4 "<<endl;
    cin>>e;
    cout<<endl;
    srand(unsigned(time(NULL)));
    randm(e);
    break;
    case 2:cout<<"so you have chosen deuluxe room"<<endl;
    cout<<"enter number of days"<<endl;
    cin>>d;
    cout<<endl;
    cs[i].totalbill=d*1250;
    cout<<"enter the floor number 5 or 6"<<endl;
    cin>>e;
    cout<<endl;
    srand(unsigned(time(NULL)));
    randm(e);
    break;
    case 3:cout<<"so you have chosen super deluxe room"<<endl;
    cout<<"enter number of days"<<endl;
    cin>>d;
    cout<<endl;
    cs[i].totalbill=d*2250;
    cout<<"enter the floor number 7 or 8"<<endl;
    cin>>e;
    cout<<endl;
    srand(unsigned(time(NULL)));
    randm(e);
    break;
    case 4:cout<<"so you have chosen king room"<<endl;
    cout<<"enter number of days"<<endl;
    cin>>d;
    cout<<endl;
    cs[i].totalbill=d*4500;
    cout<<"enter the floor number 9 or 10"<<endl;
    cin>>e;
    cout<<endl;
    srand(unsigned(time(NULL)));
    randm(e);
    break;
    default:cout<<"Please enter a valid choice"<<endl;
    }
    cout<<"do you want swimming pool and spa service costs 350 per day (1 for y / 2 for n)?"<<endl;
    cin>>b;
    cout<<endl;
    if (b==1)
    {t=350*d;
    cs[i].totalbill+=t;
    }
    cout<<"do you want television service costs 100 (1 for y/ 2 for n)?"<<endl;
    cin>>b;
    cout<<endl;
    if (b==1)
    {t=100*d;
    cs[i].totalbill+=t;
    }
    cout<<"enter 1 for only breakfast costs 50"<<endl;
    cout<<"enter 2 for only brakfast and dinner costs 100"<<endl;
    cout<<"enter 3 for whole package costs 150"<<endl;
    cin>>f;
    cout<<endl;
    switch(f)
    { case 1:cout<<"You have chosen the first option"<<endl;
    t=50*d;
    cs[i].totalbill+=t;
    break;
    case 2:cout<<"You have chosen the second option"<<endl;
    t=100*d;
    cs[i].totalbill+=t;
    break;
    case 3:cout<<"You have chosen the third option"<<endl;
    t=150*d;
    cs[i].totalbill+=t;
    break;
    default:cout<<"please enter a valid choice "<<endl;
    }
    cout<<"Last but not the least do u want  taxi service  costs 75 per day (1 for y/ 2 for n)?"<<endl;
    cin>>b;
    cout<<endl;
    if(b==1)
    {t=75*d;
    cs[i].totalbill+=t;
    }
    cout<<"your booking has been confirmed"<<endl;
    cout<<"here are the details"<<endl;
    cout<<"name:-"<<cs[i].fname<<cs[i].lname<<endl;
    cout<<"mobile number is"<<endl;
    cout<<cs[i].mob;
    cout<<"and your room type is:-"<<endl;
    if(r==1)
    cout<<"standard room"<<endl;
    if(r==2)
    cout<<"deluxe room"<<endl;
    if(r==3)
    cout<<"super deluxe room"<<endl;
    if(r==4)
    cout<<"king room"<<endl;
    cout<<"Dear customer your total bill is"<<cs[i].totalbill<<endl;
    cout<<"Please do come again after this visit we will be more than happy to entertain you"<<endl;
    break;
    case 3:exit(0);
    default : cout<<"Please enter a valid choice"<<endl;
    }
    }
    }while(x!=3);
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2,421
    my first bit of advice is to indent your code in a way that makes it more readable. as an example:
    Code:
    #include <iostream>
    using namespace std;
    
    int main(int argc, char** argv)
    {
      for (int i = 0; i < argc; ++i)
      {
        if (i)
        {
          cout << "argv[" << i << "]: " << argv[i] << endl;
        }
        else
        {
          cout << "Program name: " << argv[i] << endl;
        }
      }
      return 0;
    }
    Last edited by Elkvis; 01-31-2012 at 10:32 AM. Reason: bad code

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Dear aspiring programmer,
    The indentation has disappeared from your code, making it somewhat difficult to read.
    However, I do note that you call srand multiple times. You should only call it once as, say, your first executable statement in main. And main should be of type int, and should have a return 0 at the end.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    7
    i am a new user so plese tell me how to do this

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    2,421
    Quote Originally Posted by anurudh View Post
    i am a new user so plese tell me how to do this
    use an IDE like eclipse, visual studio, or code::blocks that does indentation automatically.

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    7
    i am a student of class 11 oogabooga so please help me out i am new to this forum so kindly make the allowances also our teacher has taught us only void main() and thus we are allowed to that only

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Tell you how to indent?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Registered User
    Join Date
    Jan 2012
    Posts
    7
    no! how to get over the error as u said i will make it only one srand but where do i give it can u please tell me that

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your randm function, shown below with some indentation, is completely insane.
    num[v] is used without being initialized in the first part, and num[u]=z is likely to be outside of its size of 20.
    I have no idea what you are trying to do in this function. Can you explain what you want it to do in words?
    Code:
    void randm (int y) {
      int num[20], u=y*100,flag=0,w;
      long z;
      for (w=0;w<20;w++) {
        do{
          z=rand()%(20);
          z+=u;
          for(int v=0;v<=u;v++)
            if (z==num[v])
             flag=1;
        }while(flag!=0);
      }
      if(flag==0) {
        num[u]=z;
        cout<<"room number is "<<z<<endl;
      }
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  10. #10
    Registered User
    Join Date
    Jan 2012
    Posts
    7
    also is the rest of my program good enough the difficulty arises as we are only allowed to use functions and structures for this project

  11. #11
    Registered User
    Join Date
    Jan 2012
    Posts
    7
    well the idea was given to me by my teacher
    it goes this way we have to generate the random number and store it in the array and then check it (for the next user) if it hasnt already been generated and continue the process till we get another number
    also
    she told me to use randomize()
    but searching on the net all i got was rand()

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    Quote Originally Posted by anurudh View Post
    well the idea was given to me by my teacher
    it goes this way we have to generate the random number and store it in the array and then check it (for the next user) if it hasnt already been generated and continue the process till we get another number
    also
    she told me to use randomize()
    but searching on the net all i got was rand()
    It sound's like the teacher is used to teaching *cough* Visual Bullsxxt, *cough*, I mean "Visual Basic", rather than C++.

    You put the work into indenting that code properly, and I might take a look at it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,625
    REMOVE GETS! Replace it with std::getline.
    Get rid of all chars, and replace them with std::string.
    Indent your code and follow some popular style (eg Allman).
    Get a modern compiler, such as GCC or Visual Studio.
    Get rid of all the old headers (iostream.h should be iostream).
    Change return type of main to int (main should always return int; no exceptions).

    Once you've done that, check back. Feel free to ask for help in achieving these goals.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stuck with function
    By scmurphy64 in forum C Programming
    Replies: 9
    Last Post: 11-10-2009, 10:41 AM
  2. I.b. Stuck on function def
    By RedZippo in forum C++ Programming
    Replies: 1
    Last Post: 05-05-2004, 11:39 AM
  3. Replies: 2
    Last Post: 12-25-2003, 12:31 AM
  4. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 07:37 PM
  5. Generating a random character/letter, stuck!
    By DanFraser in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2003, 11:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21