C inputing Fractions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chazzy69
    New Member
    • Sep 2007
    • 196

    C inputing Fractions

    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
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by chazzy69
    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

    Comment

    • chazzy69
      New Member
      • Sep 2007
      • 196

      #3
      Thanks ill give it a go

      Comment

      • chazzy69
        New Member
        • Sep 2007
        • 196

        #4
        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

        Comment

        • Savage
          Recognized Expert Top Contributor
          • Feb 2007
          • 1759

          #5
          Originally posted by chazzy69
          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

          Comment

          • chazzy69
            New Member
            • Sep 2007
            • 196

            #6
            Yes i tried the code given in that example i.e.


            from http://www.cplusplus.c om/reference/clibrary/cstring/strtok.html
            Code:
            int main ()
            {
              char str[] ="- This, a sample string.";
              char * pch;
              printf ("Splitting string \"%s\" into tokens:\n",str);
              pch = strtok (str," ,.-");
              while (pch != NULL)
              {
                printf ("%s\n",pch);
                pch = strtok (NULL, " ,.-");
              }
              return 0;
            }
            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

            Comment

            • Savage
              Recognized Expert Top Contributor
              • Feb 2007
              • 1759

              #7
              Originally posted by chazzy69
              Yes i tried the code given in that example i.e.


              from http://www.cplusplus.c om/reference/clibrary/cstring/strtok.html
              Code:
              int main ()
              {
                char str[] ="- This, a sample string.";
                char * pch;
                printf ("Splitting string \"%s\" into tokens:\n",str);
                pch = strtok (str," ,.-");
                while (pch != NULL)
                {
                  printf ("%s\n",pch);
                  pch = strtok (NULL, " ,.-");
                }
                return 0;
              }
              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

              [CODE=c]char strings[10][80];

              strcpy(strings[0],strtok(str," ,."));
              for(int i=1;i<10&&(pch= strtok(NULL," ,."))!=NULL;i++ )
              {
              strcpy(strings[i],pch);
              }[/CODE]

              Comment

              • chazzy69
                New Member
                • Sep 2007
                • 196

                #8
                Thank you heaps it works a treat!!!

                Comment

                Working...