int i not in scope

Sounds like a very stupid question below is my class definition and my compiler keeps saying the following error.

..\src\SalesPerson.cpp:45:17: error: 'i' was not declared in this scope
for(int i = 0; i < monthsPerYear; i++)
^

1
2
3
4
5
  SalesPerson::SalesPerson(){
	for ( int i = 0; i < monthsPerYear; i++ ){
		sales[ i ] = 0.0;
	}
}


How is int 'i' not in scope it was initialized in the loop!
it should not happen , but have you tried to remove int i =0 , an i might already exist in global scope ..
The problem may be with line 3, I don't think your showing us enough code to determine the problem.

Normally the function would start with
int SalesPerson()
or
void SalesPerson()

I'm not sure what your line 1 does.

@SamuelAdams it looks like the constructor so there is no return type .
@cluster3 have you tried to remove int i = 0 ?
This is a recitation document I was working on out of my text book here is the code. Keep in mind this is actually in 3 files.


main file

#include <iostream>
#include "SalesPerson.h"
using namespace std;

int main() {
	SalesPerson s;

	s.getSalesFromUser();
	s.printAnnualSales();

	return 0;
}



header file


/*
 * SalesPerson.h
 *
 *  Created on: Sep 8, 2015
 *      Author: blake
 */

#ifndef SALESPERSON_H_
#define SALESPERSON_H_

class SalesPerson {
public:
	static const int monthsPerYear = 12;
	SalesPerson();
	void getSalesFromUser();
	void setSales( int, double);
	void printAnnualSales();
private:
	double totalAnnualSales();
	double sales[monthsPerYear];
};

#endif /* SALESPERSON_H_ */



.cpp file (definition)

/*
 * SalesPerson.cpp
 *
 *  Created on: Sep 8, 2015
 *      Author: blake
 */

#include <iomanip>
#include <iostream>
#include "SalesPerson.h"
using namespace std;

SalesPerson::SalesPerson(){
	for ( int i = 0; i < monthsPerYear; i++ ){
		sales[ i ] = 0.0;
	}
}

void SalesPerson::getSalesFromUser(){
	double salesFigure;

	for (int i = 1; i<= monthsPerYear; i++){
		cout << "Enter sales amount for month " << i << ": ";
		cin >> salesFigure;
		setSales(i, salesFigure);
	}
}

void SalesPerson::setSales(int month, double amount){
	if (month >= 1 && month <= monthsPerYear && amount > 0)
		sales [month - 1] = amount;
	else
		cout << "Invalid month or sales figure" << endl;
}

void SalesPerson::printAnnualSales(){
	cout << setprecision(2) << fixed
		 << "\nThe total annual sales are: $"
		 << totalAnnualSales() << endl;
}

double SalesPerson::totalAnnualSales(){
	double total = 0.0

	for(int i = 0; i < monthsPerYear; i++)
		total += sales[i];

	return total;
}
Compiles fine: http://coliru.stacked-crooked.com/a/e38b843a2f82085a

BTW: you are missing semicolon in totalAnnualSales
Weirdest error message for missing a semicolon. Thanks btw fixed I am not sure how I missed it I looked at it for like 30min and could not figure out how i was not in scope! Thanks again.
Thanks, I haven't used a constructor before. Something new to learn :P
Topic archived. No new replies allowed.