# Thread: Program to determine sum

1. ## Program to determine sum

Just a bit of background info: I am in intro to computer science, and every other computer science class is learning word, excel, powerpoint, etc. Then I am expected to know how to write a program in c++.

Anyway.

I have no idea what I'm doing.

Our assignment is to write a program that will output the sum of 3 integers.

I don't even know how to get started because my teacher won't assist me.

Help???

2. Can you make a program that does absolutely nothing except compile? Start with that. If you don't know how to do that you'll want to start from scratch with your book or other reference material.

Once you can make an empty program, see if you can create three integers. I assume you are supposed to get the integers from the user of the program? If so, add code to get one integer and get it to compile and run. Add code to output that integer and get it to compile, run, and work. Then make the code get three integers and get itto compile and run. Then try to add the three and get it to compile and run. Then output the sum and see if it works.

If you get stuck on a specific problem, post the code you have and the specific problem and we can probably help.

Just a bit of background info: I am in intro to computer science, and every other computer science class is learning word, excel, powerpoint, etc. Then I am expected to know how to write a program in c++.

Anyway.

I have no idea what I'm doing.

Our assignment is to write a program that will output the sum of 3 integers.

I don't even know how to get started because my teacher won't assist me.

Help???
If you are trying to make this in C++ you could try looking here and here. Should help you get started (did me)

4. Err:
Code:
```#include <windows.h>
#include <iostream>
#include <fstream>

using namespace std;

int Int1;
int Int2;
int Int3;
int Result;

void main(){
cout << "Enter Int 1: " << endl;
cin >> Int1;
cout << "Enter Int 2: " << endl;
cin >> Int2;
cout << "Enter Int 3: " << endl;
cin >> Int3;
Result = Int1 + Int2 + Int3;
cout << "Sum is: " << Result << "." << endl;
}```
i was just bored..
lol

5. Originally Posted by brietje698
Err:
Code:
```#include <windows.h>
#include <iostream>
#include <fstream>

using namespace std;

int Int1;
int Int2;
int Int3;
int Result;

void main() {
cout << "Enter Int 1: " << endl;
cin >> Int1;
cout << "Enter Int 2: " << endl;
cin >> Int2;
cout << "Enter Int 3: " << endl;
cin >> Int3;
Result = Int1 + Int2 + Int3;
cout << "Sum is: " << Result << "." << endl;
}```
i was just bored..
lol
Dont use void main just use int main; (its in blue) also you dont need the ones in red either.

6. And why do you need to include "windows.h" in that code?

--
Mats

7. >> what ya think about it?
I think we try not to give complete answers to homework assignments here.

Luckily, that code won't get a good grade in many classes and will fail the assignment completely in others, so ifthe OP uses it without understanding it then he or she will likely not get rewarded much for doing so.

8. I've figured out what I'm supposed to be doing a little more.

I know this is probably not very good, but all it has to do is work for me to get credit for it. However, it said 0 errors when all I had was A & B, but when I added C it won't work. I'm not sure why? Well here's what I have so far.

Code:
```#include    <iostream>
#include    <ostream>
#include    <istream>

int main()
{
double  a;
double  b;

std::cout
<< "This program displays the sum of three numbers: A, B, and C." << std::endl;

std::cout << "Number A, please? ";
std::cin >> a;

std::cout << "Number B, please? ";
std::cin >> b;

std::cout << "Number C, please? ";
std::cin >> c;

std::cout << "The sum is " << a + b + c << "." << std::endl;
}```

I also have to ask "Do you want to do it again? Press 1 for yes, 2 for no."

Any pointers?

9. That because you havent declared C at the top of your prog like you did with A and B.

10. Maybe you need to declare a variable c?

--
Mats

11. >> I also have to ask "Do you want to do it again? Press 1 for yes, 2 for no."
What kind of loops do you know? You'll need to put the current code inside a loop and continue the loop if the user enters 1.

As far as your current code goes, it looks good. One thing to note, though, your assignment says to output the sum of three integers, but your code uses double. If you want integers, use int.

12. There are 2 errors still, but am I on the right track at all??

Code:
```#include    <iostream>
#include    <ostream>
#include    <istream>

int main()
{
char  Y , N;

int  a;
int  b;
int  c;

std::cout
<< "This program will display the sum of three numbers." << std::endl;

std::cout << "Enter an integer: ";
std::cin >> a;

std::cout << "Enter an integer: ";
std::cin >> b;

std::cout << "Enter an integer: ";
std::cin >> c;

std::cout << "The sum is " << a + b + c << "." << std::endl;

std::cout << "Would you like to try again? 1 for Yes, 2 for No. " << std::endl;

std::cin >> Y , N

;while (Y == "1");
;return (Y);

}```

13. You fixed the declaration of c and you switched to ints. Good. Did that code work for you? Always test that part first before adding a new feature (in this case adding the yes/no continue stuff). It looks fine to me, I just want to make sure you tested it and ran it.

>> ;while (Y == "1");
Looks like you picked a while loop? Do you remember the correct syntax of the while loop? Feel free to look it up. You'll want to make a block of code, which means adding braces around the code you want to repeat in your loop.

>> std::cin >> Y , N
You are asking the user to answer one question: Do you want to do it again? Therefore, you will need only one variable to hold the answer, not two. So fix it so that you have only one variable for the answer and you read into that one variable instead of trying to read into Y and N.

14. Okay I've changed it around a little, it works but not like it's supposed to...when I press 1 for Yes it exits instead of letting me try again.

Code:
```#include    <iostream>
#include    <ostream>
#include    <istream>

int main()
{
char  y;

int  a;
int  b;
int  c;

std::cout
<< "This program will display the sum of three numbers." << std::endl;

while (1 == Y);	{

std::cout << "Enter an integer: ";
std::cin >> a;

std::cout << "Enter an integer: ";
std::cin >> b;

std::cout << "Enter an integer: ";
std::cin >> c;

std::cout << "The sum is " << a + b + c << "." << std::endl;

std::cout << "Would you like to try again? 1 for Yes, 2 for No. " << std::endl;

}

return 0;

}```

15. it doesn't look like you ask for the 1
Code:
```#include <iostream>

int Y;
int  a;
int  b;
int  c;

int main()
{

Y = 1;  // initialize y

std::cout
<< "This program will display the sum of three numbers." << std::endl;

while (Y == 1)	{

std::cout << "Enter an integer: ";
std::cin>> a;

std::cout << "Enter an integer: ";
std::cin >> b;

std::cout << "Enter an integer: ";
std::cin >> c;

std::cout << "The sum is " << a + b + c << "." << std::endl;

std::cout << "Would you like to try again? 1 for Yes, 2 for No. " << std::endl;
std::cin >> Y;
std::cout << std::endl;

}
std::cin.get();

return 0;

}```