I study my first year in a uni and C++ is one of my subjects, we study basics in this course, while I was studying.. looking for questions or some problems and solve them I faced one type of questions which I couldn't solve yet.
This is a picture of the question, I hope someone write it or at least explain to me how to do it.
Question says
Write a C++ program that asks the user to input unknown number of characters terminated by '#' character.
Your program should count the number of alphabets (a-z, A-Z) and the number of non-alphabets
(i.e. digits, special characters, spaces, tabs, new lines) in the given input.
**Sample of Input/Output**
Enter characters terminated by #:
ITCS103#
Number of alphabets is 4
Number of non-alphabets is 3
Pseudo Code:
- Read a character from the buffer (char ch = cin.get();)
- If the character is a letter, increase the letter count
- If the character is a number, increase the number count
- Repeat until character is '#'
- Output the amount of letters
- Output the amount of numbers
@LowestOne
Pseudo Code:
- Read a character from the buffer (char ch = cin.get();)
- If the character is a letter, increase the letter count
- If the character is a number, increase the number count
- Repeat until character is '#'
- Output the amount of letters
- Output the amount of numbers
It is incorrect pseudo-code.:) The orginal assignment does not require to count digits. There is said to count non-alphabets characters. :)
I never tried cin.get function before, it's my first time so I don't know how to use it in a proper way.
However, I wrote the program but I faced 2 problems..
first problem is, since I don't know how to use cin.get in a proper way, I used it in a loop, I want it to read all chars at once then count alphabets and non-alphabets but I couldn't find out how.
Second problem, it counts all chars as non-alphabets..
even then, line 12 is always going to evaluate to true. "if it is less than or equal zero or greater than zero".
You want something like if (ch >= 'a' || ch <= 'z'). I'll let you figure out the uppercase, you can do it all in one if statement with proper parenthesis and operators.
I think the loop is fine. You can do cin.getline, but then you need to have a char array, and this will use more RAM (I know memory is cheap, but it isn't free). Plus, once you fill in the array, you'll have to loop through it to figure out what is in it. cin.get() in a loop is much simpler.
@Vlad: I didn't say all non-alphabet characters are digits. I said all digits are non-alphabet characters.
The structure of the pseudocode is still correctly, nonetheless. The point is that the OP knows that there should be a selection, in which one of two counters is incremented. :)