Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


C++ Loops

C++ Loops

C++ Loops

Can anyone give advice/help with a minor programming glitch.We are trying to write a small program for a BTEC college project and are experiencing difficulty making the FOR LOOP operate correctly. If we enter the number of wall we wish to find the area of it always returns one less than the actual number asked for and will not total the overall area.
Can you suggest a loop which will calculate the area of a user specified amount of walls and return the total wall area. Any loop will do.

Your assistance with this is much appreciated,

Thank you

RE: C++ Loops

Its very difficult to see any problems with the code without viewing the code, as the problem may not be in the loop, please supply more info.

RE: C++ Loops

Hopefully you have solved this by now - 3 months or so on

anyway, try starting your loop at zero:

// This should compile on even the most ancient C++ compiler.

#include <iostream.h>
int main(int argc, char * argv[])
double width   = 3.5;
double height  = 2.6;
double total   = 0;
int    NumWals = 4;

// loop from 0 to < NumWalls gives "NumWalls" iterations.
// you could also loop from 1 to <= NumWalls for same result
int wall = 0;
for (wall=0; wall<NumWalls;wall++)
  total = total + (width * height);

cout << "Total Area is " << total << endl;

Hey - I was bored ok!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close