I cannot set my program to display the output i want

This is going to be a wall of text.
I have 2 questions to ask
1) There's an error with my output and I tried to redo it a couple of times. I think it has got something to do with pointers but i'm not too good in that. But i'm not sure if that's the solution.

2)I wish to include sounds like the window logon sound instead of beeps, do you think it's possible to do that? If yes, can you explain or give some hints to what I should search for?

Thanks in advance. This is my code. I am uising Microsoft Visual Studio to do my C++.
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <windows.h>
using namespace std;
class Patient
{
private:
string name;
string ICno;
int HPno;
public:
void entername(string n){name=n;}
void showname() {cout<<"You have just entered your name as : "<<name<<endl;}
void enterIC(string ic){ICno=ic;}
void showIC() {cout<<"Your IC number is : "<<ICno<<endl;}
void enterHP(int hp){HPno=hp;}
void showHP () 
{cout<<"Your contact number is : "<<HPno<<endl;
cout<<setfill('*')<<setw(80)<<"\n ";}
};
class Fee
{
private:
int Total,day,others,choice,surgery,med,medchoice;
public:
Fee(void){Total=0;}
int enter_day()
{
cout<<"\n\nPlease enter Number of day(s)that you will be staying in the Hospital : ";
cin>>day;
cout<<setfill('*')<<setw(80)<<"\n";
cout<<"Calculating and uploading new amount...";
Sleep( 2000 );
system ( "cls" );
return 0;
}
void getTotal(int amount)
{ 
Total=(day*amount)+others+med;
}
void showTotal()
{	
cout<<"\n\n\nThe total payment is $ "<<Total<<endl;
}
int get_others()
{
do{
cout<<setfill('*')<<setw(80)<<"\n\t\t\tOther Fees"<<endl;
cout<<setfill('*')<<setw(80)<<"*\n";
cout<<"Type 1:Back surgery\t$7800\nType 2:Ear surgery\t$4900\nType 3:Plastic surgery\t$5000\nType 4:Nose surgery\t$3500\nType 5:Leg surgery\t$6000\n";
cout<<setfill('*')<<setw(80)<<"\n";
cout <<"Enter   1 for back surgery\n\t2 for ear surgery\n\t3 for plastic surgery\n\t4 for nose surgery\n\t5 for leg surgery : ";
cin>>surgery;	
switch(surgery)
{
case 1 : cout<<"You have choosed back surgery."<<endl;
cout<<setfill('*')<<setw(80)<<"\n";
cout<<"This surgery will cost you $7800\n ";
others=7800;
break;

case 2 : cout<<"You have choosed ear surgery."<<endl;
cout<<setfill('*')<<setw(80)<<"\n";
cout<<"This surgery will cost you $4900 \n";
others=4900;
break;

case 3 : cout<<"You have choosed plastic surgery."<<endl;
cout<<setfill('*')<<setw(80)<<"\n";
cout<<"This surgery will cost you $5000. \n";
others=5000;
break;
					
case 4 : cout<<"You have choosed nose surgery."<<endl;
cout<<setfill('*')<<setw(80)<<"\n";
cout<<"This surgery will cost you $3500. \n";
others=5000;
break;
					
case 5 : cout<<"You have choosed leg surgery."<<endl;
cout<<setfill('*')<<setw(80)<<"\n";
cout<<"This surgery will cost you $6000. \n";
others=6000;
break;
default : 
cin.clear();
cin.ignore( 1000, '\n' );
system ( "cls" );
cout << "\n\n\t\tIncorrect Input!\n\n";
Sleep( 2000 );              
break;
}
} while (surgery <1 || surgery >6);
cout<<setfill('*')<<setw(80)<<"\n";
cout<<endl;
cout<<"Calculating and uploading new amount...";
Sleep( 5000 );    
system ( "cls" );
return others;
}
int get_medication (void)
{
do{
cout<<setfill('~')<<setw(80)<<"\n";
cout<<"Type 1:A\t$1000\nType 2:B\t$2500\nType 3:C\t$1500\nType 4:D\t$1700\nType 5:E\t$600\n";
cout<<setfill('~')<<setw(80)<<"\n";
cout <<"Enter   1 for A\n\t2 for B\n\t3 for C\n\t4 for D\n\t5 for E : ";
cin>>medchoice;	
switch(medchoice)
{
case 1 : cout<<"You have choosed A."<<endl;
cout<<setfill('*')<<setw(80)<<"\n";
cout<<"This medication will cost you $1000\n ";
med=1000;
break;
case 2 : cout<<"You have choosed B."<<endl;
cout<<setfill('*')<<setw(80)<<"\n";
cout<<"This medication will cost you $2500 \n";
med=2500;
break;
case 3 : cout<<"You have choosed C."<<endl;
cout<<setfill('*')<<setw(80)<<"\n";
cout<<"This medication will cost you $1500. \n";
med=1500;
break;
case 4 : cout<<"You have choosed D."<<endl;
cout<<setfill('*')<<setw(80)<<"\n";
cout<<"This medication will cost you $1700. \n";
med=1700;
break;
case 5 : cout<<"You have choosed E."<<endl;
cout<<setfill('*')<<setw(80)<<"\n";
cout<<"This medication will cost you $600. \n";
med=600;
break;
case 6: cout<<"No medication is needed."<<endl;
med=0;
break;
default : 
cin.clear();
cin.ignore( 1000, '\n' );
system ( "cls" );
cout << "\n\n\t\tIncorrect Input!\n\n";
Sleep( 2000 );              
break;
}	
} while (medchoice <1 || medchoice >7);
return 0;
cout<<setfill('*')<<setw(80)<<"\n";
cout<<endl;
cout<<"Calculating and uploading new amount...";
Sleep( 2000 );
system ( "cls" );
}
};
void setBeep1(void)
{
Beep(523,300); // 523 hertz (C5) for 300
Sleep(100);
Beep(784,500);
}
void setBeep2(void)
{
Beep( 100, 100 ); 
Beep( 500, 200 );
Beep( 1000, 300 ); 
Beep( 1500, 400 );
Beep( 1900, 1000 );
}
void main()
{
string x,y;
int z,price;
char choice;

Patient patient1;
Fee money1;
cout<<"\t\t\t\t\t\t\t\t\t\t\t\t\tHospital ABC"<<endl;
ofstream outfile;
outfile.open("Patient Fee");
cout<<"Please key in your name as shown in your IC :"<<endl;
getline(cin,x);
patient1.entername(x);
setBeep2();
cin.get();
cout<<endl<<"Please key in your IC number as shown in your IC/Birth Cert :"<<endl;
cin>>y;
patient1.enterIC(y);
cin.get();
cout<<endl<<"Please key in your contact number :"<<endl;
cin>>z;
cout<<endl<<endl<<endl<<endl;
patient1.enterHP(z);
cin.get();
system ( "cls" );
patient1.showname();
patient1.showIC();
patient1.showHP();

do{	
cout<<endl<<setfill('*')<<setw(80)<<"\n";
cout<<"\n\t\t\tCLASS OF WARD ACCOMODATION";
cout<<endl<<setfill('*')<<setw(80)<<"\n ";
cout<<"\n\tAcute Ward\tIntensive Care Unit\tConsultation Fee\tTotal";
cout<<"\nA\t$230\t\t$500\t\t\t$50\t\t\t$780";
cout<<"\nB\t$150\t\t$300\t\t\t$40\t\t\t$490";
cout<<"\nC\t$75\t\t$150\t\t\t$25\t\t\t$250";
cout<<endl<<setfill('*')<<setw(80)<<"\n";
cout<<setfill('*')<<setw(80)<<"\n";

cout <<"\n\n\n\nEnter the class of Ward you want to enter : \n";
cin>>choice;

switch(choice)
{
case ('A') : cout<<"\n\n\nYou have choosed ward A."<<endl;
cout<<"This ward will cost you $780 per day";
price=780;
money1.getTotal(price);
outfile<<"You have choosen ward A."<<endl;
outfile<<"This ward will cost you $780 per day";
break;
case ('a') : cout<<"\n\n\nYou have choosed ward A."<<endl;
cout<<"This ward will cost you $780 per day";
price=780;
money1.getTotal(price);
outfile<<"You have choosen ward A."<<endl;
outfile<<"This ward will cost you $780 per day";
break;
case ('B') : cout<<"\n\n\nYou have choosed ward B."<<endl;
cout<<"This ward will cost you $490 per day";
price=490;
money1.getTotal(price);
outfile<<"You have choosen ward B."<<endl;
outfile<<"This ward will cost you $490 per day";			
break;
case ('b') : cout<<"\n\n\nYou have choosed ward B."<<endl;
cout<<"This ward will cost you $490 per day";
price=490;
money1.getTotal(price);
outfile<<"You have choosen ward B."<<endl;
outfile<<"This ward will cost you $490 per day";
break;
case ('C') : cout<<"\n\n\nYou have choosed ward C."<<endl;
cout<<"This ward will cost you $250 per day";
price=250;
money1.getTotal(price);
outfile<<"You have choosen ward C."<<endl;
outfile<<"This ward will cost you $250 per day";
break;
case ('c') : cout<<"\n\n\nYou have choosed ward C."<<endl;
cout<<"This ward will cost you $250 per day";
price=250;
money1.getTotal(price);
outfile<<"You have choosen ward C."<<endl;
outfile<<"This ward will cost you $250 per day";
break;
default :                    
cin.clear();
cin.ignore( 1000, '\n' );
system ( "cls" );
cout << "\n\n\t\tIncorrect Input!\n\n";
Sleep( 2000 );              
break;    
}
} while (choice <'A' || choice >'c');
cout<<endl;
money1.enter_day();
money1.get_others();
money1.get_medication();
money1.showTotal();
setBeep1();
outfile.close();
}
Last edited on
Argh. Please use some indentation and line spacing and I'll take a look at it.

