Jump to content

Counting sums from text files

- - - - -

  • Please log in to reply
4 replies to this topic

#1
Bertan

Bertan

    Learning Programmer

  • Members
  • PipPipPip
  • 43 posts
I just started learning about textfiles with C++.

I'm trying to make a program that will read a textfile that contains a few names together with their age. The program should count the total sum of all the persons age.

Mark
56
Andy
27
Samuel
24

I made a program that reads and writes out the textfile. But I have no clue of how to continue from here. How can I read the text file and sort the char from int, Give the age one value that does not include the names? I really need som help on this to get started. This is what I got this far.


#include<fstream>

#include<iostream>

#include<string>

#include<conio.h>

using namespace std;

int summa;

int main ()

{

char vekt[50][30];

ifstream fil;

fil.open("data.txt");


//If file not found

if (!fil) 

   {

        cout << "Cant find file";

        exit(1); // terminate with error

    }

   

int num=0;


//Reads the file

while(fil.getline(vekt[num], 30))

   {

         num++;   

   }


fil.close();


//The file

cout<<"Varor:"<<endl;

for( int i=0;i<num;i++)

cout<<vekt[i]<<" "<<endl;


getch();

return 0;

}




#2
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
why are you putting the lines in an array if all you want to do is sum up all the ages? Since names and ages are all on different lines all you have to do is check the first character read with isdigit() and if true then convert to int and add to the sum accumulator.

std::string line;

int sum = 0;

while( getline(fil, line) )

{

   if( isdigit(line[0]) )

   {

       sum += atoi(line);

  }

}


Or you could also do it with stringstream object

std::string line;

int sum = 0;

while( getline(fil, line) )

{

   if( isdigit(line[0]) )

   {

      int n;

      stringstream s;

      s << line;

      s >> n; // converts string to integer

       sum += n;

  }

}


Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#3
Bertan

Bertan

    Learning Programmer

  • Members
  • PipPipPip
  • 43 posts
Got it working now, thanks. Just out of curiosity how would I solve this if the names and the age are on the same row like this?

Mark 56
Andy 27
Samuel 24

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Finding Tokens in a String - The GNU C Library
strtok is a nice function for this task to tokenize each line assuming you do not want to do the dirty work yourself. Just ensure the data is null terminated (when passing to strtok)
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
Bertan

Bertan

    Learning Programmer

  • Members
  • PipPipPip
  • 43 posts
Thanks Nullw0rm I will check it out.

I got some new problems with a function I tried to add to the same program. I want the user to be able to add new names and age to the text file.

First problem I have is that the program only saves the last entered values, which probably has something to do with the way I put the for loops(?)

Also I have no idea how to make the program read and save entered numbers for age correctly.

Anyone can give me some hints on this?


char vekt[50][30];

int age[50][30];

int antal;

cout<<"How many names do you want to enter:  ";

cin>>antal;

cin.get();   

    for(int i=0;i<antal;i++)

		{

			for(int j=1;j<antal;j++)

				{

					cout <<" Enter name: ";

					cin.getline(vekt[j],30);

				}


			for(int k=1;k<antal;k++)

				{

					cout <<" Enter age: ";

					cin >> age[k][30];

				}

		}


ofstream utskrift("data.txt",ios::app);

	if(!utskrift)

		{

			cout<<"could not find file"<<endl;

		}

for(int i=0;i<antal;i++)

	{

		for(int j=1;j<antal;j++)

			{

				utskrift<<vekt[j]<<endl;

			}

		for(int k=1;k<antal;k++)

			{

				utskrift<<age[k]<<endl;

			}

	}

	utskrift.close();






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users