11 Replies - 443 Views - Last Post: 20 July 2012 - 02:25 PM Rate Topic: -----

#1 zachkt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 19-July 12

Trying to get the number of numbers in a txt file opened in my program

Posted 19 July 2012 - 06:11 PM

what function will allow me to calculate the number of numbers in my the text file which is opened in my program?
Here is what i got so far.


#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    // define variables
	ifstream inputFile;
    int number;
	double calaverage;
	double total;
	int numofnumbers;

	// open the file
	inputFile.open("Random.txt");
	
	if (!inputFile)
        cout << "Error opening the file!\n";
    
	// read the numbers from the file and process numbers
	else 
    {
        
       while (inputFile >> number)
        {
            cout << number << endl;
        }
	
	}
    // close the file

    inputFile.close();
	
	// display the result with formatting
    
  
    
	return 0;
}




dont worry, it runs smoothly but i just need a calculation or function that can get me the number of numbers in that txt file

Is This A Good Question/Topic? 0
  • +

Replies To: Trying to get the number of numbers in a txt file opened in my program

#2 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: Trying to get the number of numbers in a txt file opened in my program

Posted 19 July 2012 - 07:36 PM

Well you have a variable numofnumbers, where do you use it? You should initalise it first. You don't want a function here, you need to use your loop.
Was This Post Helpful? 0
  • +
  • -

#3 zachkt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 19-July 12

Re: Trying to get the number of numbers in a txt file opened in my program

Posted 19 July 2012 - 07:41 PM

View Postsimeesta, on 19 July 2012 - 07:36 PM, said:

Well you have a variable numofnumbers, where do you use it? You should initalise it first. You don't want a function here, you need to use your loop.


alright, i'll try that and get back to u ASAP, i gotta catch the Dark Night Rises! Thanks man
Was This Post Helpful? 0
  • +
  • -

#4 zachkt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 19-July 12

Re: Trying to get the number of numbers in a txt file opened in my program

Posted 20 July 2012 - 12:16 PM

why does it not work when i use:


numofnumbers =+ number;
cout << "The number of numbers in the file is "<< numofnumbers << endl;




i saw the "=+" somewhere online but the program still does not count the number of numbers in the file
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: Trying to get the number of numbers in a txt file opened in my program

Posted 20 July 2012 - 12:57 PM

Quote

i saw the "=+" somewhere online but the program still does not count the number of numbers in the file

Did you follow the following advice?

View Postsimeesta, on 19 July 2012 - 09:36 PM, said:

You should initalise it first.

Also it is not "=+" but "+=".

Jim
Was This Post Helpful? 0
  • +
  • -

#6 zachkt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 19-July 12

Re: Trying to get the number of numbers in a txt file opened in my program

Posted 20 July 2012 - 01:09 PM

View Postjimblumberg, on 20 July 2012 - 12:57 PM, said:

Quote

i saw the "=+" somewhere online but the program still does not count the number of numbers in the file

Did you follow the following advice?

View Postsimeesta, on 19 July 2012 - 09:36 PM, said:

You should initalise it first.

Also it is not "=+" but "+=".

Jim

ya i got it to work.
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    // define variables
	ifstream inputFile;
        int number;
	double calaverage;
	double total;
	int numofnumbers = 0;
	


	// open the file
	inputFile.open("Random.txt");
	
	if (!inputFile)
        cout << "Error opening the file!\n";
    
	// read the numbers from the file and process numbers
	else 
    {
        
       while (inputFile >> number)
        {
            cout << number << endl;
			numofnumbers ++;
        }
	
	}
	cout << endl;
    // close the file

    inputFile.close();
	
	// display the result with formatting
	cout << "The number of numbers in the file is "<< numofnumbers << endl;

    
    //total/numofnumbers;
    
	return 0;
}



Any idea on how i can get the sum of the numbers in the file? I want to be able to calculate the average after
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: Trying to get the number of numbers in a txt file opened in my program

