help me find out why it is like this

This is a discussion on help me find out why it is like this within the C++ Programming forums, part of the General Programming Boards category; hello, im stuck in a simple loop,i dont know why this doesnt give me the expected result! im really really ...

  1. #1
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493

    help me find out why it is like this

    hello, im stuck in a simple loop,i dont know why this doesnt give me the expected result! im really really confused!
    please help find the problem.
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
               
               string str = "sum: 5";
               
               char * cstr;
               char currentcharacter;
               char strvalue[15]; 
               
               cstr = new char [str.size()+1];
               strcpy (cstr, str.c_str());
                
               char * ptr;
               int found = str.find(':');
               int value=0,cnt=0;
               
        while( cstr[found]!='\0')
        {
               currentcharacter = cstr[ found];
               
               if ( (isspace(currentcharacter) ))
               {
                       found++;
                       continue;
               }
               
              ptr[ found] = cstr[ found];
               found++;   
                                              
        }
    
                         
                                                       
               value = atoi(ptr);
               cout<<ptr<<endl;
               cout<<value+10;
               
               
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    the ptr gets filled with garbage characters . !(or may be it doesnt ever get filled)
    Last edited by Masterx; 01-23-2009 at 09:46 AM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,433
    What are you trying to do? For example, what is the expected result and the actual result?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    well , im trying to go through the string (starting from ':' checking the whole string , if any spaces is encountered , ignore em , when you find any digits copy them in another string , after that , im converting that string to integer . and etc .
    so far it just gives me rubbish! it seems the loop is not working !!
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    1. ptr isn't allocated.
    2. You should write to ptr[0] first, then ptr[1] etc (you need another counter)
    3. You don't append a \0
    4. You don't delete any memory you allocate
    5. What does cstr.find() return if nothing is found?

    Mmm, perhaps too many clues....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your belief that ptr[found] exists is misguided.

  6. #6
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    You never allocated any memory for your ptr variable.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,433
    Quote Originally Posted by Masterx
    well , im trying to go through the string (starting from ':' checking the whole string , if any spaces is encountered , ignore em , when you find any digits copy them in another string , after that , im converting that string to integer . and etc .
    So, str is the source string. Instead of manually managing the memory of a dynamically allocated C style string, use another std::string object as the destination string. You just need to loop over the characters of the source string and append them to the destination string if they are numeric. When you are done, you can convert the string to integer.

    Alternatively, you can do without the destination string by directly "appending" the character to be in the one's place of the integer.

    A few other things to note:
    • Indent your code more consistently.
    • #include <string> since you use std::string
    • If you really wanted to use those null terminating string operations, you should have #include <cstring>
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by Salem View Post
    1. ptr isn't allocated.
    2. You should write to ptr[0] first, then ptr[1] etc (you need another counter)
    3. You don't append a \0
    4. You don't delete any memory you allocate
    5. What does cstr.find() return if nothing is found?

    Mmm, perhaps too many clues....
    tanx , you mean instead of
    Code:
    ptr[ found] = cstr[ found];
    i must write
    Code:
    int counter = 0;
    ptr[counter++] = cstr [found];
    ?

    and about
    "3. You don't append a \0
    is it not appended automaticly at the end of cstr?( i meant null terminating character( that resembles the end of the string)by writing !='\0')
    would you explain more about number 4 and 5 ?
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Maybe, but lets see if you can figure it out first
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by laserlight View Post
    So, str is the source string. Instead of manually managing the memory of a dynamically allocated C style string, use another std::string object as the destination string. You just need to loop over the characters of the source string and append them to the destination string if they are numeric. When you are done, you can convert the string to integer.

    Alternatively, you can do without the destination string by directly "appending" the character to be in the one's place of the integer.

    A few other things to note:
    • Indent your code more consistently.
    • #include <string> since you use std::string
    • If you really wanted to use those null terminating string operations, you should have #include <cstring>
    tanx , but howto loop through the "string str" string ? would this (while str!='\0') work?
    and by the way , sorry to ask, but im a noob in strings !! ,
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,433
    Quote Originally Posted by Masterx
    howto loop through the "string str" string ? would this (while str!='\0') work?
    No, since it is an idiom for null terminated strings, which std::string is (generally) not. A simple index based for loop will suffice:
    Code:
    for (std::string::size_type i = 0, len = str.length(); i != len; ++i)
    {
        // Access str[i] here.
    }
    You could also use string iterators with a generic algorithm like say, std::copy_if (or at the moment: std::remove_copy_if since std::copy_if would only be around in C++0x).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Why not use another std::string to hold you numeric characters. This could simply your life a bit.
    Code:
    std::string numericString;
    for(int i = 0; i < 5; i++){
        numericString.push_back('1');
    }//for
    Woop?

  13. #13
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by Salem View Post
    Maybe, but lets see if you can figure it out first
    tanx, that eliminated most of the garbage !only two of them appears now ( i think thats definitely has sth to with loop counters(it seems its out of the range ! it prints out the 5 (which it should , and knowing that 5 is the last character of the string , and seeing teh output as 5@#, i think this means looped more than enough, is that right?)
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  14. #14
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by prog-bman View Post
    Why not use another std::string to hold you numeric characters. This could simply your life a bit.
    Code:
    std::string numericString;
    for(int i = 0; i < 5; i++){
        numericString.push_back('1');
    }//for
    i dont know about push_back stuff! would you explain , what this snippet does!?
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,433
    Quote Originally Posted by Masterx
    i dont know about push_back stuff! would you explain , what this snippet does!?
    prog-bman's suggestion is just a duplicate of mine. The push_back appends the character to the back of the string. You might want to refer to some documentation on std::string, e.g., the entries in cppreference.com
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. could not find -lwsock32.lib
    By thomas_joyee in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2008, 12:28 PM
  3. How to find O of threads ?
    By jabka in forum C Programming
    Replies: 3
    Last Post: 03-11-2008, 12:25 PM
  4. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 07:28 AM
  5. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 08:28 AM

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