Thread: Fraction

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    23

    Exclamation Fraction

    If someone enters 2/3 how do you figure out which is the numerator and the denominator? I know the numerator is 2 and the denominator is 3 but how do you find that out when ur given 2/3?
    Last edited by Nicknameguy; 11-18-2002 at 01:46 PM.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Read it in as a string, then parse through the string until you reach the / . You know everything until that point will be the numerator. Search after the / to find the denom.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    23

    okay

    that's what i thought u needed to do but how do you search through a string?

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    string foo="3/4";
    int location;
    location=find("/");

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    23

    Fraction Find

    Okay I understand that you have to declare whatever the user inputs as a string but what I am confused about is how to find. I have tried your "find" code but it justs messes up everything. It doesn't seem to know what find is. I think it is trying to use "find" as a function but that won't work.

    So, let's say I am working on addFraction. I am going to do something similar to this right?

    Code:
    cout<<"Enter a fraction: ";
    string addedFraction;
    cin>>addedFraction;
    once I finish that I am so confused. I kind of understand why you have to declare an integer to find what is before or after the "/" but I don't know how to write that in C++ Code.

    Can you change a character into an integer? I know you can't change a string into a fraction though.

    Thanks! Please respond back ASAP!

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    char input[80];
    char temp[80];
    int numer;
    int denom;
    
    cout << "enter a fraction like 2/3" << endl;
    cin >> input;
    
    int i;
    int j = 0;
    for(i = 0; i < strlen(input); i++)
    {
      if(input[i] == '/')
      {
        temp[j++] = '\0';
        numer = atoi(temp);
        j = 0;
      }
      else
      {
         temp[j++] = input[i];
       }
    }
    temp[j] = '\0';
    denom = atoi(temp);
     
    cout << numer << '/' << denom << endl;
    here's a version using non-STL techniques. atoi() is a standard function found in stdlib.h I believe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with fraction division function
    By trippedwire in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2004, 11:38 AM
  2. A tragedy!
    By Aakash Datt in forum C++ Programming
    Replies: 4
    Last Post: 05-01-2003, 06:57 PM
  3. Overloading insertion and extraction problem
    By Curwa in forum C++ Programming
    Replies: 1
    Last Post: 01-15-2003, 09:20 PM
  4. Help with Fraction class
    By cheeisme123 in forum C++ Programming
    Replies: 0
    Last Post: 06-04-2002, 07:48 AM
  5. reducing a fraction
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 03-13-2002, 08:56 PM