Daily Archives: May 24, 2012

C++ || Char Array – Determine If A String Is A Number Or Not

The following is another intermediate homework assignment which was presented in a C++ programming course. This program was assigned to introduce more practice using and manipulating character arrays.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

Character Arrays
Cin.getline
Strlen - Get The Length Of A Char Array
Isalpha
Isspace

This program first prompts the user to input a line of text. After it obtains data from the user, using a for loop, it then displays the the string to the screen one letter (char) at a time. If the current character at that specific array index is a letter, a “flag” is set, indicating that the current word which is being displayed is not a number. If the “flag” is not set, the current word is indeed a number.

This program has the ability to intake multiple words at a time, so for example, if the user input was “Hello World 2012” the program would display the output:

Hello is NOT a number!
World is NOT a number!
2012 is a number..

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output
(Note: the code was compiled three separate times to display different output)

====== RUN 1 ======

Enter some text to see if its a number or not: My Programming Notes

My is NOT a number! There are 2 letters in that word...
Programming is NOT a number! There are 11 letters in that word...
Notes is NOT a number! There are 5 letters in that word...

====== RUN 2 ======

Enter some text to see if its a number or not: May 30th 2012

May is NOT a number! There are 3 letters in that word...
30th is NOT a number! There are 2 letters in that word...
2012 is a number..

====== RUN 3 ======

Enter some text to see if its a number or not: 5 31 2012

5 is a number..
31 is a number..
2012 is a number..