468,484 Members | 1,567 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,484 developers. It's quick & easy.

C inputing Fractions

196 100+
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
7 2591
gpraghuram
1,275 Expert 1GB
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
chazzy69
196 100+
Thanks ill give it a go
Apr 25 '08 #3
chazzy69
196 100+
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
1,764 Expert 1GB
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
chazzy69
196 100+
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
1,764 Expert 1GB
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
chazzy69
196 100+
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.

Similar topics

2 posts views Thread by karp | last post: by
33 posts views Thread by selowitch | last post: by
5 posts views Thread by Steffen | last post: by
2 posts views Thread by Mori | last post: by
5 posts views Thread by gubbachchi | last post: by
reply views Thread by Paddy | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.