Jump to content

Help with counting program

- - - - -

  • Please log in to reply
6 replies to this topic

#1
Bertan

Bertan

    Learning Programmer

  • Members
  • PipPipPip
  • 43 posts
I am suppose to make a program that asks for a few numbers and counts the total sum of all the numbers. I am new to programming and c++ and I have really no idea how to start from here. My guess is that I need to make some sort of loop that first shows the 1st number, brings the 1st number to 2nd loop, brings the sum of 1 and 2 to third and continues like that until all numbers are counted. Am I thinking right?

I am in desperately need of help to get started

#include<iostream>

#include<limits>

using namespace std;

int main()

{

const int max =5;

int a[max];


cout<<"Enter 5 integer values: ";

for(int i=0;i<max;i++)

{

cin>>a[i];//input numbers

}

for(int i=max;i<max+1;i++)

{for(int j=0;j<max;j++)


cout<<a[j]+i<<endl;


}

cin.ignore(numeric_limits<streamsize>::max(),'\n');

cin.get();

return 0;

}

Edited by Bertan, 09 September 2010 - 08:43 AM.


#2
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts

#include <iostream>

using namespace std;


int main()

{

	int i;

	int num;

	int sum = 0;

	cout << "Please enter 5 integers:" << endl;

	for (i=0;i<5;i++)

	{

		cin >> num;

		sum += num;

		cout << "Current sum: " << sum << endl;

	}

	cin.ignore();

	cin.get();

}

Is that what you mean? Or this?

#include <iostream>

using namespace std;


int main()

{

	int i;

	int num;

	int sum = 0;

	cout << "Please enter 5 integers:" << endl;

	for (i=0;i<5;i++)

	{

		cin >> num;

		sum += num;

	}

	cout << "Total: " << sum;

	cin.ignore();

	cin.get();

}


Edited by mebob, 09 September 2010 - 01:24 PM.
I did something stupid

Latinamne loqueris?

#3
Bertan

Bertan

    Learning Programmer

  • Members
  • PipPipPip
  • 43 posts
Thank you very much, it was something like the second code I was looking for.

Now I'm just curious cause I saw the code from the post you edited. Lets say the program doesnt tell how many numbers to enter. As it is now the loop will never stop and total sum will never show. I tried to put cin>> before the the for loop so it could read how many numbers that is entered and work something like this for (i=0;i+1<"numbers enteres";i++). However it didnt work.

Anyone know how to get this working?


#include <iostream>

using namespace std;

 

int main()

{

	int i;

	int num[50];

	int sum = 0;

	cout << "Add a few numnbers:" << endl;

	for (i=0;i+1<50;i++)

	{

		cin >> num[i];

		sum += num[i];

	}

	cout << "Total: " << sum << endl;

	cin.ignore();

	cin.get();

}



#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
You told it to enter 49 numbers (i+1<50 is 49 iterations) before showing the sum, it works fine. Were you meaning something else, such as infinite input, terminated by a certain key then it shows sum?
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
Bertan

Bertan

    Learning Programmer

  • Members
  • PipPipPip
  • 43 posts
But this only works if I enter 49 numbers. I want it to work no matter what numbers is entered so the 50 should be replaced with how many numbers that have been enetered but I have no clue how to do that.

#6
dbug

dbug

    Programmer

  • Members
  • PipPipPipPip
  • 155 posts
As I don't know if this is home work or not, I will give you some hints but I won't post any code.

There are two simple ways to read an indefinite number of integers. The first one is to ask the user how many numbers he will enter, store that value into a variable and use this variable in the loop.

The second alternative is to define a special number (I think the best in this case is 0) and repeat the loop until the entered number is equal to this special number. This way the user can write as many numbers as he wants and then write a 0 to exit the loop and get the final result.

In both cases is crucial to not store each read number into a static array because you don't know beforehand how many numbers you will need to store. If you define an array of 50 and the user enters 51 or more integers, you will have a buffer overflow and probably a program crash.

This program does not need to memorize each individual number, so it's better to use a single int variable to read each number and add it to the total sum, and reuse it in the next loop.

#7
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
Sorry for the late reply.
#include <iostream>

#include <cstdlib>

using namespace std;


int main()

{

    int i;

    int num;

    int icount;

    int sum = 0;

    cout << "How many numbers would you like to add? ";

    cin >> icount;

    cout << "Enter " << icount << " numbers:" << endl;

    for(i = 0;i < icount; i++)

    {

        cin >> num;

        sum += num;

    }

    cout << "Total: " << sum;

    cin.ignore();

    cin.get();

}
There; the user tells the program how many inputs he would like, and the program takes that many.
You could also load all the inputs to an array if for some reason you need to easily:
#include <iostream>

#include <cstdlib>

using namespace std;


int main()

{

    int i;

    int *num;

    int icount;

    int sum = 0;

    cout << "How many numbers would you like to add? ";

    cin >> icount;

    num = new int[icount];

    cout << "Enter " << icount << " numbers:" << endl;

    for(i = 0;i < icount; i++)

    {

        cin >> num[i];

        sum += num[i];

    }

    cout << "Total: " << sum;

    cin.ignore();

    cin.get();

}
You can do whatever you want with that array then.
Latinamne loqueris?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users