Jump to content
  • Advertisement
Sign in to follow this  
chockydavid1983

Can't get desired output

This topic is 4076 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone I'm doing the following exercise: Write a program that asks the user to input two real numbers n1 and n2. Compute the sum n1 + n2, the difference n1 - n2, the product n1 * n2 and the quotient n1 / n2 (assume n2 non-zero) and output the results. Your program output should be formatted as follows: Enter a real number: 64.67 Enter a real number: -14.2 64.67 + -14.2 = 50.47 64.67 - -14.2 = 78.87 64.67 * -14.2 = -918.314 64.67 / -14.2 = -4.55423 Press any key to continue... I use the following source code:

// Prompt the user to input 2 real numbers
// Then compute the sum, difference, product and quotient

#include <iostream>

using namespace std;

int main()
{
	float n1 = 0.0f;
	float n2 = 0.0f;
	float Sum = n1 + n2;
	float Diff = n1 = n2;
	float Prod = n1 * n2;
	float Quot = n1 / n2;

	cout << "Enter a real number n1: ";						// Output
	cin >> n1;												// Get Input

	cout << "Enter a real number n2: ";						// Output
	cin >> n2;												// Get Input

	// Perform sum
	cout << n1 << " + " << n2 << ' = ' << Sum;
	cout << endl;

	// Perform difference
	cout << n1 << " - " << n2 << " = " << Diff;
	cout << endl;

	// Perform product
	cout << n1 << " * " << n2 << " = " << Prod;
	cout << endl;

	// Perform quotient
	cout << n1 << " / " << n2 << " = " << Quot;

	cout << endl;											// Output another 2 new lines
}


In the console window, I get: Enter a real number n1: 64.67 Enter a real number n2: -14.2 64.67 + -14.221128000 64.67 - -14.2 = 0 64.67 * -14.2 = 0 64.67 / -14.2 = -1.#IND Press any key to continue . . . What have I done wrong in my source code please? Thanks, Chris.

Share this post


Link to post
Share on other sites
Advertisement
Quote:

float n1 = 0.0f;
float n2 = 0.0f;
float Sum = n1 + n2;
float Diff = n1 = n2;
float Prod = n1 * n2;
float Quot = n1 / n2;


You are initializing n1 and n2 to zero, and then you compute the sum, etc. before reading their values from the user, so all the results will be zero. Also note that for Quat, you have a division by zero (which should crash your program, I wonder why it doesn't).

You need to get the values for n1 and n2, and then do the calculations.

Share this post


Link to post
Share on other sites
float Diff = n1 = n2;

is not the right way to calculate the difference.


Anyway, here is the deal:
float n1 = 0.0f;
float n2 = 0.0f;
float Sum = n1 + n2;
float Diff = n1 = n2;
float Prod = n1 * n2;
float Quot = n1 / n2;


First you have 2 numbers, both 0, and you do some calculations with them.

After that, you ask for 2 numbers, which are completely ignored in the calculation, and you print the new numbers + old results.

Chance the order, so that the program calculates after it knows the numbers.
Also I couldn't find your return.

Share this post


Link to post
Share on other sites
Hi, thank you for your replies.

I adjusted my source code according to your suggestions and it does make sense what you've said. However, my output still isn't quite what it should be.

My source code is now:



// Prompt the user to input 2 real numbers
// Then compute the sum, difference, product and quotient

#include <iostream>

using namespace std;

int main()
{
float n1 = 0.0f;
float n2 = 0.0f;

cout << "Enter a real number n1: "; // Output
cin >> n1; // Get Input

cout << "Enter a real number n2: "; // Output
cin >> n2; // Get Input

float Sum = n1 + n2;
float Diff = n1 - n2;
float Prod = n1 * n2;
float Quot = n1 / n2;

// Perform sum
cout << n1 << " + " << n2 << ' = ' << Sum;
cout << endl;

// Perform difference
cout << n1 << " - " << n2 << " = " << Diff;
cout << endl;

// Perform product
cout << n1 << " * " << n2 << " = " << Prod;
cout << endl;

// Perform quotient
cout << n1 << " / " << n2 << " = " << Quot;

cout << endl; // Output another 2 new lines
}




I now get the following output:

Enter a real number n1: 64.67
Enter a real number n2: -14.2
64.67 + -14.2211280050.47
64.67 - -14.2 = 78.87
64.67 * -14.2 = -918.314
64.67 / -14.2 = -4.55423
Press any key to continue . . .

I can't see what I've done differently with the addition part of my source code compared to the rest.

Thanks, Chris.

Share this post


Link to post
Share on other sites
cout << n1 << " + " << n2 << ' = ' << Sum;


that = should have quotes like " and not like ' (sorry i dont know the english word for those)

Share this post


Link to post
Share on other sites

// Perform sum
cout << n1 << " + " << n2 << ' = ' << Sum;
cout << endl;

// Perform difference
cout << n1 << " - " << n2 << " = " << Diff;


'=' is not the same as "="

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!