By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,289 Members | 1,351 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,289 IT Pros & Developers. It's quick & easy.

C inputing Fractions

100+
P: 196
Hi, im just trying to figure out a fairly simple way to input fractions into my program, they have to be in the format of 7/2.

I thought that mabey i could scan the string and then split it at slash (/) while keeping the slash.

In mabey a sort of array, i know this possible in vb but not sure how to achieve this or anyother method in C.

Any help with either my idea or any other method for inputting a fraction is greatly appreciated,

Thanks
Apr 24 '08 #1
Share this Question
Share on Google+
7 Replies


gpraghuram
Expert 100+
P: 1,275
Hi, im just trying to figure out a fairly simple way to input fractions into my program, they have to be in the format of 7/2.

I thought that mabey i could scan the string and then split it at slash (/) while keeping the slash.

In mabey a sort of array, i know this possible in vb but not sure how to achieve this or anyother method in C.

Any help with either my idea or any other method for inputting a fraction is greatly appreciated,

Thanks
use fgets to get the input string.
Then use strtok to split the string using / as the token to get the strings

Raghuram
Apr 24 '08 #2

100+
P: 196
Thanks ill give it a go
Apr 25 '08 #3

100+
P: 196
I tried your idea but used scanf instead of fgets, that work ok but when i tried splitting up the string i kept running into errors and when i didn't get any the program would crash.

Could you possible give a code example of the strtok function in use

Thanks for help is greatly apprecaited
Apr 26 '08 #4

Savage
Expert 100+
P: 1,764
I tried your idea but used scanf instead of fgets, that work ok but when i tried splitting up the string i kept running into errors and when i didn't get any the program would crash.

Could you possible give a code example of the strtok function in use

Thanks for help is greatly apprecaited

strtok reference



Savage
Apr 26 '08 #5

100+
P: 196
Yes i tried the code given in that example i.e.


from http://www.cplusplus.com/reference/clibrary/cstring/strtok.html
Expand|Select|Wrap|Line Numbers
  1. int main ()
  2. {
  3.   char str[] ="- This, a sample string.";
  4.   char * pch;
  5.   printf ("Splitting string \"%s\" into tokens:\n",str);
  6.   pch = strtok (str," ,.-");
  7.   while (pch != NULL)
  8.   {
  9.     printf ("%s\n",pch);
  10.     pch = strtok (NULL, " ,.-");
  11.   }
  12.   return 0;
  13. }
  14.  
  15.  
That worked but see i am not just trying to ouput the different tokens as soon as their split but rather use them else where in the code.

What the major problems is for me is how to assign each part of the token to another string or integer, etc.

Thanks for the help
Apr 27 '08 #6

Savage
Expert 100+
P: 1,764
Yes i tried the code given in that example i.e.


from http://www.cplusplus.com/reference/clibrary/cstring/strtok.html
Expand|Select|Wrap|Line Numbers
  1. int main ()
  2. {
  3.   char str[] ="- This, a sample string.";
  4.   char * pch;
  5.   printf ("Splitting string \"%s\" into tokens:\n",str);
  6.   pch = strtok (str," ,.-");
  7.   while (pch != NULL)
  8.   {
  9.     printf ("%s\n",pch);
  10.     pch = strtok (NULL, " ,.-");
  11.   }
  12.   return 0;
  13. }
  14.  
  15.  
That worked but see i am not just trying to ouput the different tokens as soon as their split but rather use them else where in the code.

What the major problems is for me is how to assign each part of the token to another string or integer, etc.

Thanks for the help
In this example pch will contain your desired parts of string,so if you want to convert them to a integer all you have to do is call atoi function.If you want to store it like strings then you need array of strings.E.g

Expand|Select|Wrap|Line Numbers
  1. char strings[10][80];
  2.  
  3. strcpy(strings[0],strtok(str," ,."));
  4. for(int i=1;i<10&&(pch=strtok(NULL," ,."))!=NULL;i++)
  5. {
  6.  strcpy(strings[i],pch);
  7. }
Apr 27 '08 #7

100+
P: 196
Thank you heaps it works a treat!!!
Apr 27 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.