string .length()

copy string not giving length

Page 1 of 1

7 Replies - 809 Views - Last Post: 15 April 2010 - 05:43 AM Rate Topic: -----

#1 ellevador  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 136
  • Joined: 01-March 09

string .length()

Posted 14 April 2010 - 08:32 PM

int countNum(string isbn){
    int x = 10;
    int count =0;
    int c =0;
    string copyIsbn ="";
    int dashCount=0;
    for(int a=0; a < isbn.length(); a++){
        if(isbn[a]!= '-'){
            copyIsbn[c] = isbn[a];
            c++;
            count++;     
        }//end if  
        else{
            dashCount++;}
    }//end for
    cout<<"end of string"<<endl;  
         
    for(int n=0; n < copyIsbn.length(); n++){
        cout<<"TESTING"<<copyIsbn[n];    
    }
}//end function  


This program reads from a text file an ISBN # and in this function I am removing the dashes and putting putting the nums into a copyIsbn string. That part works I have tested all the indexes and produce the appropriate num. However the next for loop with for(int n=0; n < copyIsbn.length(); n++) does not produce a length num, I tested just a generic # and the loop works but not the copyIsbn string length does not.

Any help would be appreciated Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: string .length()

#2 prabh  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • Posts: 381
  • Joined: 27-December 08

Re: string .length()

Posted 14 April 2010 - 09:05 PM

View Postellevador, on 14 April 2010 - 07:32 PM, said:

int countNum(string isbn){
    int x = 10;
    int count =0;
    int c =0;
    string copyIsbn ="";
    int dashCount=0;
    for(int a=0; a < isbn.length(); a++){
        if(isbn[a]!= '-'){
            copyIsbn[c] = isbn[a];
            c++;
            count++;     
        }//end if  
        else{
            dashCount++;}
    }//end for
    cout<<"end of string"<<endl;  
         
    for(int n=0; n < copyIsbn.length(); n++){
        cout<<"TESTING"<<copyIsbn[n];    
    }
}//end function  


This program reads from a text file an ISBN # and in this function I am removing the dashes and putting putting the nums into a copyIsbn string. That part works I have tested all the indexes and produce the appropriate num. However the next for loop with for(int n=0; n < copyIsbn.length(); n++) does not produce a length num, I tested just a generic # and the loop works but not the copyIsbn string length does not.

Any help would be appreciated Thanks

I have not tested the code but i think copyIsbn is in diffrent scope

what I mean is That that you are copying data in a diffrent scope and reading its lenght in diffrent one

This post has been edited by prabh: 14 April 2010 - 09:06 PM

Was This Post Helpful? 0
  • +
  • -

#3 ellevador  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 136
  • Joined: 01-March 09

Re: string .length()

Posted 14 April 2010 - 09:10 PM

[quote name='prabh' date='14 April 2010 - 08:05 PM' timestamp='1271304328' post='990209']
I'm sorry can you elaborate on what you mean please.

[quote name='ellevador' date='14 April 2010 - 08:06 PM' timestamp='1271304409' post='990210']

View Postprabh, on 14 April 2010 - 08:05 PM, said:

I'm sorry can you elaborate on what you mean please.


When I use copyIsbn outside of the first for loop ex. copyIsbn[0], or copyIsbn[1].... it does print the correct int. So I am not sure why it is recognized and does contain information why the length is the only thing that does not produce an actual number
Was This Post Helpful? 0
  • +
  • -

#4 prabh  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • Posts: 381
  • Joined: 27-December 08

Re: string .length()

Posted 14 April 2010 - 09:11 PM

Your 'c' is not incrementing!
Was This Post Helpful? 0
  • +
  • -

#5 prabh  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • Posts: 381
  • Joined: 27-December 08

Re: string .length()

Posted 14 April 2010 - 09:16 PM

int countNum(string isbn){
    int x = 10;
    int count =0;
    int c =0;
    string copyIsbn ="";
    int dashCount=0;
    for(int a=0; a < isbn.length(); a++){
        if(isbn[a]!= '-'){
            copyIsbn[c] = isbn[a]; // here is the error you declared it above as a 
                                    //string but you are writing data into it as it was
                                   //an array strings can not be edited like this 
                                   //you have declared it as a null string try declaring it as a copyIsbn="lenght of your string"
//i.e copyIsbn="abcdefghtj"
            c++;
            count++;     
        }//end if  
        else{
            dashCount++;}
    }//end for
    cout<<"end of string"<<endl;  
         
    for(int n=0; n < copyIsbn.length(); n++){
        cout<<"TESTING"<<copyIsbn[n];    
    }
}//end function

This post has been edited by prabh: 14 April 2010 - 09:20 PM

Was This Post Helpful? 0
  • +
  • -

#6 ellevador  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 136
  • Joined: 01-March 09

Re: string .length()

Posted 14 April 2010 - 09:21 PM

c is incrementing, and this for loop
    for(int n=0; n < 10; n++){
        cout<<"TESTING"<<copyIsbn[n]; 


produces the correct output as I have changed copyIsbn.length() to a number 10, it goes through and prints each index in the string, just fine. And I do apologize if I am not correctly understanding what your saying when everything works besides the copyIsbn.length() which is a 0. It may help to know, but the string isbn is also created by using [] to input, so I do know that is possible.
Was This Post Helpful? 0
  • +
  • -

#7 ellevador  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 136
  • Joined: 01-March 09

Re: string .length()

Posted 14 April 2010 - 10:15 PM

well who knows what the deal is on this issue, but ended up creating a variable to keep count of the copyIsbn string and used that for the 'for loop'....Thats my fix.
Was This Post Helpful? 0
  • +
  • -

#8 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: string .length()

Posted 15 April 2010 - 05:43 AM

Instead of this:
copyIsbn[c] = isbn[a];


did you try this:
copyIsbn.push_back(isbn[a]);


The thing is that push_back member function automatically increments the length of the string, so it should work.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1