I'm still new at C++, I am creating a budget project.
I am struggling to get the SUM of all values of an array, the array is created by user input. the problem is when i run it, it doesn't give the right answer. I am trying to do it step by step so I've only tested it by typing "no" when asked if i wish to continue. I made notes so that there's less confusion, I've been stuck on this and could use the help. also if you have any other tips and tricks to make this cleaner feel free to let me know, thank you!
usersMonthlyExpenses[] is the array
and numsize is the size
#include <iostream>
using namespace std;
#include <locale>
#include <string>
//This function gets the users monthly expenses information
void getInfo(int usersMonthlyExpenses[], int numsize){
//initialize
cout<<"please input your expenses. Type done when finished."<<endl;
string done = "";
int sum = 0;
// this while statement loops the users input until a certain amount
for(int i = 0; i <= numsize; i++){
cin>>usersMonthlyExpenses[i];
cin>>done;
sum = sum + usersMonthlyExpenses[i];
i++;
//this if statment is for when the user has reached that certain number of input it ask if they wish to continue
if (i >= numsize){
//initialize
string yes = "yes";
string no = "no";
string choice;
cout <<"would you like to add more? please type yes or no: ";
cin >> choice;
if(choice == yes){
getInfo(usersMonthlyExpenses, numsize);
}else if(choice == no){
// this is where the problem is
for(int i = 0; i < numsize; i++){
sum+=usersMonthlyExpenses[i];
}
cout<<sum
break;
} else{
cout<< "please type yes or no: ";
cin>>choice;
}
}
// if user types done then add up their expenses
if(done == "done"){
cout<< "This is your total monthly expenses: "<< sum;
break;
}
}
}
int main(){
//declaring functions and variables
// int usersMonthlyIncome;
// int budgetSelection;
// int x;
// int fiftyRule(int usersMonthlyIncome);
// some variables to get this function initialized
int numsize = 5;
int costNameSize = 5;
int usersMonthlyExpenses[numsize] = {};
int usersCostName[costNameSize];
getInfo(usersMonthlyExpenses, numsize);
return 0;
}
i've tried creating a function for it and just passing the arrays in the parameters but that didn't work, I've rewritten it a few different ways to see what works but no matter what I did it never gave the right answer.