### HELP with program

I have NO clue what i am doing in C++ because i learned java. the program has to put out a file in the following format:

Community Hospital Billing Statement
Number of days spent in hospital: XX
Type of room: {Private, Semi-Private, Ward} << based on input
Room Charge: {room price x number of days) << rm * d
Telephone Charge: {tp * d}
Television Charge: {vp * d}
Total Due: {(rm*d)+(tp*d)+(vp*d)
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` ``````#include #include #include using namespace std; int main() {int d, rp, tp, vp, t, rm, ts, tv; ofstream bill; bill.open("bill.txt"); { cout << "This program is used to create a bill statement \n"; } { cout << "How many days did the patient stay? "; cin >> d; } { cout << "\nWhat type of room? \nP for private, S for semi-private, W for ward: "; cin >> rm; if ( rm == 'P' || rm == 'p' ) rp = 200; if ( rm == 'S' || rm == 's' ) rp = 150; else (rp = 100); } { int ts; cout << "\nWas there telephone service? Y for yes, N for No: "; cin >> ts; if( ts == 'Y' || ts == 'y' ) tp = 1.75; else(tp = 0); } { cout << "\nWas there television service? Y for yes, N for No: "; cin >> tv; if( tv == 'Y' || tv == 'y' ) vp = 1.75; else(vp = 0); } { t = (vp*d)+(tp*d)+(rp*vp); cout << "\nThe total bill is: " << t; } }``````

can someone help me?
 ``123`` ``````if ( rm == 'P' || rm == 'p' ) rp = 200; if ( rm == 'S' || rm == 's' ) rp = 150; else (rp = 100);``````

The logic of this is wrong. Let's examine what happens if someone enters P.

`if ( rm == 'P' || rm == 'p' ) rp = 200;` rm is P, so rp is set to 200.

`if ( rm == 'S' || rm == 's' ) rp = 150;` rm is NOT S and it is NOT s, so...
`else (rp = 100);` rp is set to 100.

So entering P results in rp being set to 100, but it should be 200.
Last edited on
so the last line of that would work as just
`if ( rm == 'W' || rm == 'w' ) rp = 100;`

Then the other thing is, it builds and runs no error , but after the second input of one of the 3 letters, it just stops working right. It will spit out the text of the next 3 cout lines without input and does not spit out a total.

And i also do not know how to make it generate the text file.
Updated code with Bill.txt generation in it. It builds but gives the error:
Invalid conversion from 'const char*' to 'int' on lines 28-30

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859`` ``````#include #include #include using namespace std; int main() {int d, rp, tp, vp, t, rm, ts, r, tv, rc, tc, vc; ofstream bill; bill.open("bill.txt"); { cout << "This program is used to create a bill statement \n"; } bill << "Community Hospital Patient Billing Statement\n"; { cout << "How many days did the patient stay? "; cin >> d; } bill << "Number of days stayed: " << d << "\n"; { cout << "\nWhat type of room? \nP for private, S for semi-private, W for ward: "; cin >> rm; if ( rm == 'P' || rm == 'p' ) rp = 200; if ( rm == 'S' || rm == 's' ) rp = 150; if ( rm == 'W' || rm == 'w' ) rp = 100; } { bill << "Type of room: "; if ( rm == 'P' || rm == 'p' ) r = "Private"; if ( rm == 'S' || rm == 's' ) r = "Semi-Private"; if ( rm == 'W' || rm == 'w' ) r = "Ward"; bill << r << "\n"; } rc = rp * d; bill << "Room Charge: " << rc << "\n"; { cout << "\nWas there telephone service? Y for yes, N for No: "; cin >> ts; if( ts == 'Y' || ts == 'y' ) tp = 1.75; else(tp = 0); } tc = tp * d; bill << "Telephone charge: " << tc << "\n"; { cout << "\nWas there television service? Y for yes, N for No: "; cin >> tv; if( tv == 'Y' || tv == 'y' ) vp = 1.75; else(vp = 0); } bill << "Television Charge: " << vc << "\n"; { t = vc + tc + rc; //tv prive times days + telephone times days + room cost times days cout << "\nThe total bill is: " << t; } bill << "Total Due: " << t << "/n"; bill.close(); }``````
Last edited on
Change rm to type char? Not really sure why you have a dozen int variables when it looks like most of them are storing characters.
 ``12`` ``````{int d, rp, tp, vp, t, rc, tc, vc; char rm, r, ts, tv;``````

rest of the code remained the same

the same error comes up but slightly different:

Invalid conversion from 'const char*' to 'char' on lines 28-30
`if ( rm == 'P' || rm == 'p' ) r = "Private";`

If r is char, it can't hold "Private", which in this case is the const char*
Why all the braces?

If you make use of the toupper function, you will not need to test your chars twice.

HTH
This worked. it also correctly generates the text document as requested.
Thanks.
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162`` ``````#include #include #include using namespace std; int main() {int d, rp, tp, vp, t, rc, tc, vc; char rm, ts, tv; const char* r; ofstream bill; bill.open("bill.txt"); { cout << "This program is used to create a bill statement \n"; } bill << "Community Hospital Patient Billing Statement\n"; { cout << "How many days did the patient stay? "; cin >> d; } bill << "Number of days stayed: " << d << "\n"; { cout << "\nWhat type of room? \nP for private, S for semi-private, W for ward: "; cin >> rm; if ( rm == 'P' || rm == 'p' ) rp = 200; if ( rm == 'S' || rm == 's' ) rp = 150; if ( rm == 'W' || rm == 'w' ) rp = 100; } { bill << "Type of room: "; if ( rm == 'P' || rm == 'p' ) r = "Private"; if ( rm == 'S' || rm == 's' ) r = "Semi-Private"; if ( rm == 'W' || rm == 'w' ) r = "Ward"; bill << r << "\n"; } rc = rp * d; bill << "Room Charge: " << rc << "\n"; { cout << "\nWas there telephone service? Y for yes, N for No: "; cin >> ts; if( ts == 'Y' || ts == 'y' ) tp = 1.75; else(tp = 0); } tc = tp * d; bill << "Telephone charge: " << tc << "\n"; { cout << "\nWas there television service? Y for yes, N for No: "; cin >> tv; if( tv == 'Y' || tv == 'y' ) vp = 1.75; else(vp = 0); } vc = vp * d; bill << "Television Charge: " << vc << "\n"; { t = vc + tc + rc; //tv prive times days + telephone times days + room cost times days cout << "\nThe total bill is: " << t; } bill << "Total Due: \$" << t << "\n"; bill.close(); }``````
Last edited on
 ``1234567`` `````` cout << "\nWhat type of room? \nP for private, S for semi-private, W for ward: "; cin >> rm; rm = std::toupper(rm) if ( rm == 'P' ) rp = 200; if ( rm == 'S' ) rp = 150; if ( rm == 'W') rp = 100;``````

I still don't see why you have braces around everything.
well, it works, and to me, thats good enough lol. the last issue i am hitting is that it won't use decimal numbers...

i tried double and float, but i only get whole numbers
 well, it works, and to me, thats good enough lol

Yes, but shouldn't you strive to be better? It might work, but doesn't mean it is good. If you have these habits now for this small program, what is it going to be like when you do a much bigger program? There is strong potential for it to be a big mess.

 i tried double and float, but i only get whole numbers

Let's see your new code then.
Going back to my first post, C++ isn't my thing... after this, i'm going back to java.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566`` ``````#include #include #include #include using namespace std; int main() { float t, tp, vp, d, rp, rc; char rm, ts, tv, tc, vc; const char* r; ofstream bill; bill.open("bill.txt"); { cout << "This program is used to create a bill statement \n"; } bill << "Community Hospital Patient Billing Statement\n"; { cout << "How many days did the patient stay? "; cin >> d; } bill << "Number of days stayed: " << d << "\n"; { cout << "\nWhat type of room? \nP for private, S for semi-private, W for ward: "; cin >> rm; if ( rm == 'P' || rm == 'p' ) rp = 200; if ( rm == 'S' || rm == 's' ) rp = 150; if ( rm == 'W' || rm == 'w' ) rp = 100; } { bill << "Type of room: "; if ( rm == 'P' || rm == 'p' ) r = "Private"; if ( rm == 'S' || rm == 's' ) r = "Semi-Private"; if ( rm == 'W' || rm == 'w' ) r = "Ward"; bill << r << "\n"; } rc = rp * d; bill << "Room Charge: " << rc << "\n"; { cout << "\nWas there telephone service? Y for yes, N for No: "; cin >> ts; if( ts == 'Y' || ts == 'y' ) tp = 1.75; else(tp = 0); } tc = tp * d; bill << "Telephone charge: " << tc << "\n"; { cout << "\nWas there television service? Y for yes, N for No: "; cin >> tv; if( tv == 'Y' || tv == 'y' ) vp = 3.50; else(vp = 0); } vc = vp * d; bill << "Television Charge: " << vc << "\n"; { t = vc + tc + rc; //tv prive times days + telephone times days + room cost times days cout << "\nThe total bill is: " << t; } bill << "Total Due: \$" << t << "\n"; bill.close(); return 0; }``````

Last edited on
Ok, so what was the input, and what did the output look like?

My other advice about using the `std::toupper` is independent of the language you program in - I imagine it is still reasonable advice whether you use Java, PASCAL, C, Python, FORTRAN or whatever.
`Going back to my first post, C++ isn't my thing... after this, i'm going back to java.`

So, Java uses bunch of unnecessary braces? I don't think so.

Lines 13, 15, 17, 20, 22, 29, 30, 36, 39, 45, 48, 54, 57 and 61 are all unnecessary and just clutter up your code and make it hard to read. This style would be hard to read in Java, too.
the input i'll use as an example:

Number of days in hospital : 5-------------- d = 5
Type of room : Private------------------------ rp = 200
Room Charge \$ 1000.00--------------------- d * rp = rc ---- RC = 1000
Telephone Charge \$ 0.00------------------- d * tp = tc ----- TC = 0
Television Charge \$ 17.50------------------- d * vp = vc ---- VC = 17.50
Total Due \$ 1017.50 -------------------------- rc + tc + vc = t -- 1017.50

the program only gives the total as 1017

it is ignoring decimal numbers.

text doc output:
Community Hospital Patient Billing Statement
Number of days stayed: 5
Type of room: Private
Room Charge: 1000
Telephone charge:
Television Charge: {random character}
Total Due: \$1017
Last edited on
tc is a char, but you're using it to store a number. When you output tc to file, because it is a char and not a number, you get out a char - not a number.

This is why we don't give variables ridiculous short names like tc. If you had named your variables sensibly, and called it something like "telephoneCharge", you would have spotted instantly that making it a char was silly.

Do not use ridiculous short names like tc for your variables. You are making it harder for yourself.
Last edited on
 ``12`` ``````float t, tp, vp, d, rp, rc; char rm, ts, tv, tc, vc;``````

 ``1234567`` ``````cout << "\nWas there telephone service? Y for yes, N for No: "; cin >> ts; if( ts == 'Y' || ts == 'y' ) tp = 1.75; else(tp = 0); } tc = tp * d;``````

Telephone charge is a char, which is really a small int, hence your problem. Sam e for some of the other variables.
Topic archived. No new replies allowed.