136k views
4 votes
Given the height, length, and width of a wedge, assign wedgeVolume with the volume of the wedge.

Ex: If the input is 8 9 2, then the output is:
Volume: 72
Note: The volume of a wedge is calculated by multiplying height, length, and width, and dividing by 2.


My code:
#include
using namespace std;

int main() {
int height;
int length;
int width;
int wedgeVolume;

cin >> height;
cin >> length;
cin >> width;

cout << wedgeVolume = (height * length * width) /= 2;

cout << "Volume: " << wedgeVolume << endl;

return 0;
}
-----------------
Error:
main.cpp:14:55: error: lvalue required as left operand of assignment
14 | cout << wedgeVolume = (height * length * width) /= 2;
------------------
Suggestions? Thank you in advance!

User Suragch
by
7.1k points

1 Answer

3 votes

Answer:

#include <iostream>

int main() {

double height, length, width;

double volume;

// Get input from user

std::cout << "Enter height: ";

std::cin >> height;

std::cout << "Enter length: ";

std::cin >> length;

std::cout << "Enter width: ";

std::cin >> width;

// Calculate volume

volume = (height * length * width) / 2;

// Print result

std::cout << "Volume: " << volume << std::endl;

return 0;

}

Step-by-step explanation:

In C++, you can define multiple varibles of the same type in one line i.e. "double height, length, width". They'll all have a default value of null. Also, you should use doubles or floats to account for decimals. As well, the line

cout << wedgeVolume = (height * length * width) /= 2; wouldn't work because you cant print out a defintion as your defining it. You'd need to seperate the solving of the volume and the printing of the volume to get the code you have to work.

User Jrandomuser
by
6.8k points