Teen Programmers Unite  
 

 

Return to forum top

C++help

Posted by aberov01 [send private reply] at April 10, 2002, 04:40:48 PM

hi guys,
could you please help me with my C++ question.
i have to do it until April 11.
i did but i'm sure if it's right.

here is my program question:
Write a function that takes a string and tries to turn it
into a positive integer value. If the string has any characters that are not digits then return -1, otherwise return the integer value of the string.
For example, for the string "12345" return the int 12345.
For the string "12a45" return -1. For the string "-23"
return -1, since the dash character('-')is not a digit.
Here is the function declaration:
int stringToPositiveInt(char s[ ]);

This is what i did but i'm really confused about my program:

int stringToPositiveInt(chat s[ ])
{
if (strcmp(s, "12345") == 0)
return s;
else if (strcmp(s, "12a45") == 0)
return -1;
else
return -1;
}

I would greatly appreciate for any help
my best regards
albina

Posted by CodeRed [send private reply] at April 10, 2002, 06:28:13 PM

I assume this program is supposed to work for ANY string, not just the ones mentioned. You have to think much more generally. Off the top of my head I would do something like this:

1) Read a line of input into a string
2) Read the string char by char until a non-alpha character or a space is found
3) If the current character is alpha, move it to another string
4) When a non-alpa character or space is found, use the function atoi to convert the string to an integer

Some stuff you will need includes:
1) The getline function, which is a member function of the ifstream class
2) The isalpha function, forget what header file (maybe stdlib)
3) The atoi function, again, can't remember what header file (math maybe?)

Sorry I can't help more, but it is forbidden to do peoples work for them on these message boards

Posted by CodeRed [send private reply] at April 10, 2002, 06:30:29 PM

Oh, and for "-23" just convert it to an int also, I don't understand why you want to return a -1 because that is an integer value

Posted by Psion [send private reply] at April 10, 2002, 08:59:38 PM

My best bet is that this post is a joke.

Posted by RedX [send private reply] at April 11, 2002, 08:39:53 AM

I've a better solution. It can be solved in 100.000 IF - else's. That is as long as we stay with 5 digit strings.
(assuming 5 positions with each 10 possible digits. Giving 10x10x10x10x10 posible combinations)

Except for the cin and cout there are no function calls involved. So the performance should be fantastic.
We could even better this result by putting all the values in an array and use the string converted to int as index. Ofcourse we would first have to check if the string doesn't contain letters, so we can solve this problem recursively!

int checkString(string a)
{
   int ret = checkString(a);
   if (ret != -1)
   { 
     return array[atoi(a)];
   }
   else { return -1; }
}



:-)
Yes, I know this is T-rex shit (bullshit would be an understatement). I doubt a compiler would even accept this.

RedX
Posted by vikram_1982 [send private reply] at April 11, 2002, 09:42:31 AM

What do u want to know about this program. Is it not working????

Posted by CodeRed [send private reply] at April 12, 2002, 02:58:26 PM

Correction to my original post, an alpha character would be used to terminate the loop, not a non-alpha character, since you are looking for numbers not letters (I had it the other way around, looking for letters not numbers)

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.