You are here:

C++/C++ string help


Help with format
Help with format  
Previous question:

Hi, I have to write a program in which I ask the user to input a string, with a limited range, and then take that string and print out any characters within what was written that are digits. I have been trying to work with the isdigit() function yet I have been having a lot of trouble. I was hoping that you could help me.

Current situation:

I walked through my program based on what you said and am having trouble organizing it and getting it to print the actual numbers found, rather than just printing the number place for that character. Can you help me!?

I added an attached file with what I was attempting to do, and at what point I wasn't sure how to continue.

You have the string (character array) stored in sentenceString, and the length of the string determined and stored in length.

Now, you need to set up a loop that checks each element of the array to see if it is a digit, and if it is, print it to screen.

There is no need for an if clause before the loop, since you are going to check every element of the array.

Create a for loop that does these things:
  1) loops from 0 to the end of the array
     For this part, the format of the if clause is key. You have it very close in your screenshot, but the second term (after the first semi-colon) must be a condition . For example, i<100 , or i>=myCounter , or i<length .
     When the program hits the for clause, it 1)sets the variable, 2)tests the second parameter, and if it's true, executes the code underneath (if it's not true, it skips past the for clause), and 3) if the second parameter was true, executes the third parameter (in this case, incrementing the integer i.)

  2) test the current element of the array to see if it is a digit
     This is where you want to use isdigit in an if clause. This is the first thing that happens if the second parameter of the for clause is true. To test if the current element in question is in fact a digit, you will use your if line, with your loop counter as your array element number. So:
     if (isdigit(sentenceString[i]))  

  3) if the if clause is true (that element of the array is a digit), print it
     A simple cout of the current element of the array, with a carriage return for clarity:
     cout << sentenceString[i] << "\n";  

And that's it! Remember that everything after "These numbers were found: " is in the for loop.

When you step through this program, it should flow like this:
1) prints "Please enter a sentence"
2) waits for a sentence to be entered
3) counts the number of characters entered, and sets length equal to that number (+1!)
4) prints "These numbers were found:"
5) sets int i = 0
6) checks to see if i < length
7) if i < length, checks to see if element i of sentenceString is a digit
8) if sentenceString[i] is a digit, prints it and a carriage return
9) if i < length, increment i and return to step 6

Notice in step 3, I note that the length variable will be the actual number of characters typed, +1. That is because C++ adds an "end of file" character at the end of a string - almost like recording the Enter at the end. Most times, you don't need to use this, and in this case, just remember to account for it in your loop. It actually makes it more intuitive in this case.

I know this stuff can get confusing - I found myself getting mixed up trying to explain it, even though I have been programming for a long time. If you have any more questions, feel free to ask. Someone once said that C++ (or any language, for that matter) is a constant confusion for all who have less than 2 years experience in it, no matter who they are. Keep up the effort - it's worth it! And do let me know if this explanation is clear, or if there is something I can clarify.


All Answers

Answers by Expert:

Ask Experts


Oshi Jager


I can: - help debug code, or make it more efficient - help with concepts, key terms and commands, and object design/inheritance - give suggestions for subroutines or object calls - suggest overall organization techniques and/or data organization I have a fairly good concept of program flow, so if you have ideas, but don't know how to get started... I've had to do a lot of that, and I can help.


Primarily, 36 units in Computer Science at university. Also, I started programming games in C and C++ back when the Commodore 64 was the best game machine out there, and that makes me old... for those of you who don't know the reference. I have intermittently kept up with my programming, so try me out.

As of 2 years ago: Association for Computing Machinery (ACM)

Related classes at Cal State University: - Object Oriented Programming (3 units) [basically C/C++] - Database Design and Implementation (3) [all theory and SQL oriented] - Discrete Mathematics (3) [pure theory] - Computer Organization (3) [cpu functionality and programming] - Data Structures (3) [in C++, but also in general theory] - Computer Architecture (3) [the PC from top to bottom] - Java Programming (3) - Programming Languages (3) [history] - Telecommunications and Networking (3)

©2017 All rights reserved.