Well another semester has started almost done with all java classes, and now C++ has started. I like it so far it is shorter and somewhat easier code. I have a homework assignment which I have started on, but with just starting C++ not sure where to go. Class meets tomorrow and assignment is due Thursday. A fairly simple enough homework, but I keeping getting stumped, several errors are coming up, and we are just started learning debugging. I am using Visual Studio 2005, its what we're using in class.

Allow a user to input 2 fractions and compute their sum.
such as first fraction: 1/2
second fracton: 2/5
Sum: 9/10

My code:

1.	# include<iostream>

2.	using namespace std;
3.	/* This program will prompt the user for two fractions and then it will figure their sum in a fractional form.*/

4.	int main ()
5.	{
6.	int a;
7.	int b;
8.	int c;
9.	int d;
10.	int fraction first = a/b;
11.	int fraction second = c/d;

12.	cout<< "Enter first fraction;"<<endl;
13.	cin>> a/b;
14.	cout<< "Enter second fraction;"<< endl;
15.	cin>> c/d;
16.	cout>> "Sum ="<< ((a*d)+(b*c))/(b*d)<<endl;
10 Years
Discussion Span
Last Post by brk235

variables a, b, c and d can not be input like that. Do it like this: cin >> a >> b; You can't type the division symbol, but only the two numbers

you don't need lines 10 and 11, so you might as well delete them because they are wrong anyway.

BTW: Don't manually add line numbers when you post code because it makes it difficult for other to copy/paste into their own compiler for testing. Use code tags, which will add line numbers like this [code=c++] // your code goes here



Hi Here iam pasting code for your question....when u compile it u will get what you want....please reply me if you want more than this..

# include<iostream>

	using namespace std;
	/* This program will prompt the user for two fractions and then it will figure their sum in a fractional form.*/

	int main ()
	int a;
	int b;
	int c;
	int d;
    cout<< "Enter values for the first fraction;"<<endl;
	cout<< "Enter values for the second fraction;"<< endl;
	cin>> c>>d;
	cout<<"First fraction ="<<a<<"/"<<b<<endl;
	cout<<"Second fraction ="<<c<<"/"<<d<<endl;

	cout<< "Sum ="<< ((a*d)+(b*c))<<"/"<<(b*d)<<endl;

	return 0;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.