Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Here is my code. Its purpose is to take two integers and find the sum, difference, and product. Then, the second part takes two fractions, and again finds the sum, difference, and product.

C++
//Assignment01: due Friday 9/23 to 8 212 E115 submission box.

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{
	goto top;
top:
	//Parts 1 & 2
	int x; int y; int sum1; int difference1; int product1; //Declares variables for the integers to be input.

	cout << "Enter a number: \n"; //Asks the user to enter an integer...
	cin >> x; //...and then assigns that value to 'x'.

	cout << "Enter another number: \n"; //Asks the user for another integer...
	cin >> y; //...and then assigns that value to 'y'.

	sum1 = x + y; difference1 = x - y; product1 = x * y; //Each function is defined, to get the sum, difference, and product of the two integers.
	cout << "\nThe sum of the two numbers is: " << sum1 << "\nThe difference of the two numbers is: " << difference1 << "\nThe product of the two numbers is: " << product1 << "\n\n";
	//The above line prints out the answers of the previous functions to the user.

	//Parts 3 & 4
	float a; float b; float sum2; float difference2; float product2; //Declares variables for the numbers to be inputed.

	cout << "Now enter a fraction (in decimal form): \n"; //Asks the user to enter a fraction (program will terminate if the '/' symbol is used to make the fraction.
	cin >> a;

	cout << "Enter another fraction (again, in decimal form): \n"; //Asks the user to enter a fraction (program will terminate if the '/' symbol is used to make the fraction.
	cin >> b;

	sum2 = a + b; difference2 = a - b; product2 = a * b; //Each function is defined, to get the sum, difference, and product of the two numbers.
	cout << "\nThe sum of the two numbers is: " << sum2 << "\nThe difference of the two numbers is: " << difference2 << "\nThe product of the two numbers is: " << product2;
	//The above line prints out the answers of the previous functions to the user.

	int n;

	cout << "\n\nPress 0 and then enter to end the program, or press 1 and enter to run the program again.\n";
	//Allows the user to view answers and then manually end the program, or to restart the program to run it again.
	cin >> n;

	if (n == 1) //If statement will take the user's input and decide whether the user wishes to run the program again or not.
	{
		system("CLS");
		goto top;
	}
	else if (n == 0)
	{
		return 0;
	}
}
Posted

1 solution

In a simple approach, you get the two integers (numerator and denominator) from the user and use them to create the float.
For example (error handling lefts as exercise)

C++
int n,d;
char sep;
cout << "enter a fraction <int>/<int>" << endl;
cin >> n >> sep >> d;
float f = (float)n/d;
 
Share this answer
 
Comments
Member 12006564 23-Sep-15 23:37pm    
Thanks so much, this really helped!
CPallini 24-Sep-15 4:14am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900