| | |
Must read file from while loop with nested switch and if/else, why won't it compile??
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Mar 2007
Posts: 5
Reputation:
Solved Threads: 0
Must read file from while loop with nested switch and if/else, why won't it compile??
0
#1 Mar 15th, 2007
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> using namespace std; const double PRICEA = 9.95; const double PRICEB = 14.95; const double PRICEC = 19.95; const int ONE = 10; const int TWO = 20; const double EXTRA1 = 2.00; const double EXTRA2 = 1.00; int main() { int hours; char pkgType; double cost; ifstream infile; infile.open("pa5.input"); if(infile.fail()) { cout<<"Error Opening File." <<endl; exit(1); } infile >> pkgType >> hours; while (infile) { switch (pkgType) { case 'A': if (hours > ONE ) cout << "Package A " << PRICEA << hours << cost << endl; cost = PRICEA + ((hours - ONE) * EXTRA1); break; else if (hours <= ONE) { cout << "Package A " << PRICEA << hours << cost << endl; cost = PRICEA; } break; case 'B': if (hours > TWO ) { cout << "Package B "<< PRICEB << hours << cost << endl; cost = PRICEB + ((hours - TWO) * EXTRA2); } break; else if { cout << "Package B "<< PRICEB << hours << cost << endl; cost = PRICEB; } break; case 'C': cout <<"Package C "<< PRICEC << hours << cost << endl; cost = PRICEC; break; default: cout << endl << "Invalid Package Type! " << endl; } } system("pause"); return 0; }
Last edited by ~s.o.s~; Mar 15th, 2007 at 11:03 pm. Reason: Added code tags. Learn to use them.
Re: Must read file from while loop with nested switch and if/else, why won't it compi
0
#2 Mar 15th, 2007
You seem to be leaving out braces on your if statements. It was really easy to find with the code auto-indented. Here's a code excerpt:
That happens in all the case statements. I think you want something like this:
which could be further reduced to this:
btw, for future posts, please put your code between [code] and [/code] tags to preserve formatting for the rest of us. Also, posting the error message is usually quite helpful
C++ Syntax (Toggle Plain Text)
switch (pkgType) { case 'A': if (hours > ONE ) cout << "Package A " << PRICEA << hours << cost << endl; cost = PRICEA + ((hours - ONE) * EXTRA1); // this will always get run break; // this will always get run // everything below this is unreachable code else if (hours <= ONE) { cout << "Package A " << PRICEA << hours << cost << endl; cost = PRICEA; } break;
C++ Syntax (Toggle Plain Text)
switch (pkgType) { case 'A': if (hours > ONE ) { cout << "Package A " << PRICEA << hours << cost << endl; cost = PRICEA + ((hours - ONE) * EXTRA1); } else if (hours <= ONE) { cout << "Package A " << PRICEA << hours << cost << endl; cost = PRICEA; } break;
C++ Syntax (Toggle Plain Text)
switch (pkgType) { case 'A': cout << "Package A " << PRICEA << hours << cost << endl; // this line was the same in both the if and else if (hours > ONE ) { cost = PRICEA + ((hours - ONE) * EXTRA1); } else if (hours <= ONE) { cost = PRICEA; } break;
Last edited by Infarction; Mar 15th, 2007 at 4:58 pm.
•
•
Join Date: Mar 2007
Posts: 5
Reputation:
Solved Threads: 0
Re: Must read file from while loop with nested switch and if/else, why won't it compile??
0
#3 Mar 20th, 2007
![]() |
Similar Threads
- how to read a file from client side without browsing (ASP.NET)
- C++ How can read from file.txt & where can save this file(file.txt) to start reading (C++)
- create, write & read file to/from folder (C++)
- Read/write to same file > once, Help (C++)
- Desperate and eagar to learn!!! (C++)
- Read in a file and store in char array (C)
- loop to create arrays when reading a file (Java)
Other Threads in the C++ Forum
- Previous Thread: C++
- Next Thread: help with c++ vectors
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library linker list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






