Thread: Program to determine sum

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    13

    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. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Quote Originally Posted by jadedreality View Post
    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)
    Last edited by adr; 10-08-2007 at 02:08 PM.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    61
    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;
    }
    what ya think about it?
    i was just bored..
    lol

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Quote Originally Posted by brietje698 View Post
    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;
    }
    what ya think about it?
    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.
    Last edited by adr; 10-08-2007 at 02:32 PM.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And why do you need to include "windows.h" in that code?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    13
    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. #9
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    That because you havent declared C at the top of your prog like you did with A and B.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Maybe you need to declare a variable c?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    13
    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. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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. #14
    Registered User
    Join Date
    Oct 2007
    Posts
    13
    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. #15
    NotSoAvgProgrammer
    Join Date
    Jul 2007
    Location
    Virginia, U.S.
    Posts
    57
    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;
    	
    }
    A quality job is not necessarily a *good* job. A quality job is, by definition, conformance to requirements. Therefore a *good* job, may not be a quality job.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Maxium sum in a subvecter
    By sniperwire in forum C Programming
    Replies: 2
    Last Post: 10-22-2008, 06:33 PM
  2. Tic Tac Toe program
    By muzihc in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 08:08 PM
  3. GPA Program Problems
    By Clint in forum C Programming
    Replies: 3
    Last Post: 04-28-2005, 10:45 AM
  4. Replies: 7
    Last Post: 05-26-2003, 05:44 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM