You are here:

C++/please help me out

Advertisement


Question
Please can u help me out
i am trying to read (the numbers) from the text file.
and it is working perfectly
after that i am tryng the calculate the total sum  what ever is in txt file , it is not working
can u help me out to calculate the total ammount form text file
please??????


#include<iostream>
#include<fstream>
#include<math.h>
#include <string>
#include <conio.h>
#include <stdlib.h>
#include <iostream>
#include <cstdlib>
#include <string>

int twohundert;
int hundert;
int fifty;
int twenty;
int ten;
int five;
int two;
int one;
int sum;
int ammount50[10];

void ammount()
{
  {
     read();
     
  cout <<"\n\n\n\n\t\tQuantity of " <<(char)156<<"2 Coin's";
  cin >>twohundert;
  cout <<"\n\t\tQuantity of " <<(char)156<<"1 Coin's";
  cin>> hundert;
  cout <<"\n\t\tQuantity of 50p Coin's";
  cin >>fifty;
  cout <<"\n\t\tQuantity of 20p Coin's";
  cin>>  twenty;
  cout <<"\n\t\tQuantity of 10p Coin's";
  cin >> ten;
  cout <<"\n\t\tQuantity of 5p Coin's";
  cin >> five;
  cout <<"\n\t\tQuantity of 2p Coin's";
  cin >> two;
  cout <<"\n\t\tQuantity of 1p Coin's";
  cin >>one;



  ofstream myfile;
    myfile.open ("twohundert.txt", ios::app);
    myfile <<twohundert << endl;
    myfile.close();
  
   ofstream myfile2;
    myfile2.open ("hundert.txt", ios::app);
    myfile2 <<hundert << endl;
    myfile2.close();

   ofstream myfile3;
    myfile3.open ("fifty.txt", ios::app);
    myfile3 <<fifty << endl;
    myfile3.close();

   ofstream myfile4;
    myfile4.open ("twenty.txt", ios::app);
    myfile4 <<twenty << endl;
    myfile4.close();

   ofstream myfile5;
    myfile5.open ("ten.txt", ios::app);
    myfile5 <<ten << endl;
    myfile5.close();

   ofstream myfile6;
    myfile6.open ("five.txt", ios::app);
    myfile6 <<five << endl;
    myfile6.close();

   ofstream myfile7;
    myfile7.open ("two.txt", ios::app);
    myfile7 <<two << endl;
    myfile7.close();

   ofstream myfile8;
    myfile8.open ("one.txt", ios::app);
    myfile8 <<one << endl;
   myfile8.close();
  }
  
  
  char number[10];
   int *readnumbers = 0;
  string line;
  ifstream myfile9("twohundert.txt",ios::in);
   if(myfile9.is_open())
   {
      readnumbers = new int[10];
     
      
      {int i = 0;
         while(!myfile9.eof())
         {
         myfile9.getline(number,10);
         readnumbers[i]=atoi(number);
         
         cout<<number  << " ";
         }
        
         
         myfile9.close();

      }
   
   }
}

Answer
For basics of file input, see: http://www.arachnoid.com/cpptutor/student2.html

Find the sum of all integers in a file:
       
       std::ifstream file( "filename.txt" ) ; // open the file for input
       int sum = 0 ;
       int number ;
       while( file >> number ) // while another number can be read
         sum += number ; // add the number just read to the sum  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.