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?
This is a discussion on Fraction within the C++ Programming forums, part of the General Programming Boards category; If someone enters 2/3 how do you figure out which is the numerator and the denominator? I know the numerator ...
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.
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
that's what i thought u needed to do but how do you search through a string?
string foo="3/4";
int location;
location=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?
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.Code:cout<<"Enter a fraction: "; string addedFraction; cin>>addedFraction;
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!
here's a version using non-STL techniques. atoi() is a standard function found in stdlib.h I believe.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;