Hi All,

As a non-required, ungraded, "activity", I coded the following program, and I'm wondering if there was an easier way for me to of solved the problem. Please note that this is only a second semester type course, and to this point we've only covered basic variables and constants, as well as the order of operations - we've yet to tackle strings, arrays and the like - so, I don't need to be fancy, but any pointers towards more efficient coding practices would be greatly appreaciated! :)

// File Name: transport.cpp
// Activity 3-1
// Author: (aeinstein)
// Date: 05.28.2006

//Activity 3-1: 
// "Suppose you have a group of people that needs to be transported on buses and vans.
// You can charter a bus only if you can fill it. Each bus holds 50 people. You must
// provide vans for the 49 or fewer people who will be left over after you charter buses.
// Write a program that accepts a number of people and determines how many buses must be
// chartered and reports the number of people left over that must be placed on vans.
// Hint: Use the modulus operator to determine the number of people left over."


#include <iostream.h> // necessary for cout command

int main()
{
     short buses, passengers, van;                                              // declare buses, passengers & van as short
     buses = passengers = van = 0;                                              // initialize buses, passengers & van to 0 (not neccessary, but...)
 
     int cutoff;                                                                // declare cutoff as int
     cutoff = 50;                                                               // initialize cutoff to 50

     cout << "How many people in your group need transportation? ";             // print prompt for user input to screen
     cin >> passengers;                                                         // assign user input value to the variable passengers
      
     buses = passengers / cutoff;                                               //calculate number of buses needed via passengers/cutoff
     van = passengers % cutoff;                                                 // calculate number of people needing to be "van'd" via passengers%cutoff

     cout << "With " << passengers << " people requiring transportation, ";     // print user advisory line 1 of 2, as an input confirmation, to screen
     cout << buses << " buses will be needed and " << van;                      // print user advisory line 2 of 2 to screen,
     cout << " passengers will need to go by van.\n";                           // which I coded (line 2 of 2) as two code statements to compact commenting.
 
     return 0;
}

I realize I could have coded the last two code statements (preceeding "return 0;") as one statement via: cout << buses << " buses will be needed and << van << " passengers will need to go by van.\n"; , but I like to align all of my comments five spaces to the right of my longest code statement, so I broke it up as quoted up above. I'm not sure how practical that practice will be moving forward, or in the "real world", but it seems like an acceptable practice to me for now (any suggestions on that as well?).

Regards,
aeinstein

Member Avatar for iamthwee

What are you asking exactly? Or put this way:- what answer were you expecting?

What are you asking exactly? Or put this way:- what answer were you expecting?

Basically, exactly what I put in the thread title.

With that I see that I could have declared and initialized the "cutoff" variable in one statement rather than two. Which then begs the question, can I declare multiple variables of the same data type and initialize them to one given value in just one code statement?

I'm a raw newbie at programming with a lot to learn, so I'm sure I haven't coded the most efficient program, even as simple as it is. With that, as I'm learing, I also want to develop good, concise and efficient coding habits. So, to put it a different way, aside from what I've mentioned up above, what could I have done better, if anything?

I hope that clarifies what I'm asking for! :)

Since this is a trivial program, there is not much to do to make the program efficient. You could initialize the variables as they are declared, as you said, and yes it is possible to do that with multiple variables too. However, although this may vary on who you work for, the use of declaring multiple variables in the same line is discouraged. One variable in one line will be the rule. You could merge the cout statement without changing your rule for length too.

Another golden rule will be to use the comments before the actual code.
And keep the length of a line under 80 characters. Then you can read the code without horizontal scrolling. This is rather historical when considering the width of the screen has increased since the 680 x 480 type, but this would come in handy if you review printed code.
I have often tried to get people to do this, but with no avail, replace the tab characters with spaces( 4 or 8 spaces will be the usual value). This would give consistent results with different viewers, as different code editors have different tab-size settings and may interpret them differently if you save them just as the tab character.

Also it should be

#include <iostream>
// File Name: transport.cpp
// Activity 3-1
// Author: (aeinstein)
// Date: 05.28.2006

