how can i extract fraction e.g. 12/4 which is stored in a char array?i mean i want to put 12 in int numerator and 4 in int denominator.
how can i extract fraction e.g. 12/4 which is stored in a char array?i mean i want to put 12 in int numerator and 4 in int denominator.
once a kohatian,always a kohatian!
probably best to use a struct/class to hold the two separate attributes in a single entity and then store a collection of objects of the user defined struct/class in whatever container you elect to use. I suppose you could consider putting the numerator in even numbered elements and the associated denominator in odd numbered elements, but I have never heard of doing it that way. Might be kinda fun to work it out though. Hmmm........
One way is to use function sscanf().
I think you can use <cstdio> in place of <stdio.h>Code:#include <stdio.h> #include <iostream> using namespace std; int main(void) { char *str = "12/4"; int n, d; sscanf(str,"%d/%d",&n,&d); cout << str << endl; cout << "n:" << n << endl; cout << "d:" << d << endl; return 0; }
you veterans are making it hard for the rookies.
Heres a simple program.
Here:
#include <iostream.h>
#include <conio.h>
#include <conio.c> // I'm using Bloodshed, need this for getch();
int main()
{
int numerator=4;
int denominator=12;
cout << numerator << "/ " << denominator << endl;
getch();
return 0;
}
Sure, but he asked how to extract it from a char array.
ok, cool