Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Delete a specific number from beginning of string of numbers

  1. Jul 28, 2009 #1
    I am creating a calculator and have 4 textboxes. I need to know what functions I need to delete the number '0' from a string of numbers such as, "0852" so it can look like "852." My goal is for the program to check the user input after the user has pressed the 'enter' button to go to the next textbox. I know there are functions that can check each character in a string and search for a specific character. So I just need to know the functions. I'm sure I can implement it in my program correctly. thanx in advance!!!

    P.S. This is for C++ MFC VS2005.
     
    Last edited: Jul 28, 2009
  2. jcsd
  3. Aug 8, 2009 #2
    Im not farmiliar with c++, but Id do something like this:


    Code (Text):

    string number;

    function checkFirstDigit(sting number) {
      if (first character of number is a 0) {
        number = (number with first character removed);
        return checkFirstDigit(number);
      }
      else {
        return number;
    }
    again, i dont know c++ so I dont know the syndax for comparing strings/modifying strings, so this is more simple pseudo code.
     
  4. Aug 11, 2009 #3
    Thanks for the pseudo Code, but that's about all I have done so far. I cant seem to figure this out. Im sure I would need something like a 'for loop' for an array, but what function is able to delete a specific digit in a specific position?

    PLEASE SOMEONE HELP!!!
     
  5. Aug 31, 2009 #4
    For those of you who has the same problem I had, here is the solution I came up with:

    #include "stdafx.h"
    #include <iostream>
    #include "conio.h"

    using namespace std;

    int _tmain(int argc, _TCHAR* argv[])
    {
    char num[9];

    cin >> num;

    if(num[0] == '0')
    {
    for(int i = 0; i<9; i++)
    {
    num = num[i+1];
    }
    }

    cout << num << endl;

    getch();
    return 0;
    }


    Now all I have to do is implement this into my GUI's edit boxes.
     
  6. Aug 31, 2009 #5
    When i=8, you are setting num[8] = num[9]. However, num[9] is outside the bounds of the array--it could be filled with any garbage data. This can lead to strange bugs in your program.
     
  7. Aug 31, 2009 #6

    jtbell

    User Avatar

    Staff: Mentor

    Here's a solution that uses the standard C++ 'string' type, which has member functions for searching and slicing and dicing strings:

    Code (Text):

    #include <iostream>
    #include <string>

    using namespace std;

    int main()
    {
        string digits;
        cout << "Gimme a string of digits: ";
        cin >> digits;
        int pos_first_nonzero = digits.find_first_not_of("0");
        if (pos_first_nonzero == string::npos)  // string is all zeroes
        {
            digits = "0";
        }
        else if (pos_first_nonzero != 0)
        {
            digits = digits.erase(0, pos_first_nonzero);
        }
        // If pos_first_nonzero == 0,
        // there are no leading zeroes and we don't do anything.
        cout << digits << endl;
        return 0;
    }
     
    Actually, we can simplify this a bit if we don't mind the program going through the motions of "erasing" zero characters when there are no leading zeroes:

    Code (Text):

    #include <iostream>
    #include <string>

    using namespace std;

    int main()
    {
        string digits;
        cout << "Gimme a string of digits: ";
        cin >> digits;
        int pos_first_nonzero = digits.find_first_not_of("0");
        if (pos_first_nonzero == string::npos)  // string is all zeroes
        {
            digits = "0";
        }
        else
        {
            digits = digits.erase(0, pos_first_nonzero);
        }
        cout << digits << endl;
        return 0;
    }
     
     
    Last edited: Aug 31, 2009
  8. Sep 1, 2009 #7
    Yeah i noticed that, but the way this little piece of code is going to be used is in 4 limited numeric textboxes. The user wont be able to input no more than 9 numeric characters. Of course I will be doing a lot of testing, since this is will be used in a calculator. thank you for your reply. i will keep this in mind.
     
  9. Sep 1, 2009 #8
    WOW! Yours is much safer than my messy junk, as mXSCNT pointed out. I will try to implement your example instead since my original datatype is a string, well a CString. The way i was doing it, was making me convert a CString to char. So I like your method better. Thank you all for your input. I'll you all posted.
     
  10. Sep 1, 2009 #9

    jtbell

    User Avatar

    Staff: Mentor

    Beware that 'CString' is (I think) a Microsoft thing and not a standard C++ thing, and is different from 'string'. So you'll have to convert between them. I expect that Microsoft provides string-manipulation functions for 'CString's, but I don't know anything about them.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook