![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Mar 2006
Posts: 25
Rep Power: 0
![]() |
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 (. , ? ! : . |
|
|
|
|
|
#2 |
|
Programming Guru
![]() ![]() ![]() |
#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 10:14 AM. |
|
|
|
|
|
#3 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
* 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;
} |
|
|
|
|
|
#4 |
|
Newbie
|
Yeah we're (lilmul123) wondering how to do this.
![]() |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Mar 2006
Posts: 25
Rep Power: 0
![]() |
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();
}
} |
|
|
|
|
|
#6 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Try reading the responses.
![]() |
|
|
|
|
|
#7 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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
} |
|
|
|
|
|
#8 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
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 |
|
|
|
|
|
|
#9 | |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Quote:
|
|
|
|
|
|
|
#10 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|