GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Aug-2005, 22:10
blah blah is offline
New Member
 
Join Date: Aug 2005
Posts: 13
blah is on a distinguished road

convert char to intereg


i 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  
Old 09-Aug-2005, 22:50
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by blah
i 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??
Lots of ideas. Start by reading the guidelines -- we can help you best if you know what to ask.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #3  
Old 10-Aug-2005, 01:57
blah blah is offline
New Member
 
Join Date: Aug 2005
Posts: 13
blah is on a distinguished road
Quote:
Originally Posted by WaltP
Lots of ideas. Start by reading the guidelines -- we can help you best if you know what to ask.

CPP / C++ / C Code:
#include <iostream>
using namespace std;

int main()
{

             
	return 0;
}
Last edited by LuciWiz : 26-Mar-2006 at 12:00. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #4  
Old 10-Aug-2005, 09:09
CMan's Avatar
CMan CMan is offline
New Member
 
Join Date: Jul 2005
Posts: 19
CMan is on a distinguished road
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  
Old 10-Aug-2005, 10:43
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,309
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Ya gotta have a Plan!


Quote:
Originally Posted by blah
CPP / C++ / C Code:
#include <iostream>
using namespace std;

int main()
{

             
	return 0;
}

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  
Old 12-Aug-2005, 08:06
blah blah is offline
New Member
 
Join Date: Aug 2005
Posts: 13
blah is on a distinguished road
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:
#include <iostream>
using namespace std;

int main()
{
	int one;
	int two;
	int three;

	cin >> one;

	one = 1;

	cout << one << endl;

	return 0;
}
Last edited by LuciWiz : 13-Aug-2005 at 02:16. Reason: Please insert your C++ code between [c++] & [/c++]
  #7  
Old 12-Aug-2005, 08:08
blah blah is offline
New Member
 
Join Date: Aug 2005
Posts: 13
blah is on a distinguished road
w00t

CPP / C++ / C Code:
#include <iostream>
using namespace std;

int main()
{
	int one;
	int two;
	int three;
    
	cout << "Input string you want to be converted to int: ";
	cin >> one;

	one = 1;

	cout << one << endl;

	return 0;
}
Last edited by LuciWiz : 13-Aug-2005 at 02:16. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #8  
Old 12-Aug-2005, 08:36
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,309
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by blah
w00t


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:
#include <iostream>
#include <string>
using namespace std;

int main()
{
  int one;
  int two;
  int three;
  string instring;

  cout << "Enter the string you want to be converted to int: ";
  cin  >> instring;


  cout << "You entered <" << instring << ">" << endl;
  cout << "The digit corresponding to this is ";

  if (instring == "one") {
    cout << 1;
  }
  else if (instring == "two") {
    cout << 2;
  }
  else {
    cout << "something other than 1 or 2";
  }
  cout << endl;

  return 0;
}

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  
Old 13-Aug-2005, 02:29
blah blah is offline
New Member
 
Join Date: Aug 2005
Posts: 13
blah is on a distinguished road
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  
Old 13-Aug-2005, 04:44
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,031
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by blah
1)last semester i did a retarted programming language called scheme.

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:
Originally Posted by Paul Graham
"I suppose I should learn Lisp, but it seems so foreign."

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 GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not 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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
convert char array into CString? nasaiya MS Visual C++ / MFC Forum 11 22-Aug-2005 03:13
[Tutorial] Pointers in C (Part I) Stack Overflow C Programming Language 1 08-Apr-2005 18:35
convert double to char kilgortrout C Programming Language 14 13-May-2004 07:21
convert long to pointer to char realpopeye C++ Forum 2 26-Sep-2003 10:22

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:47.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.