In Windows, you can use the PlaySound() function for sound effects. Here's an example where I play the standard "chimes.wav" in your Windows Media folder.
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
#pragma comment( lib, "winmm.lib" )

#include <iostream>
#include <limits>

#include <windows.h>

int main()
  {
  using namespace std;

  cout << "Press ENTER to hear a chime> ";
  cin.ignore( numeric_limits <streamsize> ::max(), '\n' );

  TCHAR filename[ MAX_PATH ];
  GetWindowsDirectory( filename, MAX_PATH );
  lstrcat( filename, TEXT( "\\Media\\chimes.wav" ) );

  PlaySound( filename, NULL, SND_FILENAME );

  cout << "Press ENTER to quit";
  cin.ignore( numeric_limits <streamsize> ::max(), '\n' );

  return 0;
  }

You must link with winmm. I used the GCC:
g++ -Wall -pedantic a.cpp -lwinmm
but the above code has the VC++ #pragma.

Hey Duoas,
Thank you for solving question 2). I'm playing around with it now.

I've had trouble maintaining the word count with all the spaces, which was why i didnt leave any space.
1
2
3
4
5
6
7
8
void getTotal(int amount)
{ 
       Total=(day*amount)+others+med;
}
void showTotal()
{	
       cout<<"\n\n\nThe total payment is $ "<<Total<<endl;
}


