Search code examples
initializer

C++ error "expected initializer before <variablename>"


Good afternoon programmers,

I am very new to C++. I use eclipse and I have an assignment that asks me to calculate a future population when given constant birthrates, deathrates, immigrantrates, and the current population.

I get two errors. 1) On the line "float SECONDSINAYEAR;" I get the error "expected initializer before BIRTHRATEPERSEC" 2) On the line "CURRENTPOPULATION = 318933342;" I get the error "'futurepopulation' was not declared in this scope"

#include <iostream>
using namespace std;

float main(void) {

//declaration of variables
float BIRTHRATEPERSEC;
float DEATHRATEPERSEC;
float IMMIGRANTRATEPERSEC;
float CURRENTPOPULATION;
float SECONDSINAYEAR;
float futurepopulation;

// Initialize constants
BIRTHRATEPERSEC = .5;
DEATHRATEPERSEC = -.1428571429;
IMMIGRANTRATEPERSEC = .04167;
CURRENTPOPULATION = 318933342;
SECONDSINAYEAR = 31536000;

// DO calculations
float futurepopulation = (CURRENTPOPULATION + (SECONDSINAYEAR * (BIRTHRATEPERSEC + DEATHRATEPERSEC + IMMIGRANTRATEPERSEC)));

//print output

cout << "The future population is " + futurepopulation <<endl;
return 0;

}

what should I do? Thanks!


Solution

  • First issue is that your main should return an int, not a float.

    You also declare float futurepopulation twice, once with your other variables and once with the summation.

    Last problem is that in C++ you cannot add a number to a string that way, the correct syntax would be cout << "The future population is " << futurepopulation <<endl;