Delete a specific number from beginning of string of numbers

Click For Summary

Discussion Overview

The discussion revolves around how to remove a leading '0' from a string of numbers in C++, specifically in the context of a calculator application using MFC. Participants explore various functions and methods for achieving this, including string manipulation techniques and considerations for user input validation.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant seeks guidance on functions to delete the number '0' from the beginning of a string, indicating they are familiar with character checking but need specific function recommendations.
  • Another participant provides a pseudo code example for checking and removing the first character if it is '0', but admits to not knowing the exact C++ syntax.
  • A later reply presents a C++ code snippet that checks for leading zeros using the standard string type, suggesting the use of member functions for string manipulation.
  • Concerns are raised about potential out-of-bounds errors in an array manipulation approach, highlighting the importance of careful indexing.
  • Some participants discuss the differences between using standard C++ strings and Microsoft-specific CString types, noting the need for conversion between them.
  • One participant expresses a preference for the safer string manipulation method suggested by another, indicating a shift in their approach based on feedback received.

Areas of Agreement / Disagreement

Participants present multiple competing views on how to handle string manipulation in C++. There is no consensus on a single best approach, as different methods are proposed and discussed.

Contextual Notes

Limitations include potential out-of-bounds errors in array manipulation, the need for conversion between CString and standard string types, and the varying levels of familiarity with C++ syntax among participants.

Who May Find This Useful

Readers interested in C++ programming, particularly those working with string manipulation in GUI applications or calculators, may find the discussion relevant.

sweetjones
Messages
44
Reaction score
0
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. thanks in advance!

P.S. This is for C++ MFC VS2005.
 
Last edited:
Technology news on Phys.org
Im not farmiliar with c++, but Id do something like this:


Code:
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 don't know c++ so I don't know the syndax for comparing strings/modifying strings, so this is more simple pseudo code.
 
swraman said:
Im not farmiliar with c++, but Id do something like this:


Code:
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 don't know c++ so I don't know the syndax for comparing strings/modifying strings, so this is more simple pseudo code.

Thanks for the pseudo Code, but that's about all I have done so far. I can't seem to figure this out. I am 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!
 
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];
}
}

count << num << endl;

getch();
return 0;
}

Now all I have to do is implement this into my GUI's edit boxes.
 
sweetjones said:
Code:
		for(int i = 0; i<9; i++)
		{
			num[i] = num[i+1];
		}
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.
 
Here's a solution that uses the standard C++ 'string' type, which has member functions for searching and slicing and dicing strings:

Code:
#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:
#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:
mXSCNT said:
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.

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 won't 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.
 
jtbell said:
Here's a solution that uses the standard C++ 'string' type, which has member functions for searching and slicing and dicing strings:

Code:
#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:
#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;
}

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.
 
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.
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
4K
Replies
5
Views
2K
  • · Replies 118 ·
4
Replies
118
Views
10K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 18 ·
Replies
18
Views
4K
Replies
24
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 8 ·
Replies
8
Views
4K
Replies
81
Views
7K