i need help urgent

so i need some help with a program im trying to do , its simple and one #include <iostream> is allowed so ill type the question and post what i attempted.the problem i face is it isnt showing what i want to output instead it shows something else.
Write a program with the file name Calculate.cpp
Your program will ask the user for a number.
After they type it in, the program will ask for ANOTHER number.
The user should be allowed to use DECIMALS for BOTH of these numbers.

When the second number has been entered, the program will add both numbers and print the answer. It will then do the same thing by subtracting, multiplying and dividing. Each operation should appear on its own line.

When your program runs, it should look like what is inside the boxes below.
The UNDERLINED text represents what has been typed in by the user.
Each box represents the SAME PROGRAM, but the user has typed in a different value in each run.


Enter your first number: 2.5
Enter your second number: 11.3

2.5 + 11.3 = 13.8
2.5 – 11.3 = -8.8
2.5 x 11.3 = 28.25
2.5 / 11.3 = 0.221239
Enter your first number: 5
Enter your second number: 3.5

2.5 + 11.3 = 8.5
2.5 – 11.3 = 1.5
2.5 x 11.3 = 17.5
2.5 / 11.3 = 1.428571







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
 #include <iostream>

using namespace std;

int main()
{
	// variable declaration
	double num1;
	double num2;
	double answer1;
	double answer2;
	double answer3;
	double answer4;
	
	// input system 
	cout << "please enter a num1 :";
	cin >> num1;
	cout << "please enter a num2 :";
	cin >> num2 ;
	//processing system
	cout<< num1 + num2;
	cin>>answer1;
	cout<< num1 - num2;
	cin>>answer2;
	cout<< num1 * num2;
	cin>>answer3;
	cout<< num1 / num2;
	cin>>answer4;
	//output
	cout <<"\n"<< num1 <<"+"<< num2 << answer1;
	cout <<"\n"<< num1 <<"-" <<num2 << answer2;
	cout << "\n"<<num1 <<"*X"<< num2 << answer3;
	cout << "\n"<<num1 <<"/" <<num2 << answer4;
	
cin.get();

return 1;
}
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
 #include <iostream>



int main()
{
	// variable declaration
	double num1;
	double num2;
	double ans1;
	double ans2;
	double ans3;
	double ans4;
	
	// input system 
	std::cout << "please enter num1 :\n";
	std::cin >> num1;
	std::cout << "please enter num2 :\n";
	std::cin >> num2 ;
	//processing system
    ans1=num1+num2; 
	ans2=num1-num2;
	ans3=num1*num2;
	ans4=num1/num2;
	
	//output
	std::cout <<"\n"<< num1 <<"+"<<num2<<"="<<ans1;
	std::cout <<"\n"<< num1 <<"-" <<num2 <<"="<<ans2;
	std::cout << "\n"<<num1 <<"*X"<< num2 <<"="<<ans3;
	std::cout << "\n"<<num1 <<"/" <<num2 <<"="<<ans4;
	
std::cin.get();

return 1;
}


Is what you were trying to do.
What you were doing was printing the ans and then asking for the user to input all of the answers, you weren't storing the actual number.
Last edited on
Side note:
2.5 + 11.3 = 8.5 //does not equal 8.5
2.5 – 11.3 = 1.5 //" 1.5
2.5 x 11.3 = 17.5 //" 17.5
2.5 / 11.3 = 1.428571 // " 1.4...
Hello RobertCalvin,

As I started working with the program I observes this:

In the section "variable declaration" you define all your variables nicely, but they are all uninitialized. This was not a problem until I started changing the program around. In the "processing system" section I put a comment on all the "cin" statements because "answer"s 1 - 4 need to be calculated not input. If you want user input for something these need to be four new variables that will eventually be compared to the "answer?" variables.

So when my compiler gave me an error that the "answer?" variables were not initialized I went back and initialized all the variables. Some will say that not all variables need to be initialized. I can see this with "num1" and "num2" as the first thing you do after defining the variable is to input something into these variables. With the variable "answer1" - 4 with the way you originally used them they do need to be initialized because when you use them in the 'output" section they have no value and cause a compiler error.

