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;
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 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?
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.
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.