How can I write this differently

Consider the definition of the function main:

1
2
3
4
5
6
7
int main()
{
	int x, y;
	char z;
	double rate, hours;
double amount;.
}


The variables x, y, z, rate, and hours referred to in terms a through f below are the variables of the function main. Each of the functions described must have the appropriate parameters to access these variables.

a.Write the definition of the function initialize the initialize x and y to 0, and z to the blank character.
1
2
3
4
5
6
void initialize(int& one, int& two, char& ch)
{
	one = 0;
	two = 0;
	ch = ' ';
}



b.Write the definition of the function getHoursRate that prompts the user to input the hours worked and rate per hour to initialize the variable hours and rate of the function main.
1
2
3
4
5
6
7
8
9
10
void getHoursRate(double& hrs, double& pRate)
{
	cout << "Enter hours worked: ";
	cin >> hrs;
	cout << endl;

	cout << "Enter pay rate: ";
	cin >> pRate;
	cout << endl;
}
This really looks like some sort of homework question. No one is going to do your homework for you. See what you can do yourself then when your own code gives errors we'll help with that.

Your assignment is asking you to use the functions given in questions 'a' and 'b' but apply them to the variables x, y, z etc.
So just rewrite the functions using the variables from the question. Instead of initializing "one" to 0, initialize "x" to 0 etc.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
void initialize( int& x, int& y, char& z);
int main()
{
	int x, y;
	char z='a';
	double rate, hours;
     double amount;
     cout<<x<<' '<<y<<" w:"<<z<<":w"<<endl;
     initialize(x,y,z);
     cout<<x<<' '<<y<<" w:"<<z<<":w"<<endl;
     system ("pause");
     return 0;
}
void initialize( int& x, int& y, char& z)
{
     x=0;
     y=0;
     z=' ';
}
     

Does this work for a.......................
Last edited on
Is b ok?
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
#include <iostream>
using namespace std;
void initialize( int& x, int& y, char& z);
void getHoursRat (double& hours, double& rate);
int main()
{
int x, y;
char z='a';
double rate, hours;
double amount;
cout<<x<<' '<<y<<" w:"<<z<<":w"<<endl;
cout<<hours<<' '<<rate<<endl;
initialize(x,y,z);
getHoursRat(hours, rate);
cout<<x<<' '<<y<<" w:"<<z<<":w"<<endl;
cout<<hours<<' '<<rate<<endl;
system ("pause");
return 0;
}
void initialize( int& x, int& y, char& z)
{
x=0;
y=0;
z=' ';
}
void getHoursRat (double& hours, double& rate)
{
cout<<"Include hours:";
cin>>hours;
cout<<"Include rate:";
cin>>rate;
}

thanks.......................
From what I understand of the question that is what they are looking for.

You don't seem to understand passing references to functions (judging by your code only) maybe you should look them up in more detail.

Even though the program can differentiate between variables its never a good idea to use ambiguous names for variables .
Topic archived. No new replies allowed.