February 9th, 2005, 01:35 AM
|
#1 (permalink)
| | Member
Join Date: Feb 2005
Posts: 386
| C++ Noob needs help
I need help....
I'm a new student in my very first C++ program...don't laugh
I've made it through the first 5 chapters fine....until now.
My book and my classes, which is online through my hometown community college....don't give much info this problem I'm having ...so i turn to you.
Then program must take in input from the user...which is a string of characters and put an output like this....
your string has 20 characters.....
10 numbers
9 Letters
1 other (symbols)
Which, i actually want to know one thing? How do I make this blasted language know which is a letter and which is a number.....?
I know the
char s[50]
and i know that i have to use looping but .....it just don't make sense. I need someone to start me off, or give me some good reading...I'm not asking for someone to write the program.
redfox
__________________
For the Emperor, there were the Jedi. For the Evil Empire of Microsoft, there is a penguin.
|
| |
February 9th, 2005, 01:41 AM
|
#2 (permalink)
| | Kawaru wa yo!
Join Date: Oct 2001 Location: Kingsford, MI
Posts: 16,137
|
Well, you sure don't need a 50 spot array for only 20 characters!
What you want to do is make a loop that chops off the first character of the multi-type variable, then you can use if statements to check to see if the character is a number at least. To be honest, I'm not sure on the character. You might have to use keycodes or something, or maybe C++ has something built in for that.
Get what I'm saying? |
| |
February 9th, 2005, 01:57 AM
|
#3 (permalink)
| | Member
Join Date: Feb 2005
Posts: 386
|
Well,
He wanted us to us the char s[50] so that i guess it would cover a large number if the user decided to input that.
And the only file i can use is the iostream.h |
| |
February 9th, 2005, 01:58 AM
|
#4 (permalink)
| | Kawaru wa yo!
Join Date: Oct 2001 Location: Kingsford, MI
Posts: 16,137
|
Can't help you there, I'm not a C++ guy. I just know what your basic steps would be. You'll have to figure out the functions with liberal use of google or one of the C++ whiz sites.  |
| |
February 9th, 2005, 02:10 AM
|
#5 (permalink)
| | Senior Member
Join Date: Oct 2002
Posts: 557
|
maybe a for loop? just to count threw all the numbers (since there are only 10) and if it's not then it's a letter, and probably make it a function so you can pass it 1 char at a time:
void isnum(int test) {
for(int i = 0; i <= 9; i++) {
if(test == i) {
cout << "This one is a number" << endl;
else
cout << "It's something not between 0 and 9" << endl;
}
}
}
then you'd do a for loop for each item in your array
for(int j =0; j <50; j++) {
isnum(s[j]);
}
something like that, i havent compiled this or tested it but the general idea would work hehe, i might be off and it's perty messy but what do you expect for free? lol
// edit
ok since i cant think of something then not do it i'm working on it, fixed some logic above, have to initalize the array first too or it's loaded with garbage, and also it stores asci values so just need an if statement between the asci codes, 1 sec 
Last edited by bfcx : February 9th, 2005 at 02:27 AM.
|
| |
February 9th, 2005, 02:39 AM
|
#6 (permalink)
| | Senior Member
Join Date: Oct 2002
Posts: 557
| Code: // test.cpp
#include <iostream>
using namespace std;
void isnum(int test);
// Pre: test is any valid char
// Post: prints to std out if it's any number between and
// including 0 threw 9
int main(){
char s[50];
for(int a = 0; a < 50; a++) //loop initalizes array to null
s[a]='\0';
//user input
cout << "Enter 50 array" << endl;
cin >> s;
//for each element, test it
for(int j = 0; j < 50; j++) {
isnum(s[j]);
}
}
void isnum(int test) {
if(48 <= test && test <= 57)
cout << test << " num" << endl;
else
cout << test << " not" << endl;
} works, modify it to what you need it to return hehe
also you could ask the user for the number of elements they'll use (under 50) and put that number in the for loop (one with the j variable) then it wont test all the blank entries (since i initalized them to 0.... wait, i could initalize them to null, brb)  ok edited.
Last edited by bfcx : February 9th, 2005 at 02:44 AM.
|
| |
February 9th, 2005, 02:42 AM
|
#7 (permalink)
| | Member
Join Date: Feb 2005
Posts: 386
|
I just called a buddy of mine he says, i will need to use something called a "count" which my teacher hasn't gone over with me. I think you guys got the general idea, but how do i pick each number and letter off? Oh yeah, the string that the user enters is that of a straight line...another words no spaces....
12jbod(d
You have 8 characters displayed:
2 numbers
5 Letters
1 Other |
| |
February 9th, 2005, 02:46 AM
|
#8 (permalink)
| | Senior Member
Join Date: Oct 2002
Posts: 557
|
ooo, u can still do that with my code, but instead of output int "9 num" or "a not" just keep a counter ++ it when it finds one. Just need to know the asci codes for a-z and A-Z and put a if statement in |
| |
February 9th, 2005, 02:49 AM
|
#9 (permalink)
| | Member
Join Date: Feb 2005
Posts: 386
|
is this an asci code for A-Z?????
< s[i]>='A' && s[i]<='Z') |
| |
February 9th, 2005, 02:52 AM
|
#10 (permalink)
| | Member
Join Date: Feb 2005
Posts: 386
|
i'm having trouble getting the system Pause so i can read the output brb |
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |