Getting a "expression must have a constant value" on my switch statement

#include<iostream>
#include <string>

using namespace std;

int main() {
//setting up numeral input
double loan_Payment;
double insurance;
double gas;
double oil;
double tires;
double maintenance;



//setting up text input
std::cout.width(30); std::cout << std::left << "Loan Payment";

std::cin.width(15); std::cin >> std::right >> loan_Payment ;

std::cout.width(30); std::cout << std::left << "Insurance";

std::cin.width(15); std::cin >> std::right >> insurance;

std::cout.width(30); std::cout << std::left << "Gas" ;

std::cin.width(15); std::cin >> std::right >> gas;

std::cout.width(30); std::cout << std::left << "Oil";

std::cin.width(15); std::cin >> std::right >> oil;

std::cout.width(30); std::cout << std::left << "Tires";

std::cin.width(15); std::cin >> std::right >> tires;

std::cout.width(30); std::cout << std::left << "Maintenance";

std::cin.width(15); std::cin >> std::right >> maintenance;


std::cout.width(30); std::cout << std::left << "Total";

std::cin.width(15); std::cout << std::right << loan_Payment + insurance + gas + oil + tires + maintenance << endl;

std::cout.width(30); std::cout << std::left <<"Yearly Total";

std::cin.width(15); std::cout << std::right << 12 * (loan_Payment + insurance + gas + oil + tires + maintenance) << endl;

std::cout.width(30); std::cout << std::left << " 10%";

std::cin.width(15); std::cout << std::right << .10 * (loan_Payment + insurance + gas + oil + tires + maintenance) << endl;
//If the yearly total is greater than 1000 dollars, add 10 percent of the yearly total to the yearly total.
double grand_Total;
switch(1) {
case grand_Total >= 1000: std::cout << std::right << ( 12 * (loan_Payment + insurance + gas + oil + tires + maintenance)) * (.10) + (12 * loan_Payment + insurance + gas + oil + tires + maintenance);
break;
}
The error is telling you that grand_Total >= 1000 isn't a constant expression. This isn't allowed.

A constant expression is any expression whose value can be computed by the compiler before your program runs. In practice, those are plain old literals (like 1000) or things marked const or constexpr. grand_Total isn't and can't be marked as a constant, because it's value must be modified at run-time.

You should change your program to use an if-statement (which you're mocking here in a convoluted way):
1
2
if (grand_Total >= 1000) { 
  std::cout << std::right << /* ... */ ;

Topic archived. No new replies allowed.