Adding numbers in arrays using input | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Adding numbers in arrays using input

I wrote a program that is supposed to take user input and put the input into an array, and when it is terminated it will add up the numbers in the array. Here is the code, if you can help I will be very grateful. I know it's a lot of (probably crappy) code to look at all at once, but thanks in advance. --------------------------------------------------------------------------------- #include <iostream> using namespace std; int main() { bool play = true; int ans; int array[]{}; int sum; while(play=true){ for(int x=0;;){ cout << "input numbers, input -10 to stop and add them all\n"; cin >> ans; if(ans==-10){ sum += array[x]; cout << sum; play = false; }else{ ans = array[x]; x++; } } } return 0; } ---------------------------------------------------------------------------------

23rd Jan 2017, 5:23 PM
neon_cabbage
neon_cabbage - avatar
7 Answers
+ 7
My code was based on the code you provided, which I'm glad you did. It's hard to find people who actually did some work around before asking for help here. Have a cookie šŸŖ There are a few reasons as to why your code doesn't work. - int array[]{}; Arrays in C++ must have their size initialized. If you do not have the size specified, you need to at least fill it with elements so that the system knows how much space to allocate to the array. In my code, I directly used size of 100. This isn't the best way - it's assuming the user will not input more than 100 numbers to add. - int sum; Sum must be initialized to 0 before calculations. Else, there will be junk value inside the variable and bugs will occur. - while(play=true) The sign here should be == instead of =. if(ans==-10) { sum += array[x]; cout << sum; play = false; } else { ans = array[x]; x++; } The errors here are multilayered. Variable sum must be updated for every input of ans instead of just adding array[x] once when the loop ends. In your else statement, ans should be assigned to array[x], not the other way around. There are a few more errors: - The for loop will run indefinitely due to the lack of a termination condition. Inside the loop, you increment x, but no condition is met for the loop to be exited. I see that you utilise boolean to try to quit the loop, but the boolean condition corresponds to the outer while loop. The for loop will run forever.
24th Jan 2017, 1:07 AM
Hatsy Rei
Hatsy Rei - avatar
+ 6
#include <iostream> using namespace std; int main() { int ans; int array[100]; int sum = 0; for (int i=0; ans != -10; i++) { cout << "Please input the numbers to be added up." << endl; cout << "You may input -10 to end input." << endl; cin >> ans; if(ans != -10) { array[i] = ans; sum += array[i]; } } cout << "The sum of the input numbers is " << sum << endl; return 0; }
23rd Jan 2017, 5:40 PM
Hatsy Rei
Hatsy Rei - avatar
+ 1
if you comfortable with standard libraries then is the code for you /* * Author : Keshave jat * Email : jatkeshave@gmail.com */ #include <iostream> #include <vector> using namespace std; int main(int argc, char *argv[]) { char more = 'y'; int count = 0, value = 0; vector<int> numbers; do{ system("cls"); count++; cout << "Please enter " << count << " Value" << endl; cin >> value; numbers.push_back(value); cout << "add more? Y/n" << endl; cin.clear(); cin.ignore(10000,'\n'); cin.get(more); }while(more == 'y' || more == 'Y'); // clearing console window system("cls"); //printing result cout << "-------------------------------------------------" << endl; cout << "------------------------Result-------------------" << endl; cout << "-------------------------------------------------" << endl << endl; value = 0; for(int i = 0; i < numbers.size(); i++){ value += numbers[i]; cout << numbers[i] << endl; } cout << "------------" << endl; cout << value << endl; cout << "------------" << endl; return 0; }
23rd Jan 2017, 5:59 PM
Keshave Jat
Keshave Jat - avatar
+ 1
@Hatsy Rei Why does your code work? Is my code redeemable? It definitely works, but if you could tell me why mine doesn't, I will be very very grateful. Thanks though! @Keshave Jat Thanks for your contribution! :) However, I don't think I'm on the level to understand your code yet. I will definitely save it for when I do, though.
23rd Jan 2017, 7:49 PM
neon_cabbage
neon_cabbage - avatar
+ 1
Thank you so much! I tried to ask my question in the quality expected on Stack Overflow, which includes doing everything you can on your own before asking, as well as providing relevant code. It should probably be a more common way to ask, but oh well ;)
24th Jan 2017, 1:16 AM
neon_cabbage
neon_cabbage - avatar
+ 1
@neon_cabbage as you said brother that you are unable to understand the code yet. so, if you know about memory allocation and have knowledge of malloc and relloc I can post new answer for you with memory managment implementation. thank you
24th Jan 2017, 7:43 AM
Keshave Jat
Keshave Jat - avatar
0
@Unfortunately I don't know about that, either. But if you want to post more code, you can; I'll just have to come back to it later.
24th Jan 2017, 11:16 AM
neon_cabbage
neon_cabbage - avatar