0

Hello, I have a program I am writing to count the number of integers (as well as the individual digits) in a file. I feel I'm close but I'm not quite there so any insight would be helpful.

I'll only post the segments of code relevant to where I need help.

My main function:

do
	{
		text.get(ch);

		if(isdigit(ch))
		{	
			sum = Integers(ch, count3, text);
			numofint++;
		}
	} while(!text.eof());

Integers function:

int Integers(char &ch, int &counter, ifstream& text)
{
	int total = 0;
	int sum = 0;
	bool success = true;
	
	while((isdigit(text.peek()) == success))
	{
		counter++;
		total = (total*10) + (static_cast<int>(ch)-48);
		sum += total;
	}	
	return sum;
}
2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by Angelaa
0

Sorry, I should add the only libraries I am able to use are:
#include <iostream>
#include <iomanip>
#include <cctype>
#include <fstream>

0

The function is not returning any results. My counter inside it is set up to count the digits, but is continually returning the 0 is was initialized to, so I assumed the part that was wrong is:

while(isdigit(text.peek()) == success)

note: text is my input file stream.

However, I am not sure why or what to do differently since I've tried this many ways with no success.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.