Getline Help

closed account (365X92yv)
I need some help to be able to input a line of text such as.....

1/2 + 4/3 - (3/2 * 1/3)

and calculate it. I'm trying to use getline but I either don't understand the compiler errors or I don't use the right parameters. Thanks a bunch for the help.
post the code (in tags please) so we can see what are you doing wrong
closed account (365X92yv)
int fracInput()
{

int fracTop, fracBot;
char fracSign;
string fraction;

cin.getline(fraction, 30, '/');
cout << fraction;
}

What I want is to read the whole line and be able to store the numerator in fracTop the operator in fracSign, and the denominator in fracBot.

-----------------------------------------------------------------------------------------------------------------
Write a program to do fractional arithmetic. The program should handle the operations of addition, subtraction, multiplication, and division. It should also reduce the answer to its lowest form (e.g. 3/9 should be 1/3). The user should be allowed to enter any number of problems.

-----------------------------------------------------------------------------------------------------------------
where are you getting the fractions from?
closed account (365X92yv)
I'll be inputting them using the iostream. The fractions are going to be user input.
should look somethin like this havn't tested this but should work

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <istream>
#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;





int iNumerator;
	char cOperator;
	int iDenominator;
	//char cPlus[] = "+";
		//char cMinus[] = "-";
		//char cMult1[] = "x"; 
		//char cMult2[] = "X";
		//char cMult3[] = "*";
		//char cDivide[] = "/";
		

 int calculator(int iNumerator,int iDenominator)
{
	
  cout <<iNumerator / iDenominator;

//cin.getline(fraction, 30, '/');
//cout << fraction;
}


main()
{
	cout << "enter numerator" end1;
cin >> iNumerator;

cout << "enter denominator";

cin >> iDenominator;

calculator(iNumerator,iDenominator);

// or just skip the function (I was writing a calculator not just a divider)  and delete function call an just write cout << iNumerator / iDenominator;

// add pause here havn't gotten that far...

// this is good practice for me btw I'm a noob too!!

// Just thought of this, this function will only return an interger value it will NOT include the remainder in the result and it will not return a float value

}
Last edited on
closed account (365X92yv)
Waiting on your reply Detroit.
My reply?



1
2
3
4
5
6
7
8
9
10
11
int fracInput()
{

int fracTop, fracBot;
char fracSign;
//string fraction;
char fraction[30]; //since you are using the C style cin.getline you must use a char array

cin.getline(fraction, 30, '/');
cout << fraction;
}

that will let you at least pull something from the input

for C-strings:
http://www.cplusplus.com/reference/iostream/istream/getline/

for the string class:
http://www.cplusplus.com/reference/string/getline/
Last edited on
Topic archived. No new replies allowed.