Could someone help me with an assigment?

Hello! I'm a new female coder and I have started getting Computer science assignments but do not fully understand some of them. Can someone please help me with this assignment and how I would lay it out and everything? thank you!

"Write a program with a function that uses local variables in the function to calculate how much money you will earn for working a certain number of hours a week at a particular rate per hour. Prompt the user to enter the hours worked and rate of pay."

This is c++ and we have just started using functions. I have completely been lost since then.
This might get you started.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
"Write a program with a function that uses local variables in the function to 
calculate how much money you will earn for working a certain number of hours a 
week at a particular rate per hour. Prompt the user to enter the hours worked 
and rate of pay."
*/

#include <iostream>

double calc_pay()
{
    int num_hours = 0;
    double hourly_rate = 0.0;
    
    // TODO
    // Read the user input, do calculation and return result
}

int main()
{
    std::cout<< "Welcome at the Pay Calculator\n";
    calc_pay();
}


I have completely been lost since then.


What part are you having problems with? Can you prompt the user and obtain the inputs? Can you perform the calculation from the inputs? If you didn't have to use a function, could you then code the program? Post your current code.
@thmm: You wrote a function that returns a value, yet you use (call) it without making use of the returned value. That (line 22) is not good for educational purposes.

Doesn't modern C++ have an (optional) "Thou shalt not ignore the value I give!" attribute for functions?
local variables in the function

@thmm helpful suggestion is OK. Maybe the question is OK too and parameter passing comes later in @OP's burgeoning career in coding.
Taken all back now because I missed the double.
Last edited on
@keskiverto,
I got a bit confused. The instruction doesn't say if the function should return a value or print the result.
Doesn't modern C++ have an (optional) "Thou shalt not ignore the value I give!" attribute for functions?
Yes I think its called [[nodiscard]], but that's an overkill for beginners
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

[[nodiscard]]
double calc_pay() {
	int num_hours {};
	double hourly_rate {};

	// TODO
	// Read the user input

	return num_hours * hourly_rate;
}

int main() {
	std::cout << "Welcome to the Pay Calculator\n";
	std::cout << calc_pay() << '\n';
}

> Doesn't modern C++ have an (optional) "Thou shalt not ignore the value I give!" attribute for functions?

Not as strong as "thou shall not ignore".
The compiler "is encouraged" to issue a warning (if the discarded value is not cast to void)
Registered users can post here. Sign in or register to post.