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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <fstream>
#include <stdio.h>
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?
1
2
3
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <fstream>
#include <stdio.h>
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.
1
2
{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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <fstream>
#include <stdio.h>
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
1
2
3
4
5
6
7
         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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <iomanip>
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
1
2
float t, tp, vp, d, rp, rc;
char rm, ts, tv, tc, vc;


1
2
3
4
5
6
7
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.