Posted 20 July 2012 - 01:50 PM

How do you sum a series of numbers without a computer?

Jim
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3569
  • View blog
  • Posts: 11,089
  • Joined: 05-May 12

Re: Trying to get the number of numbers in a txt file opened in my program

Posted 20 July 2012 - 01:55 PM

Based on the OP, I think that he is hoping there is a just-take-a-pill-instant-weight-loss, or get-rich-quick type function where you just pass in the filename and that it will just add up all the numbers in the file for him.

This post has been edited by Skydiver: 20 July 2012 - 01:55 PM

Was This Post Helpful? 0
  • +
  • -

#9 zachkt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 19-July 12

Re: Trying to get the number of numbers in a txt file opened in my program

Posted 20 July 2012 - 02:02 PM

View PostSkydiver, on 20 July 2012 - 01:55 PM, said:

Based on the OP, I think that he is hoping there is a just-take-a-pill-instant-weight-loss, or get-rich-quick type function where you just pass in the filename and that it will just add up all the numbers in the file for him.

HAhah ya well i got again.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    // define variables
	ifstream inputFile;
    int number;
	double calaverage;
	double total = 0;
	int numofnumbers = 0;
	


	// open the file
	inputFile.open("Random.txt");
	cout << "Here are the numbers in the file: \t\t";
	cout <<endl; 
	
	if (!inputFile)
        cout << "Error opening the file!\n";
    
	// read the numbers from the file and process numbers
	else 
    {
        
       while (inputFile >> number)
        {
            cout << number << endl;
			numofnumbers ++;
			total = total + number;
			calaverage = total/numofnumbers;
        }
	
	}
	cout << endl;
    
	// close the file

    inputFile.close();
	
	// display the result with formatting
	cout << "The number of numbers in the file is "<< numofnumbers << endl;
	cout << "The sum of the numbers in the file is "<< total << endl;
	cout << "The average of the numbers in the file is "<< calaverage << endl;
	cout << endl;
   
	return 0;
}




I have realized that i have been answering all my questions but can you tell me how i can format the numbers in a way that they can appear in columns instead of a list?
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3569
  • View blog
  • Posts: 11,089
  • Joined: 05-May 12

Re: Trying to get the number of numbers in a txt file opened in my program

Posted 20 July 2012 - 02:13 PM

It's great to see that you are getting to your own answers!

As for the columns, do you know how many columns you want in the output?

Or does your input file have numbers in columns, and you want your output to match the input?

As an aside: somebody a few days ago posted a question like "how to get the average of numbers in a matrix". The problem he was trying to solve was very similar to yours. If the two of you are in the same class or taking the same course, sometimes the best way to learn is to have a study buddy.

This post has been edited by Skydiver: 20 July 2012 - 02:14 PM

Was This Post Helpful? 0
  • +
  • -

#11 zachkt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 19-July 12

Re: Trying to get the number of numbers in a txt file opened in my program

Posted 20 July 2012 - 02:23 PM

View PostSkydiver, on 20 July 2012 - 02:13 PM, said:

It's great to see that you are getting to your own answers!

As for the columns, do you know how many columns you want in the output?

Or does your input file have numbers in columns, and you want your output to match the input?

As an aside: somebody a few days ago posted a question like "how to get the average of numbers in a matrix". The problem he was trying to solve was very similar to yours. If the two of you are in the same class or taking the same course, sometimes the best way to learn is to have a study buddy.

When i run the program, it opens a file which contains a bunch of numbers. The numbers are displayed in like a long list (200 numbers). Instead of the list i was wondering if i can format the list in a way that its in columns (like 20x10) so that i can see all the numbers and the calculations without having to scroll so much.
Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is online

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: Trying to get the number of numbers in a txt file opened in my program

Posted 20 July 2012 - 02:25 PM

You may want to look into using the C++ manipulators.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1