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

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
Asked:
lonewarrior
  • 3
  • 2
  • 2
  • +3
1 Solution
 
ExceterCommented:
Homework.

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

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

If you HONESTLY want help you must try to complete this yourself and return here with SPECIFIC difficulties. We are here to help YOU work through problems. NOT TO HELP YOU CHEAT.

Exceter
0
 
lonewarriorAuthor 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
   scores.open ("raw_grades.txt");
   if (scores.fail ())
    {
         cout << "Input file opening failed.\n";
         exit(1);
    }
   final_score.open ("formatted_grades.txt");
   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"
             << " Your weights don't add up to 100";
        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
 
tinchosCommented:
do you want to count the number of digits or the number of numbers?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
lonewarriorAuthor 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
 
lonewarriorAuthor 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
 
Fluffy_CheckersCommented:
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
 
Fluffy_CheckersCommented:
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
 
ExceterCommented:
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
 
manu2020Commented:
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
 
bcladdCommented:
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.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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