3 Replies - 233 Views - Last Post: 26 September 2013 - 12:50 AM Rate Topic: -----

#1 bethiegrace529  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-September 13

I need help with a task of writing a simple program.

Posted 24 September 2013 - 05:45 PM

I don't have much for my code yet and im very new to this. I cant figure out how to do it and I'm driving myself crazy. I have to write a program that takes in ten numbers then outputs the sum of the negative numbers, the sum of the positive numbers and then the sum of all the numbers. Can anybody help me out? This is what i have and i don't think its anywhere close to right.


[input ten whole numbers
	output numbers>0, numbers<=0, and sum of all numbers
	int num1,num2, num3, num4, num5, num6, num7, num8, num9, num10 ;
	cout << "Put in ten whole numbmbers:" ;
	cin >> num1 >> num2 >> num3 >> num4 >> num5 >> num6 >> num7 >> num8 >> num9 >> num10 ;
	//i have no idea what to do now 
]

This post has been edited by modi123_1: 24 September 2013 - 05:46 PM
Reason for edit:: please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: I need help with a task of writing a simple program.

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8363
  • View blog
  • Posts: 31,052
  • Joined: 12-June 08

Re: I need help with a task of writing a simple program.

Posted 24 September 2013 - 05:47 PM

Typically.. if you need to sum up numbers and store that value somewhere you should declare a variable for the 'sum' and, I guess in your case (since you are hard coding everything), hard code it getting the sum of num1+num2+..+num10.
Was This Post Helpful? 0
  • +
  • -

#3 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 629
  • View blog
  • Posts: 2,105
  • Joined: 31-December 10

Re: I need help with a task of writing a simple program.

Posted 24 September 2013 - 06:26 PM

You would be better off using arrays, loops, and possibly functions to separate the code.
Was This Post Helpful? 0
  • +
  • -

#4 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,764
  • Joined: 20-September 08

Re: I need help with a task of writing a simple program.

Posted 26 September 2013 - 12:50 AM

Typically the very first C++ program demo'd ...

shows how to print a simple line of text on the console screen.

The text printed out on the console screen is often just: "Hello World!"

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello World!" << endl << endl;

    cout << "Press 'Enter' to continue/exit ... ";
    cin.get(); // keep 'Window' open until 'Enter' key is pressed ..
}




Then a next step could be to get some input from the user via the keyboard ...

and to then print that input on the console screen.


#include <iostream>
#include <string>

using namespace std;


int main()
{
    cout << "Please enter your name: ";

    string name; // get a string to hold some text ...
    cin >> name; // enter some text and see what happens


    cout << "Are you really: " << name << "?" << endl;


    // here we 'eat' the above left over '\n' char 
    // ( and any other left-over char's there )

    getline( cin, name );

    cout << "Please enter your WHOLE name: ";
    getline( cin, name ); // all your names and see what happens

    cout << "Are you really: " << name << "?" << endl;

    cout << "Press 'Enter' to continue/exit ... ";
    cin.get(); // keep 'Window' open until 'Enter' key is pressed ...
}




Then a next (BIG) step might be like 'your problem' ...

that combines getting input IN A LOOP (using a 'while loop' )

testing the input using an 'if' statement ...

to do some data processing on each item of data that was input ...

This may help you to get started ...

Enjoy!


// takeIn10Nums.cpp //

/*
	I have to write a program that takes in ten numbers ...
	
	 * then outputs the sum of the negative numbers, 
	 * the sum of the positive numbers 
	 * and then the sum of all the numbers ...

*/

#include <iostream> // so you can use the C++ library cin, cout, etc...

using namespace std; // so we can code 'cout' instead of 'std::cout' ... etc


int main() // all C++ programs have a 'main; function where the program starts
{
    // declare some variables that we need ...
	int sumPos = 0; // MUST set sums ro zero to start the summing
	int sumNeg = 0; // MUST set sums ro zero to start the summing
	int tmp ;
	
	cout << "We are now going to loop to get 10 integers ...\n\n";
	
	int i = 0; // set counter to initial value of zero ...
	while( i < 10 ) // check value of counter at top of WHILE LOOP ...
	{
		cout << "Please input number " << i+1 << " : ";
		cin >> tmp;
		if( cin.good() )
		{
			if( tmp >= 0 ) sumPos = sumPos + tmp; // since num was pos ...
			else sumNeg = sumNeg + tmp;
			++i; // ok, we have a good value, so increment counter
		}
		else // Oops ... some bad data was entered above, so ...
		{
			cin.clear(); // clear any/all cin error flags set above
			cout << "\nNOT valid integer input ... try again please.\n";
		}
		
		cin.sync(); // 'keeping cin stream flushed as we go' ... 
		
	} // end of WHILE LOOP ...
	
	
	
	// when reach here, we had 10 valid numbers,
    // so print out the results ...
    
    cout << endl; // prints a blank line
	
	cout << "The sum of all the positive numbers was: " << sumPos << endl;
	cout << "The sum of all the negative numbers was: " << sumNeg << endl;
	cout << "The grand sum of all the numbers was: " << sumPos + sumNeg << endl;
	
	cout << "\nPress 'Enter' to continue/exit ... ";
	cin.get(); // Keep Window open by waiting for 'Enter key' to be pressed ... 
}


Was This Post Helpful? -2
  • +
  • -

Page 1 of 1