0

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.

  • 2
    These declarations int usersMonthlyExpenses[numsize] = {}; int usersCostName[costNameSize]; declare variable length arrays. Variable length arrays are not a standard C++ feature. – Vlad from Moscow May 04 '23 at 22:10
  • 3
    Think about how `for(int i = 0; i <= numsize; i++)` works and consider if you want another `i++;` inside the loop or not. (Hint: You do not) Also, by using `<=` you will access the array out of bounds. Valid indices are from 0 to size - 1. Since you've already been adding values to `sum` you don't need another loop to do it at the end. – Retired Ninja May 04 '23 at 22:13
  • PSA: Try and steer towards using `std::vector` instead of C-style pointer/size pairs. These arrays are super easy to iterate with `for (auto&& x : y)`. – tadman May 04 '23 at 22:27
  • 2
    The array is not required at all in this program as currently written. It is pointless, and only increases the risk of programming errors. All you need to do is read each single value into a local variable and add that to the running total. End of story. The recursive call is weird too. Why would you ask the user to enter additional data and then just overwrite the old data? If you _did_ want to use that data later, it's now gone. – paddy May 04 '23 at 22:31
  • Prefer `std::vector` to arrays. When inputting the data, you can use `std::vector<>::push_back()` method to append the datum to the vector. – Thomas Matthews May 04 '23 at 22:31
  • A side note: [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – wohlstad May 05 '23 at 03:45

0 Answers0