C++ Errors

Status
Not open for further replies.

toxicity_27

The Angriest of Amoebas
Messages
1,255
Location
Minnesota
Well I'm getting some errors when I'm trying to compile this C++ program. It's really starting to make me mad as the code should be correct. Here is a list of the problems I am getting thanks for the help.

prog12.cpp: In function `int main()':
prog12.cpp:72: error: invalid conversion from `const char*' to `char'
prog12.cpp:89: error: expected primary-expression before "else"
prog12.cpp:89: error: expected `;' before "else"
prog12.cpp:92: error: expected primary-expression before "else"
prog12.cpp:92: error: expected `;' before "else"
prog12.cpp:100: error: ISO C++ forbids comparison between pointer and integer
prog12.cpp:126: error: invalid conversion from `const char*' to `char'
prog12.cpp:160: error: expected `}' at end of input
 
post your code. You have some very simple formatting errors. I'm guessing you missed a semicolon somewhere and it's throwing off the rest of your code.
 
#include <iostream>

using namespace std;

//Program Description
//This program will output charges for customers. It will also output
//the total amounts used and total revenue recieved for the amounts.

int main()
{

//Constants
const double water2 = 0.40;
const double water3 = 0.32;
const double electric1 = 0.05;
const double electric2 = 0.03;
const double electric3 = 0.04;
const double electric4 = 0.06;
const double gas1 = 1.50;
const double gas2 = 1.40;
const double gas3 = 1.55;
const double water_minimum = 3.00;
const double electric_minimum = 7.00;
const double gas_minimum = 6.00;

//Variables

int cust_number;
char code;
int water;
int electric;
int gas;
double water_charge;
double electric_charge;
double gas_charge;
double total_bill;
int total_water;
int total_electric;
int total_gas;
double total_water_revenue;
double total_electric_revenue;
double total_gas_revenue;
double total_revenue;
int num_customers;
//Begin Program
//Initialize Totals

num_customers = 0;
total_water = 0;
total_electric = 0;
total_gas = 0;
total_water_revenue = 0;
total_electric_revenue =0;
total_gas_revenue = 0;
//Loop to read, process and report each customer

cout << "Enter Customer Number. 0 ends the input." << endl;
cin >> cust_number;
while (cust_number !=0)
{
cout << "Enter customer code, water used, and electric used.";
cout << endl;
cin >> code >> water >> electric;
if (code = "G")
{
cout << "Enter gas." << endl;
cin >> gas;
//Calculate water charge

if (water <= 10000)
water_charge = water/100*water1;
else if (water <= 100000)
water_charge=100*water1+(water-10000)/100*water2;
else
water_charge=100*water1+900*water2+(water-100000)/100*water3;
//Calculate elctricity charge

if (electric <= 500)
{
electric_charge = electric * electric1;
else

if (electric <= 1500)
electric_charge = 500*electric1+(electric-500)*electric2;
else
electric_charge=500*electric1+1000*electric2+2000*electric3
+(electric-3500)*electric4;
}
if (electric_charge < electric_minimum)
electric_charge = electric_minimum;
//Calculate gas charge
if (code == "G")
{
if (gas <= 50000)
gas_charge = gas/1000*gas1;
else
if (gas <= 100000)
gas_charge = 50*gas1+(gas-50000)/1000*gas2;
else
gas_charge = 50*gas1+50*gas2+(gas-100000)/1000*gas3;
if (gas_charge < gas_minimum)
gas_charge = gas_minimum;
else //code = "R"
gas = 0;
gas_charge = 0;
}
//Calculate total_bill

total_bill = water_charge + electric_charge + gas_charge;
//Report customer bill

cout << "CUSTOMER BILL" << endl;
cout << "Customer Number: " << cust_number << endl;
cout << "Water Used: " << water << endl;
cout << "Water Charge: " << water_charge << endl;
cout << "Electricity Used: " << electric << endl;
cout << "Electricity Charge: " << electric_charge << endl;
if (code = "G")
{
cout << "Gas Used: " << gas << endl;
cout << "Gas Charge: " << gas_charge << endl;
}
cout << "Total Bill: " << total_bill << endl;
//Update summary data

num_customers++;
total_water = total_water + water;
total_electric = total_electric + electric;
total_gas = total_gas + gas;
total_water_revenue = total_water_revenue + electric_charge;
total_gas_revenue = total_gas_revenue + gas_charge;
//Read the next customer number

cout << "Enter next customer number " << endl;
cin >> cust_number;
} //End of looping for each individual customer
//Report summary data for all customers

total_revenue = total_water_revenue + total_electric_revenue +
total_gas_revenue;
cout << "SUMMARY OF CUSTOMERS" << endl;
cout << "Number of Customers: " << num_customers << endl;
cout << "Total Water Used: " << total_water << endl;
cout << "Total Water Revenue: " << total_water_revenue << endl;
cout << "Total Electricity Used: " << total_electric << endl;
cout << "Total Electricity Revenue: " << total_electric_revenue << endl;
cout << "Total Gas Used: " << total_gas << endl;
cout << "Total Gas Revenue: " << total_gas_revenue << endl;
cout << "Total Revenue: " << total_revenue << endl;

return 0;
}
 
You've got quite a few formatting errors in there.

Go back through, thinking of what conditinals you actually want, and then analyze your parenthesis.

Block everything in your IF statements with brackets {}. Even if they're only one liners. I think you're missing quite a few closing brackets and some of your conditional logic is off.
 
First off, replace all instances of:

Code:
if (code = "G")
with:
Code:
if (code == 'G')

On line 71, you make a reference to 'water1' which has not been previously declared (it doesn't exist yet).

And the end of the code block for the if statement starting at line 79 is missing it's closing brace.
 
#include <iostream>

using namespace std;

//Program Description
//This program will output charges for customers. It will also output
//the total amounts used and total revenue recieved for the amounts.

int main()
{

//Constants
const double water1 = 0.60;
const double water2 = 0.40;
const double water3 = 0.32;
const double electric1 = 0.05;
const double electric2 = 0.03;
const double electric3 = 0.04;
const double electric4 = 0.06;
const double gas1 = 1.50;
const double gas2 = 1.40;
const double gas3 = 1.55;
const double water_minimum = 3.00;
const double electric_minimum = 7.00;
const double gas_minimum = 6.00;

//Variables

int cust_number;
char code;
int water;
int electric;
int gas;
double water_charge;
double electric_charge;
double gas_charge;
double total_bill;
int total_water;
int total_electric;
int total_gas;
double total_water_revenue;
double total_electric_revenue;
double total_gas_revenue;
double total_revenue;
int num_customers;
//Begin Program
//Initialize Totals

num_customers = 0;
total_water = 0;
total_electric = 0;
total_gas = 0;
total_water_revenue = 0;
total_electric_revenue =0;
total_gas_revenue = 0;
//Loop to read, process and report each customer

cout << "Enter Customer Number. 0 ends the input." << endl;
cin >> cust_number;
while (cust_number !=0)
{
cout << "Enter customer code, water used, and electric used.";
cout << endl;
cin >> code >> water >> electric;
if (code = "G")
{
cout << "Enter gas." << endl;
cin >> gas;
}
//Calculate water charge

if (water <= 10000)
water_charge = water/100*water1;
else if (water <= 100000)
water_charge=100*water1+(water-10000)/100*water2;
else
water_charge=100*water1+900*water2+(water-100000)/100*water3;
//Calculate electricity charge

if (electric <= 500)
electric_charge = electric * electric1;
else
if (electric <= 1500)
electric_charge = 500*electric1+(electric-500)*electric2;
else
electric_charge=500*electric1+1000*electric2+2000*
electric3
+(electric-3500)*electric4;
if (electric_charge < electric_minimum)
electric_charge = electric_minimum;
//Calculate gas charge
if (code == "G")
{
if (gas <= 50000)
gas_charge = gas/1000*gas1;
else
if (gas <= 100000)
gas_charge = 50*gas1+(gas-50000)/1000*gas2;
else
gas_charge = 50*gas1+50*gas2+(gas-100000)/1000*gas3;
if (gas_charge < gas_minimum)
gas_charge = gas_minimum;
else //code = "R"
gas = 0;
gas_charge = 0;
}
//Calculate total_bill

total_bill = water_charge + electric_charge + gas_charge;
//Report customer bill

cout << "CUSTOMER BILL" << endl;
cout << "Customer Number: " << cust_number << endl;
cout << "Water Used: " << water << endl;
cout << "Water Charge: " << water_charge << endl;
cout << "Electricity Used: " << electric << endl;
cout << "Electricity Charge: " << electric_charge << endl;
if (code == "G")
{
cout << "Gas Used: " << gas << endl;
cout << "Gas Charge: " << gas_charge << endl;
}
cout << "Total Bill: " << total_bill << endl;
//Update summary data

num_customers++;
total_water = total_water + water;
total_electric = total_electric + electric;
total_gas = total_gas + gas;
total_water_revenue = total_water_revenue + electric_charge;
total_gas_revenue = total_gas_revenue + gas_charge;
//Read the next customer number

cout << "Enter next customer number " << endl;
cin >> cust_number;
} //End of looping for each individual customer
//Report summary data for all customers

total_revenue = total_water_revenue + total_electric_revenue +
total_gas_revenue;
cout << "SUMMARY OF CUSTOMERS" << endl;
cout << "Number of Customers: " << num_customers << endl;
cout << "Total Water Used: " << total_water << endl;
cout << "Total Water Revenue: " << total_water_revenue << endl;
cout << "Total Electricity Used: " << total_electric << endl;
cout << "Total Electricity Revenue: " << total_electric_revenue << endl;
cout << "Total Gas Used: " << total_gas << endl;
cout << "Total Gas Revenue: " << total_gas_revenue << endl;
cout << "Total Revenue: " << total_revenue << endl;

return 0;
}

Okay, so I fixed those errors, but now I get these errors*.

prog12.cpp: In function `int main()':
prog12.cpp:72: error: ISO C++ forbids comparison between pointer and integer
prog12.cpp:99: error: ISO C++ forbids comparison between pointer and integer
prog12.cpp:125: error: invalid conversion from `const char*' to `char'


*Note: Line 158 is the very last line.
 
Hmm, it seems you lack much attention to detail.

I said replace all instances of:

Code:
if (code = "G")
with:
Code:
if (code == 'G')

Note the use of single quotes, not double.
 
Okay, I fixed that, but now I get this error.

prog12.cpp:125: error: invalid conversion from `const char*' to `char'
 
Status
Not open for further replies.
Back
Top Bottom