The code was in the same class which was class Fee and i think it is unable to pass the value of Total from one function to another.
Last edited on
Yeah, since your variables are only changed inside the function, and you are not passing them via refrence, you are going to probably want to do something like this:

1
2
3
4
5
6
7
8
int getTotal(int amount)
{ 
       return (day*amount)+others+med;
}
void showTotal(int total)
{	
       cout<<"\n\n\nThe total payment is $ "<<total<<endl;
}


Then you can just do (wherever you want to show the total):

showTotal(getTotal(amount));

To pass by reference you would do:

1
2
3
4
void getTotal(int amount, int &total)
{ 
       total=(day*amount)+others+med;
}


You would still need to pass the value to showTotal(), however passing by reference will allow you to edit the variable once and not have to keep calling getTotal() everytime you want to get the value of total.
Last edited on
Hey firedraco, thanks for your reply, however after changing, it gives me some errors.

I've changed
1
2
3
4
5
6
7
8
int getTotal(int amount)
{ 
       return (day*amount)+others+med;
}
void showTotal(int total)
{	
       cout<<"\n\n\nThe total payment is $ "<<total<<endl;
}


and i tried to call out
showTotal(getTotal(amount));

in the main function with my class object in the front so it looks like
money1.showTotal(getTotal(amount));

