Thread: For loop addition

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    2

    For loop addition

    hello i am having some trouble as i have left my notes at college and i am currently on half term

    My program is to work out expenses for employees, so ihave used a for loop to loop he set of code that requires employees to input their data and the code for calculation, that seems to work fine

    but what i need to do next is to calculate a department total so, how do i add all the expenses from the loop so that i output a department total


    Code:
    //for loop for the employees expnses statement
     
    for (i = 0; i <noemp; i++) 
    {
     
    //clears screen after department details have been entered
    system("cls");
          
     
    //Input for employees details
    //call function empdetails
    empdetails() ;
     
    //if the engine option is A
    if (esize == 'A'|| esize == 'a')
    {
    //if they have travelled less than 500 miles
    if (mtravel <= 500)
    { 
    //call function under
    under();
    tc = tr * 1;
    }
    //if they travelled over 500 miles
    else if (mtravel > 500)
    {
    //call function over
    over();
    tc = toc * 1;
    }
    }
     
    //if the engine option is B
    else if (esize == 'B'|| esize == 'b')
    {
    //if they have travelled less than 500 miles
    if (mtravel <= 500)
    { 
    //call function under
    under();
    tc = tr * 0.8
    }
    //if they travelled over 500 miles
    else if (mtravel > 500)
    {
    //call function over
    over();
    tc = toc * 0.8;
    }
    }
     
    //if the engine option is C
    else if (esize == 'C'|| esize == 'c')
                                      {
    //if they have travelled less than 500 miles
    if (mtravel <= 500)
           { 
                  //call function under
                  under();
                  tc = tr * 0.5;
           }
    //if they travelled over 500 miles
    else if (mtravel > 500)
           {
                  //call function over
                  over();
                  tc = toc * 0.5;
           }
    }
     
    //error message is correct character is not inputted
    else
    { 
    //clears the screen before error message is shown
    system("cls");
    cout << "ERROR - Please input A/B/C" <<endl <<endl;
    }
          
                 
    //clears screen before expenses statement appears 
    system("cls");
    //Output of the expenses statement for employees
    //call function expenses 
    expenses();
     
    //calculation for total department expenses
    deptotal = tc + ;
     
    system("pause");
    }
    //clear screen before department summary output
    system("cls");
     
                 
    //calculation for average expenses
     
    //Department summary output
    cout << deptotal;
    system("pause");

    so i basically need to add all the tc variables together so that i get a total

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You need to work on your indentation, that would make your work, and ours, much easier.

    About your question, I'm not sure what you're trying to do, but I guess you could try adding them all up. Initialize "deptotal" to zero before the loop, and add "tc" to it after every turn.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Feb 2015
    Posts
    2
    Thank you so much, it works I forgot to make it equal to 0. it was indented but it looked all messed up when I was going to post it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  2. New addition
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 11-06-2004, 08:41 PM
  3. Addition to FAQ
    By sigma in forum C Programming
    Replies: 2
    Last Post: 04-01-2002, 12:20 PM
  4. Help - Addition
    By Billye Scott in forum C++ Programming
    Replies: 2
    Last Post: 03-04-2002, 06:52 PM
  5. addition
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 01-19-2002, 07:53 AM