I don't know how you would extract the individual integers from that input by using only getline. You have to do some further processing on it like I did above. Surely you're not restricted to only using the things you have learned?
when i tried the code with string u gave me. it gives me this error
assignment 2(1082) malloc: *** error for object 0x100005240: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Program received signal: “SIGABRT”.
sharedlibrary apply-load-rules all
[code]
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//functions
int add(int a, int b, int c, int d, int& f) //addition
{
int e= (a*d) + (c*b);
f= (b*d);
return e;
}
int sub(int a, int b, int c, int d, int e, int& f) //substraction
{
e= (a*d) - (c*b);
f= (b*d);
return e;
}
int mul (int a, int b, int c, int d, int e, int& f) //multiplication
{
e = (a*c);
f = (b*d);
return f;
}
int div (int a, int b, int c, int d, int e, int& f) //division
{
e = (a*d);
f = (b*c);
return f;
}
//main function
int main ()
{
//variables declarations
int a,b,c,d,e,f;
// main function code ....
cout << setfill( '*' ) << setw( 79 ) << '*' << endl;
cout << setfill( '*' ) << setw( 24 ) << '*' <<" Fractional Arithmetic Program "<< setfill( '*' ) << setw( 24 ) << '*'<<endl;
cout << setfill( '*' ) << setw( 79 ) << '*';
cout << endl <<endl << endl << endl;
cout << "This program will perform arithmetic on fractions. Problems should be entered like this: 2/5 - 4/7";
cout << endl << endl;
cout <<"The allowed operations are addition (+), subtraction (-), multiplication (*), and division (/)." << endl << endl;
cout <<"Please enter your problem =>";
string expression;
getline(cin, expression);
a = (int)expression[1] - 48;
b = (int)expression[3] - 48;
c = (int)expression[7] - 48;
d = (int)expression[9] - 48;
my input
*******************************************************************************
************************ Fractional Arithmetic Program ************************
*******************************************************************************
This program will perform arithmetic on fractions. Problems should be entered like this: 2/5 - 4/7
The allowed operations are addition (+), subtraction (-), multiplication (*), and division (/).
Please enter your problem =>3/4 - 6/9
assignment 2(1153) malloc: *** error for object 0x100005240: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Program received signal: “SIGABRT”.
sharedlibrary apply-load-rules all
(gdb)
Oh right, the format doesn't include the round brackets :).
You need to hence adjust the indexes for the calculations of a, b, c and d. I.e. a will use index 0, b index 2, c index 6 and d index 8. Do you understand why?
e.g. indexes for the various characters in a sample input:
2 / 5 - 4 / 7
0 1 2 3 4 5 6 7 8