strpbrk() problem

This is a discussion on strpbrk() problem within the C++ Programming forums, part of the General Programming Boards category; Hi! I problems solving thins assignment: * I want to read numbers from a file and the numbers may be ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    8

    strpbrk() problem

    Hi!

    I problems solving thins assignment:

    * I want to read numbers from a file and the numbers may be seperated.like this

    200ee4e 66 yyy 888
    22 5 6
    1

    * I want to read just the numbers and sum them up.But i get problems with newline the code stops reading after newlin/space.
    It reads 2 0 0 4 but jumps out before 66

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstring>
    using namespace std;
    
    int main(){
    
        ifstream inputFile;
        char input[81];
        char test[] = "0123456789";
        char *ptr;
        int teller = 0;
        
        inputFile.open("oppgave2.txt");
        if(!inputFile){
            cout<<"File error\n";
    
    
        }
        else{
            inputFile>>input;
            ptr = strpbrk (input, test);
            while(ptr != NULL){
    
    
                cout<<*ptr<<" ";
                 ptr = strpbrk (ptr+1,test);
    
               
            }
    
    
        }
    
        inputFile.close();
    
        return 0;
    }
    Last edited by zyberb; 04-03-2011 at 05:07 PM.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    To figure out what happens try
    Code:
    inputFile>>input;
    cout << input << endl;
    Then consider using something like getline()

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    8
    I changed the code a bit. Now i can read the numbers but i get wring sum.
    Because the program sums 100 + 34 +4?+67+7?+7+5+5?+63+3?+6+1 =297 while it should be 100+34+67+7+5+63+6+1=283??. I can se that for every space it adds the last number? how to i stop that??


    Code:
    //Oppgave 2 ōving 1
    #include <iostream>
    #include <fstream>
    #include <cstring>
    #include <cstdlib>
    using namespace std;
    /*
        oppgave2.txt contains
        100er34 67 yyy 7
        5 63 6
        1
    
    
    */
    
    int main(){
    
        ifstream inputFile;
        char input[81];
        char input2[81];
        char input3[81];
        char test[] = "0123456789";
        char *ptr;
        int sum = 0;
        //cout<<"her";
        inputFile.open("oppgave2.txt");
        if(!inputFile){
            cout<<"File error\n";
    
    
        }
        else{
            //inputFile>>input;
    
    
    
    
            while(!inputFile.eof()){
    
                 inputFile.getline(input,81);
    
                 ptr = strpbrk (input, test);
    
            while(ptr != NULL ){
    
                cout<<*ptr<<" ";
    
                sum += atoi(ptr);
                cout<<"sum: "<<sum<<" ";
    
    
                ptr = strpbrk(ptr+1,test);
    
    
            }
        }
            cout<<"\n sum:"<<sum;
    
        }
    
        inputFile.close();
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21