Help me with this question

Hello

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.

Image link: http://i48.tinypic.com/262no69.jpg
At the moment i cant open up images, if you can copy and paste i could gladly help. or you could wait as i can get to the link in a couple hours.
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
closed account (iG3b7k9E)
I didn't quite understand the question in the picture, but if you could explain it in a simpler way I might be able to help.
1
2
3
4
5
6
7
8
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

Give it a try and come back with issues :)
Last edited on

1
2
3
4
5
6
7
8
9
@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. :)
@Vlad: Digits are non-alphabet characters. The pseudocode looks right to me.
Last edited on
@Da0omph
No, non-alphabetic characters are not only digits.
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..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
int main()
{
	int AB=0, NonAB=0;
	char ch;

	cout<<"Enter characters terminated by #:"<<endl;

	do{
		cin.get(ch);
		if(ch <= 0 || ch > 0)
			NonAB++;
		else AB++;
	}while(ch != '#');

	cout<<"\nNumber of alphabets is "<<AB<<endl
		<<"Number of non-alphabets is "<<NonAB<<endl;
	return 0;
}
Last edited on
Vlad's right, if he hadn't smiled so much I would have cried :)

@Ruthless: I would say this is a well written program.

At line 11, ch is going to be an ASCII value, so it isn't going to be zero (unless one of the keyboard keys is NULL).

http://www.asciitable.com/index/asciifull.gif

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.

getline: http://www.cplusplus.com/reference/iostream/istream/getline/


Last edited on
@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. :)
Last edited on
Topic archived. No new replies allowed.