Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 8th, 2006, 8:47 AM   #1
lilmul123
Newbie
 
Join Date: Mar 2006
Posts: 18
Rep Power: 0 lilmul123 is on a distinguished road
How to go about starting this program?

Here's what I have to do. Can anyone give me an idea on what I should do to do this?

Write a program that counts the number of letters, digits (0-9), and common puncuation symbols entered by the user. Stop inputting when the user presses ENTER. Use a switch statement to categorize the characters into puncuation, digits, and letters. When the program ends, report the number of characters in each category. (If you like, simply assume that, if a character is not a digit or puncuation, it is a letter. Also, just use the most common puncuation symbols (. , ? ! : .
lilmul123 is offline   Reply With Quote
Old May 8th, 2006, 8:56 AM   #2
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
#include <iostream>
#include <cctype>
 
using namespace std;
 
int main (void)
{
// use isdigit, isalpha, etc.
return 0;
}

Edited to remove the deprecated header. oops...
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."

Last edited by Infinite Recursion; May 8th, 2006 at 9:14 AM.
Infinite Recursion is offline   Reply With Quote
Old May 8th, 2006, 9:05 AM   #3
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
* thwaps IR *

Bad, bad!

Use the cctype header instead of ctype.h - the former is a C++ library, and the latter is a C library. The only difference is that in the C++ version, everything is in the std namespace. As IR points out, this library contains a number of functions designed to do exactly what you want to do: isalpha checks whether the character passed to it is a letter, for example.

For punctuation, you'll have to check 'em manually:
switch (character)
{
    case '.':
    case '?':
    [...]
        // do stuff
        break;
}
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 8th, 2006, 9:18 AM   #4
rxbandit
Newbie
 
Join Date: May 2006
Posts: 1
Rep Power: 0 rxbandit is on a distinguished road
Send a message via AIM to rxbandit
Yeah we're (lilmul123) wondering how to do this.
rxbandit is offline   Reply With Quote
Old May 8th, 2006, 9:19 AM   #5
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Try reading the responses.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 8th, 2006, 9:20 AM   #6
lilmul123
Newbie
 
Join Date: Mar 2006
Posts: 18
Rep Power: 0 lilmul123 is on a distinguished road
well, here's what I have thus far. can you tell me if i'm on the right track or not? i dont exactly know how to use the isdigit or isalpha statements.

#include "conio.h"
#include "stdio.h"
#include "iostream.h"
#include "ctype.h"

void main()
{
char addup,dothis;
int lettercnt;
clrscr();
cout << "Please enter a bunch of characters, puncuation, or numbers: \n";
cin >> addup;
addup = getch();
{
switch(addup)
{
case 'a'||'e'||'i'||'o'||'u':
	lettercnt++;
	break;
}
{
cout << "You typed this many characters: " << lettercnt;
}
getch();
}
}
lilmul123 is offline   Reply With Quote
Old May 8th, 2006, 9:23 AM   #7
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Looks alright. A switch-case block doesn't work like that though.
switch (addup)
{
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
        // do stuff
        break;
    
    case '0':
    case '1':
    [...]
    case '9':
        // do other stuff
        break;
    
    default:
        // nothing useful happened - do other stuff
}
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 8th, 2006, 9:24 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
For punctuation, you'll have to check 'em manually:
Nah, there are all sorts of things, like 'ispunct', 'isgraph', on and on. See the <locale> header.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old May 8th, 2006, 9:26 AM   #9
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
An alternative to the switch statement for symbols would be to base if off ASCII ranges or have a string of symbols and utilize the "find_first_of" function.

 
bool isSymbol (char x)
{
	 string SYMBOLS = "X ~`!@#$%^&*()_+-={}[]:\";'<>?,./|";
	 bool result = false;
	 int location = 0;
	 
	 location = SYMBOLS.find_first_of(x);
	 
	 if (location > 0)
		result = true;
	 
	 return result;
}
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old May 8th, 2006, 9:29 AM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
isalnum
Tests whether an element in a locale is an alphabetic or a numeric character.

isalpha
Tests whether an element in a locale is alphabetic character.

iscntrl
Tests whether an element in a locale is a control character.

isdigit
Tests whether an element in a locale is a numeric character.

isgraph
Tests whether an element in a locale is an alphanumeric or punctuation character.

islower
Tests whether an element in a locale is lower case.

isprint
Tests whether an element in a locale is a printable character.

ispunct
Tests whether an element in a locale is a punctuation character.

isspace
Tests whether an element in a locale is a whitespace character.

isupper
Tests whether an element in a locale is upper case.

isxdigit
Tests whether an element in a locale is a character used to represent a hexadecimal number.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:30 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC