#include<iostream>
usingnamespace std;
constint MAX_NUMBERS = 6;
int main()
{
int numbers[MAX_NUMBERS] = {0};
for (int i = 0; i < MAX_NUMBERS; i++)
{
cout << "\nEnter number[" << i << "] ";
cin >> numbers[i];
}
int sum = 0;
// TODO calc and output the sum from numbers
return 0;
}
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/ http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
As Thomas1965 pointed out
Why do you declare an array with 5 numbers when you need to store 6 numbers ?
And the instructions
Write a program that accepts 6 integers from the user.
Puts them into an array.
And outputs the sum.
When I ran the program it stored four numbers not five and not the six that you need.
For the most part the program works, but it does need some changes to work right.
For a start:
1 2 3 4 5
while(k++ < 5)
{
cout<<"enter number "<<endl;
cin>>arr[k];
}
As it is "k" will only store 4 numbers in the array and element zero of the array is never used. The whole while condition needs fixed. The same will have to be done with the next two while loops and the line m = 4; will need to be changed.
It was using five numbers because it was an example. I thought I stated that. But Thomas1965, the code you provided me with only allows the user to enter 6 numbers. How can I make the output be the sum of those 6 numbers?
I think we both understand the what you posted is an example.
As I stated earlier the program works. It just needs changed to work properly.
In all three while loops the concept works, but the way they are done is wrong and does not work as is.
I have finished the program and made it work. I have given you a hint about what is wrong "k++" and am waiting to see what you know and understand instead of just giving you the answer.
All I did was to make changes to what you posted without changing the whole code.
What is the point of the exercise? What are you trying to learn about? Arrays, loops or how to use both?
The original program is hardly an example - it has so many problems it would be better to throw it away and start again. If your professor gave it to you, I certainly hope the instructions were - "see how many errors you can find in this poorly-written code".
The compiler warns of undefined behaviour in two different places:
In function 'int main()':
25 20 [Warning] iteration 4 invokes undefined behavior [-Waggressive-loop-optimizations]
23 15 [Note] within this loop
16 26 [Warning] iteration 4 invokes undefined behavior [-Waggressive-loop-optimizations]
14 15 [Note] within this loop
I don't know how to write it at all. You see how poorly written my professor examples are. I don't even know where to begin to find out what is wrong with the k++. This is much harder for me than others. Sorry.
It sound like you need to revisit your textbook for awhile.
What I was trying to show you earlier:
Write a program that accepts 6 integers from the user.
Puts them into an array.
And outputs the sum.
As you see it now looks like three different steps for the program. Work on one and when it is working move on to the next. Do not try to visualize the whole program at once.
Thomas1965 showed you a for loop that will work well for the input.
Work something up and post it. Even if it is wrong we can work on fixing and understanding how it works.
Very good. Now follow the links from my earlier post about using code tags and you will be one step close. Then we can work on blank lines and indenting to make your code easier to read.
I have revised your code a little to show you a different way of writing this program. Read the comments in the program for what I did.
#include<iostream>
// This is not a problem for this program, but it WILL be in the future. Best to learn using "std::"
// early when it is easier. This will help explain the "using namespace xxxx" problem and even when it is
// useful http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/
//using namespace std;
int main()
{
// This allows you to change the size of the array and the condition in the for loops from one place.
// Making this variable a const allows you to use this name in defining an array which needs a constant
// number. Regular variables can ot be used to define the size of an array only a constant number like
// "6" or a variable defined as const.
// This is defined in main because that is where it is used. A global variable will work, but not necessary.
constint MAXSIZE{ 6 };
int arr[MAXSIZE]{}; // variable declaration <--- Added the {} to initialize the array to zeros.
int val = 0;
std::cout << "Enter six numbers, pressing enter after each value: \n";
for (int i = 0; i < MAXSIZE; i++)
{
// This line is a nicer way of letting the user know what to enter. Notice the "i + 1" this is because
// the for loop starts at zero.
std::cout << "\n Enter number " << i + 1 << ". "; // <--- The space at the end and nothing for a new line will allow the cin to be on the same line.
std::cin >> arr[i];
}
for (int i = 0; i < MAXSIZE; i++)
val = val + arr[i]; // <--- This line is indented to show it is part of the for loop.
// <--- Blank line here to make it easier to read and show it is not part of the for loop.
std::cout << "The sum of the numbers entered are: " << val << std::endl;
return 0;
}
See what you think and let me know if you have any questions. Load it up and run it and see how it compares to what you did. The program will function the same it is just dressed up a bit.
Note: I made the changes, but have not had the chance to test it yet to make sure I did not make any typos or do something stupid. Since I did not make any major changes to the code it should work.
Hope that helps,
Andy
EDIT: Sorry I missed line 21. That is what happens with copy and paste.