I believe that from C++11 on, could be C++98, the uniform initializer of {}s is the easiest way to initialize a variable. An empty set of {}s will initialize to zero. In the case of a "double" "0.0". The added advantage is that you can put a number between the {}s if needed. Since it is so easy to type I feel that it is a good idea to initialize numeric variables when defined. I also feel that it is a good idea to know the starting value of a variable and know that it is not garbage.

Later as I added to the program initialization was not necessary, but since it was there already I left it that way.

The "input system" section is OK, although I did change the prompt slightly.

Eventually I added the section " Calculating answers" to give "answe?"s 1 - 4 a value for later use.

Between the "input" and "calculating" sections I added a line to manipulate the output for a better look with decimal point and the amount of numbers to the right of the decimal point.

In this line std::cout << std::fixed << std::showpoint << std::setprecision(4); the "fixed" means to show a decimal number not scientific notation. The "showpoint" means to show ".0" if there is nothing to the right of the decimal point. The "setprecision(4)" tells the output how many numbers to the right of the decimal point to print, that is the number in (). This only needs to be done once before any output involving a "double" or "float".

In the "processing system" you can see what I did.

In the "output" section there are minor changes I did. You should be able to see what they are in the following code. Mostly it has to do with spaces around things like "<<" and in the string constants like " + ". This makes the code easier to read and also the output easier to read when things are not run together.

An example of my output:

please enter a num1: 2.5
please enter a num2: 11.3
13.8000
-8.8000
28.2500
0.2212

2.5000 + 11.3000 = 13.8000
2.5000 - 11.3000 = -8.8000
2.5000 * 11.3000 = 28.2500
2.5000 / 11.3000 = 0.2212


 Press Enter to continue:


As you can see the output is easier to read.

After the output I added the two lines before the "cin.get()" to make it work. Since you used "cin >> aVariable" previous to the "cin.get()" there is a new line character left in the input buffer that "cin.get()" is extracting and then moving on to end the program. The "cin.ignore" line will clear the input buffer allowing the "cin.get()" to wait for enter.

I usually offer this as a C++ replacement to people who use "system("pause")" in their program. I offer it here in place of the quick fix I made;
1
2
3
4
5
6
// <--- Used mostly for testing in Debug mode. Removed if compiled for release.
// <--- Used to keep the console window open in Visual Studio Debug mode.
// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue";
std::cin.get();

The "return 1;" for the last line is the wrong usage. A zero means that the return is normal and there was no problem with the program. Any number greater then zero meas that there was a problem and that number can be used to tell what the problem is. Also if there is more than one return statement in a program the number can help you find where the problem is.

With that I offer this revision to your code to give you an idea of what could be done:
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
#include <iostream>
#include <iomanip> // <--- Added for line 22.

//using namespace std;  // <--- Best not to use.

int main()
{
	// variable declaration
	double num1{};
	double num2{};
	double answer1{};
	double answer2{};
	double answer3{};
	double answer4{};

	// input system 
	std::cout << "please enter a num1: ";  // <--- Changed.
	std::cin >> num1;
	std::cout << "please enter a num2: ";
	std::cin >> num2;

	std::cout << std::fixed << std::showpoint << std::setprecision(4);

	// Calculating answers. Added this section.
	answer1 = num1 + num2;
	answer2 = num1 - num2;
	answer3 = num1 * num2;
	answer4 = num1 / num2;

	//processing system
	std::cout << answer1 << std::endl;
	//std::cin >> answer1;
	std::cout << answer2 << std::endl;
	//std::cin >> answer2;
	std::cout << answer3 << std::endl;
	//std::cin >> answer3;
	std::cout << answer4 << std::endl;
	//std::cin >> answer4;

	//output
	std::cout << "\n" << num1 << " + " << num2 << " = " << answer1;
	std::cout << "\n" << num1 << " - " << num2 << " = " << answer2;
	std::cout << "\n" << num1 << " * " << num2 << " = " << answer3;
	std::cout << "\n" << num1 << " / " << num2 << " = " << answer4 << std::endl;

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue: ";  //<--- Added.
	std::cin.get();

	return 0;  //<--- Changed. Anything above zero means that there was a promlem. Zero is a normal return.
}

Hope that helps,

Andy
thankyou soo much , really appreciate the help . :)
Topic archived. No new replies allowed.