?
Solved

counting numbers in a line

Posted on 2003-02-21
11
Medium Priority
?
235 Views
Last Modified: 2010-04-01
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
Comment
Question by:lonewarrior
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
11 Comments
 
LVL 8

Expert Comment

by:Exceter
ID: 7995969
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
 

Author Comment

by:lonewarrior
ID: 7996142
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
 
LVL 9

Expert Comment

by:tinchos
ID: 7996404
do you want to count the number of digits or the number of numbers?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:lonewarrior
ID: 7996589
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 Comment

by:lonewarrior
ID: 7996660
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
 

Accepted Solution

by:
Fluffy_Checkers earned 200 total points
ID: 7996781
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
 

Expert Comment

by:Fluffy_Checkers
ID: 7996797
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
 
LVL 8

Expert Comment

by:Exceter
ID: 7996804
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
 

Expert Comment

by:manu2020
ID: 8002756
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
 
LVL 11

Expert Comment

by:bcladd
ID: 9784140
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

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question