Need help with finding sum of an array plz.

Ask for help with your homework/assignments in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Need help with finding sum of an array plz.

Postby MglMogul » Tue Nov 29, 2011 11:53 pm

I need help finding the sum of the expense in this array. Please tell me whats wrong with this and help me fix it. Will appreciate it thank you
Code: Select all
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;

int main()
{

    char type[100];
   double total=0;
   double amount;
   int numbers=0;
   int i=0;
   char name[30];
      cout << "enter the employee name:"<< endl;
      cin.getline(name,30);
      cout << "enter the number of expense items:" <<  endl;
      cin >> numbers;
      cin.ignore();
      while (i < numbers)
{
   i++;
      cout << "enter the type of expense:";
      cin.getline(type,100);
      cout << "enter the amount of expense:";
      cin >> amount;
      cin.ignore();   
      for (i=0; i <=numbers; i++)
      {
         total += amount[i];
         i++;
      cout << "total =" << total << endl;
      }
}
}
MglMogul
 
Posts: 10
Joined: Tue Nov 29, 2011 11:51 pm

Re: Need help with finding sum of an array plz.

Postby Wizard » Wed Nov 30, 2011 8:51 am

here:
Code: Select all
      for (i=0; i <=numbers; i++)
      {
         total += amount[i];
         i++;
      cout << "total =" << total << endl;
      }

After asking for a single amount you then create a loop to (presumably) go through every amount and add it to total. The first problem being that you only have one "amount" value at this time so using a for loop is wrong; the second problem is that you are trying to treat amount as an array but it isn't an array, it's just a single number.
You also have another unrelated problem: you're doing i++ twice, once in the for declaration and again within the loop itself. In fact, using "i" there as your counter is entirely wrong because you're already using it to count the bigger while loop. Just get rid of that for loop, you don't need it. Replace with simple "total += amount;" and you should be fine.
User avatar
Wizard
Site Admin
 
Posts: 3226
Joined: Mon Sep 22, 2003 4:52 pm
Location: ON, CA

Re: Need help with finding sum of an array plz.

Postby MglMogul » Wed Nov 30, 2011 11:23 am

Thank you very much again :D.
MglMogul
 
Posts: 10
Joined: Tue Nov 29, 2011 11:51 pm

Re: Need help with finding sum of an array plz.

Postby MglMogul » Wed Nov 30, 2011 11:46 am

I need to display the results in a text file as well and I can't get all of them displayed on it but only 1.
can you help me fix this?
This is a code that will go bottom of the code above.
Code: Select all
}      
  ofstream myfile;
  myfile.open ("data.txt");
  myfile << "\n" << name << ":" << type << ":" << amount << endl;
  myfile << "\ntotal = " << total << endl;
  myfile.close();

}   
MglMogul
 
Posts: 10
Joined: Tue Nov 29, 2011 11:51 pm

Re: Need help with finding sum of an array plz.

Postby Wizard » Thu Dec 01, 2011 8:54 am

do the "myfile.open" part at the very start of your program, and replace all cout with myfile. Don't close the file until the end.
User avatar
Wizard
Site Admin
 
Posts: 3226
Joined: Mon Sep 22, 2003 4:52 pm
Location: ON, CA

Re: Need help with finding sum of an array plz.

Postby MglMogul » Thu Dec 01, 2011 11:03 am

Wizard wrote:do the "myfile.open" part at the very start of your program, and replace all cout with myfile. Don't close the file until the end.

Thank you!!! You are the best :D
MglMogul
 
Posts: 10
Joined: Tue Nov 29, 2011 11:51 pm


Return to Homeworks

Who is online

Users browsing this forum: Google [Bot] and 0 guests