For count help.

I'm not sure how to use the for command in this instance. The pseudo code says "for count = 1-12", but I'm not sure how to translate that into syntax. I would ask my professor, but it's way too late to call. Please help, I don't even care if you mock me and call me names. I CAN TAKE IT.

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
29
30
31
32
33
  #include <iostream>
#include <string>
using namespace std;
int main() 
{
    //Variable Declarations
    int acctNum = 0;
    string firstName = "";
    string lastName = "";
    int purchasePrice = 0;
    int payment = 0;
    
    //Housekeeping
    cout << "Please enter account number: ";
    cin >> acctNum;
    cout << "Please enter customer's first name: ";
    cin >> firstName;
    cout << "Please enter customer's last name: ";
    cin >> lastName;
    cout << "Please enter purchase price: ";
    cin >> purchasePrice;
    
    //detail loop
    payment = purchasePrice / 12;
    cout << "Customer First Name: " << firstName;
    cout << "Customer Last Name: " << lastName;
    cout << "Account Number: " << acctNum;
    
    for(count = 0; count < payment; count++)
    {
              cout << "Payment Number " << count << ": $" << payment << end1;
    }        
}
1
2
3
4
5
for (int i =1; i <=12; i++)
{
     //do whatever
}
The normal idiom for doing something a set number of times is:

 
for (int a = 0; a < 12; ++a) {}


But magic numbers are not a good idea, so use a const variable instead:

1
2
3
4
5
const int MaxNumPayments = 12;

for (int a = 0; a < MaxNumPayments; ++a){
     // do whatever
}


Reason for starting at zero is because arrays start at zero, getting used to doing it this way won't cause you any errors when you come to using arrays. Subscripting for other things start at zero also.

Whenever I see this: for (int i =1; i <=12; i++), I am immediately suspicious.

@LivinLikeLarry

Your logic isn't quite right with your use of the payment variable in your loop.

Also, were you meant to use int for the money values? double would be be better.

Hope all goes well.

Topic archived. No new replies allowed.