Help With My Code

Project



You should write a program that prompts the user for three floating point numbers of type double (using cout to prompt the user and write to the screen, or standard output stream, and cin to read in from the keyboard, or standard input stream). Name the variables nu through n3.
Next, the program should find the sum of n1 and n3 and store it in a variable named sum. The program should find the difference between n2 and n3 and store it in a variable called diff. The output should look exactly like this (don’t worry if there are extra zeroes at the end of the output of the answers):
Please enter number 1: 4.58
Please enter number 2: 3.3
Please enter number 3: 6.12
The sum is: 10.7
The difference is: -2.82
Be sure to declare five double variables, one for each of the input values, one for the sum, and one for the difference. The program should be fairly short, with five declarations, five output statements, three input statements, and one return statement inside of main() (not necessarily in that order).




/* Author Zachary Pennace
Date : Sepmtember 15th
Profesor Stuetzle
*/


#include <namespace>
#include <iostream

using namespace std;

int main()
{


float num1 = 4.58;
float num2 = 3.3;
float num3 = 6.12;

float sum = 10.7
float diff = -2.82
cout << "please enter num1" << end1; // enter num1
cin >> num1;
cout << "please enter num2" << end1; //enter num2
cin >> num2;
cout << "please enter num3" << end1; //enter num 3
cin >> num3;
cout << "Please enter n1 + n3" << end1; // find sum of n1+n3
cin >> sum;
cout << "Please enter the diffrence n2 - n3" <<end1; //find diffrence of n2-n3
cin >> diff;
return 0;
}
What do you need help with?
does this code look good?
I'm not sure why you have #include <namespace>.

I would avoid using using namespace std;
There is already plenty written about it on why it's bad, just search it up on google.
instead have
1
2
3
4
5
6
7
8
9
10
std::cout << "please enter num1" << end1; // enter num1 
std::cin >> num1;
std::cout << "please enter num2" << end1; //enter num2
std::cin >> num2;
std::cout << "please enter num3" << end1; //enter num 3 
std::cin >> num3;
std::cout << "Please enter n1 + n3" << end1; // find sum of n1+n3
std::cin >> sum;
std::cout << "Please enter the diffrence n2 - n3" <<end1; //find diffrence of n2-n3
std::cin >> diff;


I don't think you need them in front of your floats, ints, etc. But if you're getting errors, that would probably be why.

1
2
3
float num1 = 4.58;
float num2 = 3.3;
float num3 = 6.12;

You don't need to declare these with numbers since you're overwriting them in the code. just have
1
2
3
float num1;
float num2;
float num3;

or
 
float num1, num2, num3;



Also, from what I'm reading from the task given. You aren't supposed to enter in the sum and difference, the program should do that by itself.
1
2
float sum = num1 + num2;
float diff = num3 -  num1;

^ that should do it


Also, check your spelling, you have a few errors there, which won't matter a lot, but will be the extra half mark that could get you an A+ instead of an A.

If you have any questions, feel free to ask :D
Last edited on
i am submitting it on mimir and got this error


Compiler/Debug Output
main.cpp: In function ‘int main()’:
main.cpp:28:37: error: ‘end1’ was not declared in this scope
std::cout << "please enter num1" << end1; // enter num1
^
The input of the test case:
5.5
1.8
8.8
Your code's output:
timeout: failed to run command ‘./a.out’: No such file or directory
The correct output of the test case:
Please enter number 1: Please enter number 2: Please enter number 3:
The sum is: 14.3
The difference is: -7
`diff` of correct output and your output:
*** skybox22032-SUID-1505d09f-cf34-4401-bf6c-50811d560dda/OUTPUT 2015-09-16 00:49:19.542434021 +0000
--- skybox22032-SUID-1505d09f-cf34-4401-bf6c-50811d560dda/EOUT 2015-09-16 00:49:19.398435819 +0000
***************
*** 1 ****
! timeout: failed to run command ‘./a.out’: No such file or directory
--- 1,3 ----
! Please enter number 1: Please enter number 2: Please enter number 3:
! The sum is: 14.3
! The difference is: -7
\ No newline at end of file
try replacing end1 with endl
Big important tip for all programmers.

1) Check for all closing brackets, braces, and parentheses. You forgot to close out your iostream. And as @Radar mentioned, and #include namespace isn't actually a thing.

2) You have a few misspellings that would really kill your programs. It's not end1 as in, end + the number 1. It's endl, as in end + the lowercase of the letter L. I ran it through the online compiler cpp.sh, and after fixing those, it worked fine.

EDIT: @ahmedreda got to it before me :3
Last edited on
so the project isn't asking me to declare the variables
Highly doubt it. Also looking back at your code and your professor's instructions, I'm thinking he wants you to just declare variables and have keyboard input in order to assign values to them. His example of 4.58 and the other numbers was just an example. What your program is supposed to do is it's supposed to take any range of floats or doubles, I'm not sure, and give out the sum and difference of whatever you're supposed to manipulate.
Topic archived. No new replies allowed.