Adding numbers and storing them in a variable.

Hey everyone, I'm new to the forums and C++. I'm trying to make a program that will:

A) Add an unspecified amount of bills for each month and store them in a variable.

Hence, there could be 1 or 100 entries - I want them to be saved in the variables for each month. To move on to entering bills for the next month, the user just has to enter 0 and it will move on to the next month.

Starting with March and ending with August.

I'm trying to understand how to add totals and save them to a variable, but I am unable to figure out the code. Could someone please look at my code and tell me what I'm doing wrong? This code feels very off (as it is) and I know I'm moving in the right direction - I just need some help! Thanks in advance guys..

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{

cout << "Welcome to the NetBound Online Monthly Sales Total Application";
cout << "This programs adds monthly sales totals and then outputs them"
"to a file";
cout << " ";

cout << "To exit addition of sales numbers to each month, simply enter 0";

int nMarch;
int nApril;
int nMay;
int nJune;
int nJuly;
int nAugust;

int nMarchTotal;



cout << "Please enter totals for the month of March: ";
cin >> nMarch;

while ( nMarch > 0)
{

cout << "Please enter totals for the month of March: ";
cin >> nMarch;

}

// Displays the totals for the month of march

cout << "The totals for the month of March are:" << nMarchTotal;

system("PAUSE");
return EXIT_SUCCESS;
}

I didn't exactly understand your problem. Do you want to have a list of numbers for each month?
This is how to do that:
1
2
3
4
5
6
7
8
9
10
11
string month[6] = {"march", "april", "may", "june", "july", "august"};
vector<int> value[6];
for(int m = 0; m < 6; m++){
    cout << month[m] << " : ";
    int val;
    while(true){
        cin >> val;
        if(val == 0) break;
        else value[m].push_back(val);
    }
}
Thank you so much for the response, I really do appreciate it :)

I wanted to enter a bunch of invoices for each month, and then have the total stored in a variable (e.g. "March").

..and then have the totals for each month stored to a file (I already have the code for that figure out).

I have no idea if your code is what I'm looking for, but I'm guessing it is on the right track. I know mine was way off lol, but you have to learn somehow I suppose. I kinda like to push myself into more advanced territories even when I may not be ready lol.

If you want to simply add input to appropriate variable, change vector<int> to int and value[m].push_back(val); to value[m] += val;
Thanks for the help. How do I print out the results of each month to the screen? I can't seem to figure that out just yet :)

I know how to use "cout", but in this situation I'm a little confused as to how the syntax would work.
If you want to print the values entered for each month, you'll have to use vectors. Inputting part is done in my first post. The other parts are to add up all values and to print them. Do you know how to use vectors?
No, not at this point.

I'm on day 4 of "learn c++ in 21 days" :)

I've just finished learning using the "if, else" statments. I can understand most of the code you've showed me so far, but I am not familiar with vectors at all.
oh, wait. ignore my previous post. I keep forgetting what the original problem was. sorry.

ints will do fine here.
as you have this code,
1
2
3
4
5
6
7
8
9
10
11
string month[6] = {"march", "april", "may", "june", "july", "august"};
int value[6] = {0};
for(int m = 0; m < 6; m++){
    cout << month[m] << " : ";
    int val;
    while(true){
        cin >> val;
        if(val == 0) break;
        else value[m] += val;
    }
}

all values are in array. To print them do this:
1
2
3
for(int m = 0; m < 6; m++){
    cout << month[m] << ": " << value[m] << '\n';
}
Or a slightly simpler version of hamsterman's code:

1
2
3
4
5
6
7
8
9
10
string month[6] = {"march", "april", "may", "june", "july", "august"};
int value[6] = {0};
for(int m = 0; m < 6; m++){
    cout << month[m] << " : ";
    int val=1;
    while(val){
        cin >> val;
        value[m] += val;
    }
}


Since you initialize val as 1, it'll go into the while-loop and be immediately replaced by the actual value it's meant to have. From then on, while(val), i.e. while val is true (different from 0), it will keep looping.
I compiled all the code together, and it doesnt work. It prints out different numbers all the time, e.g. 197556478 or 0. Somethings def. not right..
Last edited on
Post the code using [ code ] [ /code ] tags.
maybe you forgot to add = {0} to your array declaration?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    
    string month[6] = {"March", "April", "May", "June", "July", "August"};
int value[6];
for(int m = 0; m < 6; m++){
    cout << "Please enter totals for the month of: " << month[m] << " : ";
    int val;
    while(true){
        cin >> val;
        if(val == 0) break;
        else value[m] += val;
    }
    
for(int m = 0; m < 6; m++){
cout << month[m] << ": " << value[m] << '\n';
}
}
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
line 10 should be int value[6] = {0}; this initializes every element of the array to 0.
thank you so much - it works! :)
Topic archived. No new replies allowed.