//Activity 3-1:
// "Suppose you have a group of people that needs to be transported on buses and
// vans. You can charter a bus only if you can fill it. Each bus holds 50 people. You
// must provide vans for the 49 or fewer people who will be left over after you
// charter buses.Write a program that accepts a number of people and determines how many buses
// must be chartered and reports the number of people left over that must be placed on
// vans.
// Hint: Use the modulus operator to determine the number of people left over."


// necessary for cout command and 
// it should be <iostream> not <iostream.h>
#include <iostream> 

int main()
{
    // declare buses, passengers & van as short and initialize in multiple lines
    short buses         = 0;
    short passengers    = 0;
    short van           = 0;

    // or in one line
    // short buses = 0; passengers = 0, van = 0; 

    // declare cutoff as int and initialize it to 50
    int cutoff = 50;

    // print prompt for user input to screen
    cout << "How many people in your group need transportation? ";

    // assign user input value to the variable passengers
    cin >> passengers;

    //calculate number of buses needed via passengers/cutoff
    buses = passengers / cutoff;

    // calculate number of people needing to be "van'd" via passengers%cutoff
    van = passengers % cutoff;

    cout << "With " << passengers << " people requiring transportation, "
         << buses << " buses will be needed and " << van
         << " passengers will need to go by van.\n";

    return 0;
}

Thanks a lot Wolfpack, exactly the kind of info I was looking for!!! It may be a trivial program - not surprising since we've only covered the first three chapter of an novice level C++ text - but getting this information now helps me develop proper habits early on. I don't recall using any tabs, but I'll certainly watch out for that moving forward - along with the rest of your advice. Thanks again! :)

I don't recall using any tabs,

I should have been more clearer. I was referring to the use of tabs to indent the program. If you just use the tab character as it is, when you copy and paste to daniweb, or view it in another text editor other than the one you typed it, say edit it in Visual Studio Editor and view it in emacs, it may look like this. As you can see, the indenting have all gone askew. If the programmer had used the "replace tabs with spaces" option that comes in most decent editors, you will not see this problem whether you later open it in emacs, vim or visual studio.

Hi WolfPack,

OK, I'll definitely watch out for that in the future - I used notepad and, to the best of my recollection, I didn't use tabbing at all, but maybe a couple inadvertent ones got by! :)

Regards,
Paul

You would like to try TextPad, Notepad++, or Crimson Editor which are free editors for windows and have syntax highlighting and other features that notepad does not provide.

This is useless i know, but im using a free compiler i got with C++ for Dummies (which REALLY sucks) and it doesn't have tab characters for specifically that reason i think. Tabbing in it is confusing, i dont get the rules yrt but sometimes it does 4 spaces, 8 spaces, jumps to the previous tab and weird stuff like that. Same for deleting, cause it doesnt know if the spaces ur deleting were originally tabs. I don't mean to clog, i'm just bored.

You would like to try TextPad, Notepad++, or Crimson Editor which are free editors for windows and have syntax highlighting and other features that notepad does not provide.

For those who may be interested:

TextPad - This apparently is a "shareware" product - please remember to pay the requested $29.00 [USD], if you decide to keep the product!

NotePad++ - This is true freeware.

Crimson Editor - This, again, is true freeware.

Personaly, I'm going to try the two freeware products first, and then TextPad, to see if it's capabilities are enhanced enough beyond the freeware products to justify spending $29.00 - in about a month or so I'll post a follow-up to offer my opinion.

Thanks for the heads-up WolfPack!

Member Avatar for iamthwee

Or you could just use dev -cpp, which does just about everything you need.

Or you could just use dev -cpp, which does just about everything you need.

Obviously that's good advice for someone who's decided to commit to programming, which I haven't. I'll look into a specific IDE when I don't have to follow course requiremets - for now I have to use the Digital Mars C++ compiler for the course. Also, until I can actually complete at least the entry level C++ class that I'm currently taking, there's no sense in determining what IDE I might want to use moving forward. On the otherhand, should the day come where it would be appropriate for me to make such a determination - as I expect it will - then I'll definitely take your advice under consideration! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.