"___ was not declared in this scope" errors

I am receiving compiler errors as follows and do not know how to fix this. I know it's the same type of error, I just don't exactly know how to fix this error. Thanks in advanced!

main.cpp: In function ‘int main()’:
main.cpp:26:18: error: ‘count’ was not declared in this scope
for (count = 1; count <= 12; count ++);
^
main.cpp:28:13: error: ‘count’ was not declared in this scope
count = count + 1;
^
main.cpp:29:13: error: ‘output’ was not declared in this scope
output count;
^
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
#include <iostream>
#include <cstdlib>
#include <string>
    using namespace std;
    int main()
    {
            int acctNum;
            string firstName;
            string lastName;
            int purchasePrice;
            int payment;
           
            
            cout << "Please enter account number";
            cin >> acctNum;
            cout << "Please enter customer's first name";
            cin >> firstName;
            cout << "Please enter purchase price";
            cin >> purchasePrice;
            
            payment = purchasePrice / 12;
            cout << "Customer first name:" << firstName <<endl;
            cout << "Customer last name:" << lastName <<endl;
            cout << "Account number:" << acctNum <<endl;
            
            for (count = 1; count <= 12; count ++);
            cout << "Payment number";
            count = count + 1;
            output count;
    }
Last edited on
You're getting that error because you hadn't initialized the 'count' variable. Write
int count; after int payment;

Also replace output count with cout<<count;
cout is used for console output. There's nothing called output in STL of C++


Also:
can you explain what you're trying to do with this block:
1
2
3
4
            for (count = 1; count <= 12; count ++);
            cout << "Payment number";
            count = count + 1;
            output count;
Last edited on
Hello bkatic,

In addition to what Nwb has said you should give this a read http://www.cplusplus.com/doc/tutorial/control/#for

After yo will notice that for loops are generally written as:
1
2
3
4
for (int count = 1; count <= 12; count++)
{
    // <--- your code here.
}

The "int" allows you to define the variable count for use as a local variable in the for loop.

To what you think is in the for loop. Actually nothing because of the semicolon at the end of line 27 that should not be there. What it does is limit the for loop to just that line.

This is what your code looks line in a different way:
1
2
3
4
5
6
7
for (count = 1; count <= 12; count++);

cout << "Payment number";
	
count = count + 1;
	
output count;


I think this is what you intended:
1
2
3
4
5
6
7
8
for (int count = 1; count <= 12; count++)
{
	cout << "Payment number";

	count = count + 1;

	output count;
}


What is the point of line 5 in the above code? Between this and the for loop you are adding 2 to count on each iteration of the loop. Adding to count is what the third part of the for condition is for.

Hope that helps,

Andy
Wow, thank you for the responses! I finally got it to work. As to what I'm trying to do with the

1
2
3
4
for (count = 1; count <=12; count ++)
cout << "Payment number";
count = count + 1;
cout << count; 


is from the pseudocode my homework assignment gave me. The pseudocode is as follows:

for count 1 to 12
output "Payment Number", count, ": $" , payment.
endfor

For context the assignment is designing an application that accept's a client's loan amount and monthly payment amount and outputs the loan balance each month until the loan is paid off.

But after further inspection and your feedback I understand why it doesn't make sense and I have to switch things around. Thank you again!
So you're supposed to display
"Payment Number" count ": $" payment
for 12 purchases

( In your for loop you need to add braces like Andy said, but also you don't need count = count + 1 because count++ in the for parameter does that job for you. You must have been confused with while )

But where will you get the 'payment' from? Something is missing because the program accepts only 1 purchase but is asked to display 12 purchases??

Also I didn't get why "payment = purchasePrice / 12;" is being done.
And I suspect that purchasePrice is supposed to be an array. Also lastName was never assigned, you can't call it when it's not assigned.

Topic archived. No new replies allowed.