![]() |
|
#1
|
|||
|
|||
convert char to interegi need to convert a string say "one hundred" to integer 100. or "one thousand and one" to 1001. i input a string and out put an integer.
any ideas how?? |
|||
|
#2
|
||||
|
||||
|
Quote:
__________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
|
#3
|
|||
|
|||
|
Quote:
CPP / C++ / C Code:
Last edited by LuciWiz : 26-Mar-2006 at 13:00.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|
#4
|
||||
|
||||
|
that's a start
you need to develop your own algorithm, first start by parsing the input into an array of strings maybe. Then read each member of the array and look up its numerical equivalent. (also please put your code between quotes) __________________
http://www.cheaito.com |
|
#5
|
|||
|
|||
Ya gotta have a Plan!Quote:
Well, I can't argue with the correctness of what you have so far. Now I suggest that, before you start cranking out code to fill in the blank lines, that you should have a Plan. (I keep telling people, "You gotta have a Plan!") In the field of programming, a Plan is referred to as a Program Specification. A Program Specification generally has three descriptive parts: Describe the input. Describe the output. Describe the processing required to convert the Input to the Output. The Program Specification, more generally might include programming language, development environment, target environment, target user community, etc., but I think Input-Processing-Output is all you need here. What's the input? Text (presumably in English, but it could be anything) denoting an cardinal number. Now, what is considered legal input? Let's take, for example, the number "1234". If you were writing the text for this number, so that you could test the program, would it be this? "one thousand two hundred thirty four" How about this? "one thousand two hundred and thirty four" or this? "one thousand two hundred thirty-four" I have seen checks written in all of these formats, and then some. (Also, what about upper case/lower case requirements --- is the first word capitalized? Is capitalization optional?) Decide up front which of these examples will be accepted by your program. You should also specify what the largest input numbers will be handled by the program: 99? 999? 9999? Note that for US English speakers, the number 1000000000 (ten to the ninth power) is a "billion". Not so in the land of the Mother Toungue. (That's a "milliard", I think, and a "billion" is 1000000000000 --- ten to the twelfth power.) So: first of all: decide (and describe in the Program Specification) the requirements for input. Input specifications also can include information about where the input is coming from: stdin (the command line interface), a file, or what? If it's from a file, how does the program get to know what the file name is? Now, in this case, describing the processing and describing the output are kind of trivial, but you should do it anyhow. For example in the "processing" part of the description, you could describe how you will handle illegal input (that is, what will the program do if it can't figure out how to handle the input, given the input specification). For the output digits: do you just print the digits, or do you separate them in groups of three? If they are separated in groups, do you use a comma as a separator (as we do in the Colonies) or a period, as our British and Continental cousins usually do? If you weren't given all of this in your assignment, and you can do anything you want to, then why not make it easy on yourself? Pick an input format and an output format that you think will be easier to handle. But make sure you know what you want to do before you start implementing it. Now, once you have defined the required format for the input text, sit down with pencil and paper, and work backwards from some "typical" output numbers to see what the program should handle for various cases. What would you expect to be the input to the program for, say 12. What about 23? What about 101? What about 111?, etc. Regards, Dave |
|
#6
|
|||
|
|||
|
i managed to input one and get an out put of 1, but when i do it with two i still get one.
CPP / C++ / C Code:
Last edited by LuciWiz : 13-Aug-2005 at 03:16.
Reason: Please insert your C++ code between [c++] & [/c++]
|
|
#7
|
|||
|
|||
|
w00t
CPP / C++ / C Code:
Last edited by LuciWiz : 13-Aug-2005 at 03:16.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|
#8
|
|||
|
|||
|
Quote:
I take it that your plan is to start with units and work up from there. Not a bad idea: do the simplest case first, then get to the "good stuff". What do you expect your program to do? You input an int (not a string). Then you set the int value to 1 and print it out. Yep, when I run your program, I always get 1 out of it. Now, if you want a program to convert a string to an int, you have to have a program that gets a string (not an int) from the user, then, based on that input, print out the digit corresponding to that string. I am going to give you a complete program that will give the correct answer for one or two. This only picks up the first thing you enter, and only gives the answer if it is "one" or "two" (without the quote marks, of course). They must be lower case. CPP / C++ / C Code:
Now, how would you make it work for any digit zero, one, ... up to nine? You could just continue the else if{} stuff for the remaining eight cases. Or you could define an array of ten strings initialized to {"zero", "one", ..., "nine"}, and use a loop to see if the input matched any one of them. I have no way of knowing how much material you have covered before getting this assignment, and I still don't know what the assignment actually is, so I'm just throwing out a couple of possible approaches to the single-digit problem. As I implied in a previous post, this could be quite an interesting project if the requirements are fairly sophisticated, so vectors of strings and state machine concepts could be useful here. I perceive that your level of experience is pretty limited, so I really have no idea how to try to help you more. Regards, Dave |
|
#9
|
|||
|
|||
|
1) how in the world do you know that i have an assigment??
2) i study in university. last semester i did a retarted programming language called scheme. this semester im doing c and c++ OMG W00T. no experiense in programming. |
|
#10
|
||||
|
||||
|
Quote:
In my opinion, Scheme is quite versatile. I didn't really work with this dialect of Lisp, but I did take the Common Lisp course while studying at the University. I had a background in Pascal, C and C++, and I really hated Lisp at first. "What's with all those silly parenthesis?" I thought Even a guy who ended up to write one of the best books "On Lisp" didn't like it at first: Quote:
Unfortunately, as most of the interesting classes I took in college, when the semester was over, so was that subject. On to another... But Lisp would be a very suitable language to solve your problem in. Just think about it: a list of strings containing number names and a twin list of digits? What's the best language for list processing? What I mean is: if you have trouble following Dave's suggestions while thinking in an unfamiliar programming language, maybe you could try to solve the problem in a familiar one, and then figure out how to do it in C. With help, of course. I know that's how I switched from Pascal to C. Maybe it will work for you too. Best regards, Lucian __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
Recent GIDBlog
Problems with the Navy (Chiefs) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| convert char array into CString? | nasaiya | MS Visual C++ / MFC Forum | 11 | 22-Aug-2005 04:13 |
| [Tutorial] Pointers in C (Part I) | Stack Overflow | C Programming Language | 1 | 08-Apr-2005 19:35 |
| convert double to char | kilgortrout | C Programming Language | 14 | 13-May-2004 08:21 |
| convert long to pointer to char | realpopeye | C++ Forum | 2 | 26-Sep-2003 11:22 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The