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 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():
No semi-colon needed there.
Next, you have set up a conditional:
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, the second one sentenceString, 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 = "";
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: