Thread: something really simple....

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    48

    Unhappy something really simple....

    This is supposed to be an easy one, but I can't get it to work right..
    Could anyone please help?

    ------------------------------------
    Code:
    ---------------------------------
    int main ()
    {
                    int num1, num2;
    	float sum, average;
    	num1=0; num2=0;
    	sum = num1 + num2;
    	average = (num1 + num2) / 2.0;
    	cout << "This program calculates the sum and average of two input integers." << endl;
    	cout << "Please enter two integers separated by a space: ";
    	cin >> num1 ; num2;
    	cout << endl;
    	
    	// Add num1 + num2 for the sum.
    	sum = num1 + num2;
    
    	// Average of num1 and num2.
    	average = (num1 + num2) / 2.0;
    
    	// Display the sum of num1 + num2.
    	cout << "The sum of your two numbers is " << sum << endl;
    
    	// Display the average of num1 and num2.
    	cout << "The average of your two numbers is " << average << endl << endl;
    	
    
    	return 0;
    }
    Last edited by sweetly; 09-21-2003 at 09:58 PM.

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146

    Re: something really simple....

    Code:
    int main ()
    {
                    int num1, num2;
    	float sum, average;
    	num1=0; num2=0;
    	sum = num1 + num2;                    //These two lines are
    	average = (num1 + num2) / 2.0;   //doing nothing valuable.
    	cout << "This program calculates the sum and average of two input integers." << endl;
    	cout << "Please enter two integers separated by a space: ";
    	cin >> num1 ; num2;  // Here's your biggest problem.
    //Change that line to cin >> num1 >> num2;
    	cout << endl;
    	
    	// Add num1 + num2 for the sum.
    	sum = num1 + num2;
    
    	// Average of num1 and num2.
    	average = (num1 + num2) / 2.0; //It'd be better to use your sum variable rather that do the same computation twice.
    
    	// Display the sum of num1 + num2.
    	cout << "The sum of your two numbers is " << sum << endl;
    
    	// Display the average of num1 and num2.
    	cout << "The average of your two numbers is " << average << endl << endl;
    	
    
    	return 0;
    }
    That ought to fix everything necessary. Also, sum could be an int. And when you open a tag like code, you need to close it with [ /code ], except without those spaces.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    //edit: he (joshdick) was posting at the same time as me -_-

    you forgot the closing code tag
    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
       int num1, num2;
       float sum, average;
       num1=0; num2=0;
    
       sum = num1 + num2; //not necessary
       average = (num1 + num2) / 2.0;//not necessary
    
       cout << "This program calculates the sum and average of two input integers." << endl;
       cout << "Please enter two integers separated by a space: ";
       cin >> num1 >> num2;
       cout << endl;
    
       // Add num1 + num2 for the sum.
       sum = num1 + num2;
    
       // Average of num1 and num2.
       average = (num1 + num2) / 2.0; //if this doesn't work (it should), try average=static_cast<float>(num1+num2)/2.0;
    
       // Display the sum of num1 + num2.
       cout << "The sum of your two numbers is " << sum << endl;
    
       // Display the average of num1 and num2.
       cout << "The average of your two numbers is " << average << endl << endl;
    
    
       return 0;
    }
    Last edited by major_small; 09-21-2003 at 07:58 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    48
    Thank you major small & joshdick - I was able to get it to work with your guys help!

    Also thanks for the [/code]
    It's only my second post (I'm obviously VERY new to C++)
    and I did read the message about posting code, but forgot the closing code.

    Thanks!!!

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    no problem... i often forget teh closing tag... just go back and edit the post...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Glad I could help. I'm also glad that I got to this post first. It's the small things in life that make me happy
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    i'm always worried that that will happen... i do all that work, then somebody like you comes along and posts before i get a chance -_- j/k... it wasn't much work, really
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    I'm usually worried about that too, which is why I do my best to post quickly.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    48
    Wow! If you guys are really eager to help I have a few more problems to work on.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM