Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,392 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,245 people online right now. Registration is fast and FREE... Join Now!




C++ loop

 
Reply to this topicStart new topic

C++ loop, I dont no how to add loop

julia231
post 20 Aug, 2008 - 11:48 PM
Post #1


New D.I.C Head

*
Joined: 20 Aug, 2008
Posts: 2

a) add a loop to the main (i.e for loop) to create a deposit for each group element
cool.gif add a loop to the main (i.e. for loop) to output the balance of each group element
the output loop should also identify which group element is being output

ex. group 1 account :
balance : 300
ex. group 2 account :
balance :600

c) add a new method, named "interest" to the class, this method will accept one parameter, for interest rate. The programmer may also choose to change the data type of the balance variable (for example, the balance variable could be changed to double or float)
CODE


#include <iostream>
using namespace std;

class Account
{
    long balance;    // current balance in pennies

    // int may be insufficient in C/C++

public:
    void deposit (long amount)
    {
        balance += amount;
    }

    void withdraw (long amount)
    {
        balance -= amount;
    }

    void status ()
    {
        cout << "balance : " << balance << endl;
    }
    // the following is called a "constructor" - used to initialize new accts
    Account (long initialAmount = 0)
    
    {
        balance = initialAmount;
    }
    // new acct
    // =0 means that if the initialAmount is omitted it defaults to 0

    // the following is a "destructor"
    ~Account ()
    {
        cout << "transfer " << balance <<" to manager " << endl;
    }

};
// note ";" required

int main ()
{
    Account mine, yours (100), group[6];
    // allocates 8 Account objects and initialize each with "c-tor"
    // The second account is initialized with a balance of 100 - others all 0.

    mine.deposit (500); // now balance should be 500
    mine.status();        // " The balance is : 500"
    yours.status();     // " The balance is : 100"
    yours.deposit(300);    
    yours.status();        //"The balance is : 400"
    mine.withdraw(600);     // now in the red
    mine.status ();        //" The balance is : -100"
    return 0;
}
                //8 accounts are destructed here
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 20 Aug, 2008 - 11:52 PM
Post #2


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,328



Thanked 57 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


An example for loop :
cpp

#include <stdio.h>

int main(void) {
int bal=300; // Balance
int Max_Group=10;
int i; // We use this to compare

for(i=1;i<=Max_Group;i++) {
printf("Group %d account:\nBalance : %d\n",i,bal*i);
}
return 0;
}
User is offlineProfile CardPM

Go to the top of the page

julia231
post 20 Aug, 2008 - 11:56 PM
Post #3


New D.I.C Head

*
Joined: 20 Aug, 2008
Posts: 2

i still dont realli understand what you mean ?
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 21 Aug, 2008 - 12:10 AM
Post #4


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,328



Thanked 57 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


The output from the example loop :

QUOTE

Group 1 account:
Balance : 300
Group 2 account:
Balance : 600
Group 3 account:
Balance : 900
Group 4 account:
Balance : 1200
Group 5 account:
Balance : 1500
Group 6 account:
Balance : 1800
Group 7 account:
Balance : 2100
Group 8 account:
Balance : 2400
Group 9 account:
Balance : 2700
Group 10 account:
Balance : 3000


It's just a simple for loop.

CODE

for(startExpression; testExpression; countExpression)
{
    block of code;
}
User is offlineProfile CardPM

Go to the top of the page

OliveOyl3471
post 21 Aug, 2008 - 04:16 AM
Post #5


It's all about the code ♥

Group Icon
Joined: 11 Jul, 2007
Posts: 1,484



Thanked 14 times

Dream Kudos: 100
My Contributions


QUOTE(julia231 @ 21 Aug, 2008 - 02:56 AM) *

i still dont realli understand what you mean ?


A for loop just means that as long as the condition in the for loop is true, keep on doing whatever is in the statment(s) part of the for loop.

So...
as long as i is less or equal to ten, it will keep printing your cout statement.

i++ means add one to i each time.

So...
i starts out with a value of 1, and each time the for loop iterates it adds one to i, so the next time the for loop iterates, i will be 2, then it will be 3, etc. and the loop will continue until the condition (i<=10) is no longer true.

%d within the quotes will cause whatever follows the quote to be printed in place of the %d.

