• Status: Solved
• Priority: Medium
• Security: Public
• Views: 236

# counting numbers in a line

hi, im reading 4 lines of data from a data file, and i want to count the number of digits in each line
as in the output would be:
8
7
7
6
how would i go about doing that?

100012336       27  98       8     87     3     55  78
100016738       21  78      68     27     5     53
105656738       21  78      68     27     5     53
100016738       21  78      68     27     5
0
lonewarrior
• 3
• 2
• 2
• +3
1 Solution

Commented:
Homework.

It is against EE policy for experts to do homework problems.

Didn't you read the membership agreement when you signed up here!?

Exceter
0

Author Commented:
alright, this is what i got, but i cant get it to work

ifstream scores;
ofstream final_score;
// Open input and output files, exit on any error
if (scores.fail ())
{
cout << "Input file opening failed.\n";
exit(1);
}
if (final_score.fail ())
{
cout << "Output file opening failed.\n";
exit(1);
}

scores >> weight_assign1>>wieght_assign2>>wieght_assign3>>weight_midterm1>>weight_midterm2>>weight_final;

// checking to see if the weights = 100
weight_legimicy = weight_assign1+wieght_assign2+wieght_assign3+weight_midterm1+weight_midterm2+weight_final;
if ( weight_legimicy != 100)
{
cout << "** check input data **\n"
exit(1);
}

int next;
int counter = 0;
char fin;
while(!scores.eof())
{
while(scores >> next &&  fin !='\n')
{
counter++;
scores.get(fin);
}

cout<<counter<<endl;

counter =1;

}

0

Commented:
do you want to count the number of digits or the number of numbers?
0

Author Commented:
count the numbers, so if its:
100012336       27  98       8     87     3     55  78
100016738       21  78      68     27     5     53
105656738       21  78      68     27     5     53
100016738       21  78      68     27     5
then the output would be
8
7
7
6
0

Author Commented:
count the numbers, so if its:
100012336       27  98       8     87     3     55  78
100016738       21  78      68     27     5     53
105656738       21  78      68     27     5     53
100016738       21  78      68     27     5
then the output would be
8
7
7
6
0

Commented:
It ain't pretty and it more C than C++, but it answers the question.

#include <stdio.h>     // needed for printf
#include <ctype.h>     // needed for isdigit
#include <string.h>     //needed for strlen

int main(void)
{
FILE *fp;
char buffer[256];
int     numbercount = 0;
int     flag = 0;
int linenum = 1;
unsigned int i;

fp = fopen("numbers.txt", "r");
if (fp == NULL) {
printf("Could not open file....\n");
return -1;
}

while(fgets(buffer, 256, fp)) {
for(i = 0; i < strlen(buffer); i++) {
//Note that this can't handle scientific notation numbers
if (isdigit(buffer[i])) {
if (!flag) flag = 1;
} else {
if (flag) {
flag = 0;
numbercount++;
}
}
}
printf("Found %d numbers on line %d\n", numbercount, linenum);
linenum ++;
numbercount = 0;
}

return 0;
}
0

Commented:
It ain't pretty and it more C than C++, but it answers the question.

#include <stdio.h>     // needed for printf
#include <ctype.h>     // needed for isdigit
#include <string.h>     //needed for strlen

int main(void)
{
FILE *fp;
char buffer[256];
int     numbercount = 0;
int     flag = 0;
int linenum = 1;
unsigned int i;

fp = fopen("numbers.txt", "r");
if (fp == NULL) {
printf("Could not open file....\n");
return -1;
}

while(fgets(buffer, 256, fp)) {
for(i = 0; i < strlen(buffer); i++) {
//Note that this can't handle scientific notation numbers
if (isdigit(buffer[i])) {
if (!flag) flag = 1;
} else {
if (flag) {
flag = 0;
numbercount++;
}
}
}
printf("Found %d numbers on line %d\n", numbercount, linenum);
linenum ++;
numbercount = 0;
}

return 0;
}
0

Commented:
I would recommend doing this by reading in an entire line of text and tokenizing it. After each token you increment a counter. Then when strtok() returns null your counter will contain the correct value. For example,

You can read an entire line of text using getline()

in.getline( buffer, 100, '\n' );

Exceter
0

Commented:
i want to modify the code of Fluffy_Checkers

#include <stdio.h>     // needed for printf
#include <ctype.h>     // needed for isdigit
#include <string.h>     //needed for strlen

int main(void)
{
FILE *fp;
char buffer[256];
int     numbercount = 0;

fp = fopen("numbers.txt", "r");
if (fp == NULL) {
printf("Could not open file....\n");
return -1;
}

while(fgets(buffer, 256, fp)) {
numbercount=0;
for(i = 0; i < strlen(buffer); i++) {
//Note that this can't handle scientific notation numbers
if (buffer[i]==' '||buffer!='\n') {
numbercount++;
if(buffer=='\n')
{
printf("%d",numbercount);
break; /* inner for loop */
}
}
}
}

return 0;
}
0

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Ansered: Points to Fluffy_Checkers

Please leave any comments here within the next seven days. Experts: Silence
means you don't care.

EE Cleanup Volunteer

0

## Featured Post

• 3
• 2
• 2
• +3
Tackle projects and never again get stuck behind a technical roadblock.