CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2010
    Posts
    1

    Code Formatting Help...

    I'm working on a program that reads data from the user (in the form of number of people in a hotel room) and outputs whether or not there is a vacancy.

    The code is as follows:


    1) #include "stdafx.h"
    2) #include <iostream>
    3)
    4) int main ()
    5) {
    6) using namespace std;
    7) using namespace std;
    8) cout << "How many people checked into room 1?" << endl;
    9) int nPeople1;
    10) cin>> nPeople1;
    11) using namespace std;
    12) cout << "How many people checked into room 2?" << endl;
    13) int nPeople2;
    14) cin>> nPeople2;
    15) using namespace std;
    16) cout << "How many people checked into room 3?" << endl;
    17) int nPeople3;
    18) cin>> nPeople3;
    19) using namespace std;
    20) cout << "How many people checked into room 4?" << endl;
    21) int nPeople4;
    22) cin>> nPeople4;
    23) cout << "How many people checked into room 5?" << endl;
    24) int nPeople5;
    25) cin>> nPeople5;
    26) if (nPeople1 == 0||nPeople2 == 0||nPeople3 == 0||nPeople4 == 0||nPeople5 == 0)
    27) cout<< "VACANCY"<< endl;
    28) else
    29) cout<< "NO VACANCY"<< endl;
    30)
    31) return 0;
    32) }

    If possible, I would like to break the main function into 2 separate functions. One called IsOcc that gathers the data, and the main function to process and output the vacancy status.

    Any help is greatly appreciated!

    -Edide

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Code Formatting Help...

    A single function to query the status of all the rooms from the user isn't really practical if the status of each room is stored in a separate variable local to main(). Is that way of storing the room status mandatory in the assignment?

    You should remove most of your

    Code:
    using namespace std;
    directives. You only need that once in main(), or, if it is meant to affect more than one function, outside of a function body, right after the #includes.

    HTH

    Please use code tags the next time you post code, and no line numbers.

    Ah, and... Welcome to CodeGuru!
    Last edited by Eri523; December 2nd, 2010 at 04:28 PM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Code Formatting Help...

    You need to learn to use arrays and loops. What would you do if your hotel had 1,000 rooms?

  4. #4
    Join Date
    Jul 2010
    Posts
    88

    Re: Code Formatting Help...

    Whenever you see a linear pattern in the names, the same thing can be done with an index.

    MyVar1 = MyVar[1-1]
    MyVar2 = MyVar[2-1]
    MyVar3 = MyVar[3-1]
    ...

    To do something with each element, do it using a dynamic index.

    Code:
    #define LoopForward(min,var,max) for(var=min;var<=max;var++)
    
    int x;
    LoopForward(0,x,2) {
        MyVar[x] = MyFunction(x);
    }
    Last edited by Dawoodoz; December 3rd, 2010 at 07:29 AM. Reason: Forgot how the code tag was written

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Code Formatting Help...

    Quote Originally Posted by Dawoodoz View Post
    Whenever you see a linear pattern in the names, the same thing can be done with an index.

    MyVar1 = MyVar[1-1]
    MyVar2 = MyVar[2-1]
    MyVar3 = MyVar[3-1]
    ...

    To do something with each element, do it using a dynamic index.

    Code:
    #define LoopForward(min,var,max) for(var=min;var<=max;var++)
    
    int x;
    LoopForward(0,x,2) {
        MyVar[x] = MyFunction(x);
    }
    That's what I said, but why hide the for loop behind a #define? Completely unnecessary and makes thing more confusing for a beginner.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured