c++ - Illegal else without matching if -
i'm getting error message says illegal else without matching if. think wrong else statement, don't see where. have unneeded bracket? it's on line 78 column 2. thank you
#include "stdafx.h" #include <iostream> #include <iomanip> #include <string> using namespace std; int main() { string service_code, //hold service code account_number; //hold account number double final_amount = 0, // hold final amount final_damount, //hold amount day minutes minutes = 0, //hold amount minutes day_minutes = 0, // hold amount fay minutes night_minutes = 0, //hold amount night minutes final_namount = 0; //hold amount night minutes cout << "please enter account number: "; cin >> account_number; //entering account number cout << "please enter service code (r or r regular service , p or p premium service): "; cin >> service_code; //enteringthe service code cout << "please enter amount of minutes used: "; cin >> minutes; //entering minutes used if(service_code == "r" || "r") { if(minutes <= 50) final_amount = 10; cout << "your final amount $: " << final_amount << endl; //displaying final amount when minutes less or equal 50 } { if(minutes > 50) final_amount = (minutes - 50) * 0.20 + 10; cout << "your final amount is: $ " << final_amount << endl; //displaying final amount when minutes greater 50 } { else if(service_code == "p" || "p") cout << "please enter amount of minutes used during day: " << day_minutes << endl; //entering minutes used during day cout << "please enter amount of minutes used during night: " << night_minutes << endl; //entering minutes used during night } { if(day_minutes <=75) final_damount = 0; final_amount = final_damount + final_namount + 20; //calculating final amount minutes used during day } { if(day_minutes > 75) final_damount = day_minutes * 0.10; final_amount = final_damount + final_namount + 20; //calcuating final amount minutes used during day } { if(night_minutes <= 100) final_namount = 0; final_amount = final_damount + final_namount + 20; //calcuating final amount minutes used during night } { if(night_minutes > 100) final_namount = night_minutes * 0.05; final_amount = final_damount + final_namount + 20; //calcuating final amount minutes used during night cout << "your final amount is: $ " << final_amount << endl; //displaying final amount } { else cout << "error, program not accept negative numbers.\n"; //displaying error message cout << "account number: " << account_number << endl; //displaying account number cout << "service code: " << service_code << endl; //displaying service code cout << "service code: " << minutes << endl; //displaying minutes } return 0; }
most of if
statements missing opening curly-braces. means wrong code being executed program because first statement after each if
statement executed if conditional expression true , rest of code executed.
...this same issue apple's infamous goto error
bug: http://nakedsecurity.sophos.com/2014/02/24/anatomy-of-a-goto-fail-apples-ssl-bug-explained-plus-an-unofficial-patch/
c not python, indentation not respected compiler , not mean code belongs keyword or block.
i suggest reading on c's syntax, , it's (generally) idea use curly-braces statement blocks (if
, for
, do
, while
, etc) avoid nasties these. internally, team @ microsoft, runs program called stylecop won't let check-in code our central repository if contains brace-less statements.
Comments
Post a Comment