home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 1707
Discussions: 188,401, Posts: 2,243,608, Members: 232,631
Old February 9th, 2005, 01:35 AM   Digg it!   #1 (permalink)
Member
 
redfoxstorm's Avatar
 
Join Date: Feb 2005
Posts: 386
Unhappy
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.

redfoxstorm is offline   Reply With Quote
Old February 9th, 2005, 01:41 AM     #2 (permalink)
Kawaru wa yo!
 
Whir's Avatar
 
Join Date: Oct 2001
Location: Kingsford, MI
Posts: 16,137
Blog Entries: 7
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?

Whir is online now   Reply With Quote
Old February 9th, 2005, 01:57 AM     #3 (permalink)
Member
 
redfoxstorm's Avatar
 
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

redfoxstorm is offline   Reply With Quote
Old February 9th, 2005, 01:58 AM     #4 (permalink)
Kawaru wa yo!
 
Whir's Avatar
 
Join Date: Oct 2001
Location: Kingsford, MI
Posts: 16,137
Blog Entries: 7
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.
Whir is online now   Reply With Quote
Old February 9th, 2005, 02:10 AM     #5 (permalink)
Senior Member
 
bfcx's Avatar
 
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.
bfcx is offline   Reply With Quote
Old February 9th, 2005, 02:39 AM     #6 (permalink)
Senior Member
 
bfcx's Avatar
 
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.
bfcx is offline   Reply With Quote
Old February 9th, 2005, 02:42 AM     #7 (permalink)
Member
 
redfoxstorm's Avatar
 
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
redfoxstorm is offline   Reply With Quote
Old February 9th, 2005, 02:46 AM     #8 (permalink)
Senior Member
 
bfcx's Avatar
 
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
bfcx is offline   Reply With Quote
Old February 9th, 2005, 02:49 AM     #9 (permalink)
Member
 
redfoxstorm's Avatar
 
Join Date: Feb 2005
Posts: 386
is this an asci code for A-Z?????

< s[i]>='A' && s[i]<='Z')
redfoxstorm is offline   Reply With Quote
Old February 9th, 2005, 02:52 AM     #10 (permalink)
Member
 
redfoxstorm's Avatar
 
Join Date: Feb 2005
Posts: 386
i'm having trouble getting the system Pause so i can read the output brb
redfoxstorm is offline   Reply With Quote
Reply


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

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
starting a web server sr71000 Webmastering and Programming 28 August 12th, 2004 12:45 AM
Finding my Review. bharlow General Tech Discussion 4 September 5th, 2003 02:34 PM
Best Linux? durante Linux and Unix 26 August 10th, 2003 01:23 AM
Is everybody n00bs here? x86MeOneMoreTime Linux and Unix 99 July 20th, 2003 07:20 PM
Let's all say hi to . . . George VII IMO Community 6 June 30th, 2002 04:36 PM

Most Active Discussions
Is It Just Me? (2906)
3-days in and no threads about Gaza (161)
Unarmed man on his stomach shot by .. (6)
New Build ( Finally ) (7)
CPU wont boot (7)
Building a gaming computer advice (5)
I think I just killed my computer w.. (24)
RCA 52Inch HDTV wont turn on (5)
Folderchat Weekday thread (444)
Recent Discussions
Laptop proccesor to desktop mob.. (2)
Please help! multiple problems! (4)
RCA 52Inch HDTV wont turn on (5)
New Build ( Finally ) (7)
Common Spyware Solutions (97)
How do you move a hard-drive to.. (4)
What is the best external enclo.. (0)
Partition Magic 7.0 (Unallocate.. (17)
For cheap price and good qualit.. (1)
Blackberry Storm, Gears of War .. (1)
Core 2 Quad Q9550 system (3)
COWBOOM Ripoff! Used Laptop w/$.. (4)


All times are GMT -4. The time now is 04:08 AM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28