So...
i,bal*i means that the value of i, then the value of bal*i will be printed in place of the %d's in the quotes, each time your loop iterates, so the first time the loop iterates 300 will be printed, then the next time bal*i will be 600, since i is now 2, and it continues until the condition in the for loop is no longer true.

I think it's a good idea, as your instructions point out, to change the data type of balance to a float (you could use double but a float will probably work just as well, and it it smaller than a double). An int won't allow you to use decimals, so a float or double is better when dealing with money.

Does that help? smile.gif

This post has been edited by OliveOyl3471: 21 Aug, 2008 - 04:29 AM
User is online!Profile CardPM

Go to the top of the page

DTR
post 21 Aug, 2008 - 05:26 AM
Post #6


New D.I.C Head

*
Joined: 7 Aug, 2008
Posts: 33


My Contributions


Why are we offering for as an example anyway?


CODE


int i = 0;

  while(i < 1000) //While variable i contains a value that is lesser than 1000
  {

  printf("lol");

  i++;
  }

// Yes, there is no indexing in this example, but meh!




For me at least the basic While-loop was much less confusing when learning things out. for(beginState; condition; changeOfState) just felt confusing... I frankly don't know why.

Edit: Oh, "i.e for loop"....
Gee, should read the posts every once in a while.



This post has been edited by DTR: 21 Aug, 2008 - 05:30 AM
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 21 Aug, 2008 - 05:52 AM
Post #7


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,402



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


Why use a while loop when you can use a for loop?

It's much more concise, everything to do with the loop is in one place.

Comparision:
cpp
#include <iostream>
using std::cout;
using std::endl;
using std::cin;

int main ()
{
for (int i = 0; i < 20; i++)
cout << i << endl;
cin.get ();
return EXIT_SUCCESS;
}

is much easier to read than:
cpp
#include <iostream>
using std::cout;
using std::endl;
using std::cin;

int main ()
{
int i = 0;
// have about 500 lines of code here
while (i < 20) // what was i again? I forgot by the time I reached this line...
cout << i << endl;
cin.get ();
return EXIT_SUCCESS;
}
User is offlineProfile CardPM

Go to the top of the page

KYA
post 21 Aug, 2008 - 06:08 AM
Post #8


#include <nerd.h>

Group Icon
Joined: 14 Sep, 2007
Posts: 4,205



Thanked 50 times

Dream Kudos: 1150
My Contributions


I second gabe's post. For loops are cleaner and all conditions are laid out at the front, no "hidden" agenda.
User is online!Profile CardPM

Go to the top of the page

DTR
post 21 Aug, 2008 - 07:16 AM
Post #9


New D.I.C Head

*
Joined: 7 Aug, 2008
Posts: 33


My Contributions


Thats just stupid way of initializing i far away from its using spot.


We can screw up For if we really try too.

CODE


   #include <iostream>  
  
    using std::cout;  
    using std::endl;  
    using std::cin;  
      
    int main ()  
   {  
      



for (int i = 0; i < 20; i++)  
          cout << i << endl;  
          cin.get ();  
   return EXIT_SUCCESS;  
   }  

User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 21 Aug, 2008 - 07:21 AM
Post #10


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,402



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


That doesn't screw it up in the slightest. It's just messy indentation.

All I'm saying is that for is much more efficient that while when you want to loop a set number of times. while, and do/while are best when you want to run a loop until a set statement is true. (A sentinel value)
User is offlineProfile CardPM

Go to the top of the page

DTR
post 21 Aug, 2008 - 07:29 AM
Post #11


New D.I.C Head

*
Joined: 7 Aug, 2008
Posts: 33


My Contributions


Thats just stupid way of initializing i far away from its using spot.


We can screw up For if we really try too.

CODE


   #include <iostream>  
  
    using std::cout;  
    using std::endl;  
    using std::cin;  
      
    int main ()  
   {  
  
   int beginningPointForsomeObscureLoop = 5;  
  
   // Some 500 lines of code here

   for (int i = beginningPointForsomeObscureLoop; i < beginningPointForsomeObscureLoop+20; i++)   // What was that value again?
          cout << i << endl;  
          cin.get ();  
   return EXIT_SUCCESS;  
   }  

User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 07:16AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month