Easy Question...

The question is ...

Assume that qty and salesRep are both integers. Use a type cast expression to rewrite the following statements so it will no longer perform integer division

unitsEach = qty / salesRep

This is what I wrote

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  #include <iostream>
using namespace std;
int main()
{
	int qty,salesReps;
	
	cout<<"Enter the value of qty : "<<endl;
	cin>>qty;

	cout<<"Enter the value of salesReps : "<<endl;
	cin>>salesReps;

	float unitsEach = qty / salesReps;

	cout<<"The value of unitsEach is : "<<unitsEach<<endl;

	
return 0 ;
}


But my answer still comes out to be an integer...
You are not doing a typecast. You have to cast either qty or salesReps to be a floating point rather than an integer.

http://www.cplusplus.com/doc/tutorial/typecasting/

See the "explicit conversion" section near the top.
I did not under =S

if you could show a code to this answer, then it would be a great help...
There's code examples in that link:

1
2
3
short a=2000;
int b;
b = (int) a;    // c-like cast notation 


That last line casts 'a' (a short) to an int.

You're doing the same, but casting an int to a float.
So is this correct?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
int main()
{
	int qty,salesReps;
	double unitsEach;

	cout<<"Enter the value of qty : "<<endl;
	cin>>qty;

	cout<<"Enter the value of salesReps : "<<endl;
	cin>>salesReps;

	unitsEach = static_cast<double>(qty) / salesReps;

	cout<<"The value of unitsEach is : "<<unitsEach<<endl;

	
return 0 ;
}
you could stick a breakpoint on line 16 and find out for yourself.

i'd actually cast the divisor (i.e. salesReps), but yea, looks okay I think :)
How would you cast the divisor btw?
i meant to type "denominator" sorry.
but yea, just the same way as you've done the numerator.

unitsEach = qty / static_cast<double>(salesReps);

Another question :

write a multiple assignment statement that can be used instead of the following group of assignment statements

purple_houses = 20;
black_houses = 20;
pink_houses = 20;
orange_houses = 20;

What is missing/wrong in this statement? I can not figure it out..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>;
using namespace std;
int main()
{
double number1, number2, sum;

cout << "Enter a number: ";
cin >> number1;
cout << "Enter another number: ";
cin >> number2;

number1 + number2 = sum;

cout << "The sum of the two numbers is " << sum;

	return 0;

}
closed account (o3hC5Di1)
Hi there,

Please create separate topics for unrelated problems. As for your question, the problem is here:

number1 + number2 = sum;

Let's break down what happens:

- number2 is added to number1, creating a temporary result (also known as an rvalue)
- an rvalue cannot be assigned a new value, which is what you are trying to do by doing = sum

"sum" is an lvalue, basically meaning it can be of the left side of operator=(). All this to say that this line should look like this:

sum = number1 + number2;

Hope that helps.
All the best,
NwN
Topic archived. No new replies allowed.