473,898 Members | 2,803 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with a loop that's suppose to remove all non-digits

25 New Member

I'm having a problem explaining why this loop does what it does.

Expand|Select|Wrap|Line Numbers
  1. string isbn = "--0---13-6--15--250-3";
  3.     // Go through isbn, remove any non digits
  4.     for ( int x = 0 ; x < 10 ; x++)
  5.     {
  6.         if (!isdigit(isbn[x]))
  7.             isbn = isbn.erase(x, 1);
  8.     }
  10.     cout << isbn << endl;
output is -0-136-15-250-3

Why doesn't it remove the other dashes?

May 7 '09 #1
12 2039
687 Contributor
Because x is incremented even if current character is erased. If character after it is the dash, it remains in string.
May 7 '09 #2
11,448 Recognized Expert MVP

Don't increment x when you've just removed a non-digit.

kind regards,

May 7 '09 #3
25 New Member
Thanks to both of you for the explanation and solution!

I made my new loop:
Expand|Select|Wrap|Line Numbers
  1. string isbn = "0-13-613950-X";
  3.     // Go through isbn, remove any non digits
  4.     int x = 0;
  5.     while (x < 10)
  6.     {
  7.         if (isbn[x] == 88 || isdigit(isbn[x]))
  8.             x++;
  9.         else
  10.             isbn = isbn.erase(x, 1);
  11.     }    
  12.     cout << isbn << endl;
May 7 '09 #4
11,448 Recognized Expert MVP
What is that '88' supposed to mean? Never use magic numbers in your code; use the symbolic character values, such as 'X'.

kind regards,


ps. your loop condition is wobbly at best.
May 7 '09 #5
25 New Member
advice noted!

any suggestions on how to make it better? I noticed when I entered a number such as 0-13-613950------X, the program would just halt or possibly be in a infinite loop.


nevermind the last part of it, only occurs when my string has less than 10 digits
May 7 '09 #6
25 New Member
Now I'm wondering how I would process the the string if there were less than 10 digits. Seems like I would have to remove all the things I don't want then get the size of the new string, but seems like I would need to know the size of the string with all the things I don't want from the beginning.
May 7 '09 #7
11,448 Recognized Expert MVP
While x is less than the length of the string ...

kind regards,

May 7 '09 #8
25 New Member
wow its late, I just get caught up in for loops too much and never really think of while loops. Anyways, I decided to scrap the for and while loops and use a recursive method.

Expand|Select|Wrap|Line Numbers
  1. string remove(const string &s)
  2. {
  3.     if (s.size() != 0)
  4.     {
  5.         if (s.size() == 1)
  6.         {
  7.             if(isdigit(s[0]) || s[0] == 'X' || s[0] == 'x')
  8.                 return s;
  9.             else
  10.                 return "";
  11.         }
  12.         else
  13.         {
  14.             if (isdigit(s[0]) || s[0] == 'X' || s[0] == 'x')
  15.                 return s.substr(0,1) + remove(s.substr(1));    
  16.             else
  17.                 return remove(s.substr(1));
  18.         }    
  19.     }
  20.     return "No String";
  21. }
May 7 '09 #9
11,448 Recognized Expert MVP
You can do better than that: twice that complicated if-condition isn't needed.

kind regards,

May 7 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Funnyweb | last post by:
I have a database table, which has field that could contain a single integer or a list of comma separated integers. Is it possible to match each row of that field against an array of integers and return those rows where any of the integers in that field are in my array? For example suppose I did the following: arInts = array(2,4,6,8,10);
by: Ryan Lowe | last post by:
i thought id ask here before wirting a PEP, if people thought it would be a good enhancement to allow if clauses in regular for-statements like so: >>> for x in y if x < 10 : is semantically equivalent to >>> for x in : but is shorter and easier to read. basically, the if works as a filter for
by: serge calderara | last post by:
Dear all, I have an application which is suppose to start another executable process. As soon as that process is running, I need to retrive its handle. The problem of the particular process I am starting is that it has a welcome window first which gets displayed and then the real windows after a while,in other words it means that the process name is the same, but the handle I need to retrive is the one from the final window and not the...
by: William Payne | last post by:
Ok, in my program I have a std::list<Document*>, where Document is one of my own classes. I need to go through this list and check each item if it's ready for deletion. If it's not, skip to next, if it's ready I take steps to delete it. The thing is that the list is part of a gui application and the system removes the Document* from the list during the execution of the loop body. That means I cant use iterators to iterate over the list....
by: stu_gots | last post by:
I have been losing sleep over this puzzle, and I'm convinced my train of thought is heading in the wrong direction. It is difficult to explain my circumstances, so I will present an identical make-believe challenge in order to avoid confusing the issue further. Suppose I was hosting a dinner and I wanted to invite exactly 12 guests from my neighborhood. I'm really picky about that... I have 12 chairs besides my own, and I want them all...
by: na1paj | last post by:
here's a simple linked list program. the DeleteNode function is producing an infinit loop i think, but i can't figure out where.. #include <stdio.h> typedef struct { char *str; //str is a dynamic array of characters int length; //number of characters } String;
by: Naren | last post by:
Hello All, Can any one help me in this file read problem. #include <stdio.h> int main() {
by: kevin | last post by:
Is that even possible? I am creating a web service in .NET to expose some already created .NET programs to other groups. One group is writing the client in PERL, and thus wishes the wsdl schema to not be sequenced. (PERL hashes do not retain order information) First, the w3 specs don't mention the sequence in any detail - its just there in the examples, which makes me wonder if removing the sequence tag is even supportable. Second,...
by: rover8898 | last post by:
Hello all, I have a programming problem/challenge. Let explain my scenario: I have a C program (distributed accross many files and functions) that is responsible for handling hardware. This piece of hardware (a microcontroller) can do several lengthy procedures depending on the commands it receives. The problem arises when the hardware receives a "stop command". The
by: Maria Mela | last post by:
Hello everyone... Anybody can help me on this project?? Thks for your time... Requirements Information: of the Card Each card will have a set of information associated. These information include information of the usuary, last transactions and balance. Thus for each card, the following information must exist in the system:
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.