it gives me 2 errors.
1) error C2065: 'amount': undeclared identifier
2) error C3861: 'getTotal': identifier not found

so i tried
money1.showTotal(Fee::getTotal(amount));

1) error C2065: 'amount': undeclared identifier
(and after adding int amount (which is obviously wrong) ) I got this error,
2)error C2352: 'Fee::getTotal': illegal call of non-static member function
From your last post only, it looks like you are trying to use 1) a variable that doesn't exist, and 2) a member function without an object.

1
2
3
int an_amount = 42;

money1.showTotal( money1.getTotal( an_amount ) );

Hope this helps.
Hey Duoas, thanks for clearing my errors again. I figured out the problem was from the an_amount. There wasn't any other errors after i declare an_amount and passing the variable to the function money1.getTotal but the output was different.

After some thinking I figured out that all I have to do was to pass the value of price over!

Thanks alot Duoas and all those that tried to help, it's a project for me and i'm trying to perfect it to get the best grades.

That's all for now, but i might want to add in new stuff to make it look better. I realise the <windows.h> is a very good header and could access things like playsound is there any other fun stuff that I should know before i submit my project? :D
You can do all sorts of weird and amazing things with <windows.h>. (I am presuming that your prof will be compiling all this on Windows.)

Hmm... here are a few interesting things I can think of (stuff people tend to want to do):

Clear the screen http://www.cplusplus.com/forum/beginner/1988/page3.html#msg10830

Press any key to continue... http://www.cplusplus.com/forum/beginner/1988/page3.html#msg14102
(See the last part of the code I posted.)

In the same vein: Unbuffered Input http://www.cplusplus.com/forum/beginner/3329/

Get Mouse and Window Events (and unbuffered input)
http://msdn.microsoft.com/en-us/library/ms686033(VS.85).aspx

Set the Console Title SetConsoleTitle( (LPCTSTR) text.c_str() ); Where 'text' is a std::string. You can also just give a char* directly.

Use Pop-up Windows Dialogues http://www.flounder.com/consoledialogs.htm
Any unique string will do for all that GUID stuff.
If you have Windows kernel 5.00 or later (you do if you have XP) you
can use GetConsoleWindow() instead of that FindConsoleHandle().
1
2
3
4
5
6
7
#if (_WIN32_WINNT < 0x0500)
  #undef  _WIN32_WINNT
  #define _WIN32_WINNT 0x0500
#endif
#include <windows.h>
...
HWND console_window = GetConsoleWindow();


Other Win32 Console Functions Reference
http://msdn.microsoft.com/en-us/library/ms682073(VS.85).aspx

And, if you are in to really weird stuff

Plot a Sinewave to the Console (or other graphics)
http://www.daniweb.com/code/snippet217.html

Put a Bitmap Image on your Console (a similar trick by the same guy)
http://www.daniweb.com/code/snippet174.html

Hmm... I can't think of anything else right now...

[edit] I have also found it a good idea to provide a makefile with assignments so the prof doesn't have to do anything special himself. Especially if you link with things like gdi32, etc. Remember, don't over-do anything. The prof should be pleasantly surprised by a couple of nice bells and whistles, but he shouldn't think that you have written a different application than the simple one he asked for.

Good luck!
Last edited on
Topic archived. No new replies allowed.