You are here:

C++/C++ string help


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.

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

There are a few things to keep in mind here.

Your string sentenceString[100] is an array, in other words a series of 100 characters. You will need to look at each element to determine if it is a digit or not - there is no way to skip this part.
When you need to look at an individual part of an array, you need to specify which member of the array you want.
You also have to remember that the first element of your array is always number 0, so if it is a 100 element array, as in your string here, they are numbered 0-99.

To try to make this clearer, let's step through the first part of your program. When run, it will ask the user to enter something. Now, suppose I enter:
I am going to see how this works.

The program has now set aside memory of 100 bytes, and in place 0 - the beginning - it has an "I",
in place 1 it has a white space, place 2 an "a", place 3 an "m", et cetera, so it looks like this:

I am going

So your challenge here is to set up a loop, one that looks at each character in the array in turn, and if it is a digit, prints it to the screen.

I am not going to give you all of the code here, of course - you will have to work this out. But I will give you some starting places.

First, make sure you are using an editor or compiler that shows you as much information as possible while you are writing and compiling! I don't usually work with Visual Studio, so I don't know if it as helpful as any other compiler out there, but I suggest trying some others to see if they are any more helpful to you as you write. There are several good options out there.

Second, make sure you have included the iostream library at the top of your code. You probably did, but it doesn't show in your screenshot. It might be included automatically in that compiler, but if not, you need to type this BEFORE the main():

#include <iostream>

No semi-colon needed there.

Next, you have set up a conditional:

if (isdigit(sentenceString[]));

In place of this, you need to set up a loop, to check each element of the array to see if it is a digit or not, and if it is, print it to screen. Keep in mind that you have to refer to each element individually - so for instance the first one would be sentenceString[0], the second one sentenceString[1], etc. To make the loop work, use a counter variable in place of a number. For example:

for (int i=1; i<20; i++)
   cout << i << "n";

This simply defines integer i, prints to screen the value of i and a carriage return, and then loops it until i reaches the value 20.

Now, to make this work with your program, you might want to use the actual character count of what someone typed in. You can do this with the command


In your program, define an integer for the length of the input string. Then, after the input, set this integer equal to that string length:

int length = 0;
char sentenceString[100] = "";
cout << " Please enter a sentence, tell me a story if you wish. n";
length = cin.gcount();

Now you can use the length variable to control your loop.

I want to point out here that you don't want "These numbers were found: " printing to the screen every time a digit is found, so put that before the loop.
Now, for an extra challenge, see if you can get it to print consecutive numbers without a carriage return. So if I were to enter:

Where4 art 101 Per7e?

it would read out:

These numbers were found:


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)

©2016 All rights reserved.