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?

Printable View

- 11-14-2002NicknameguyFraction
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?

- 11-14-2002MrWizard
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.

- 11-14-2002Nicknameguyokay
that's what i thought u needed to do but how do you search through a string?

- 11-14-2002golfinguy4
string foo="3/4";

int location;

location=find("/"); - 11-18-2002NicknameguyFraction 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;

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! - 11-19-2002eladCode:
`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;