Hi guys, first post here. I decided to come here because it looks really friendly.
Ive looked around on the internet, but I dont really understand how to do this.
Take note that I am just beginning in c++. (Few days practice), so i decided to try to make a little test program that can help me improve. I only really know basic functions at the moment though. The program works out the area of a rectangle (yeah, easy.. but im terrible.) and now im trying to add on to that, and make it be able to work out the volume of a triangular prism too.
What I want: I want to add an option at the start of the program to ask the user whether they want to work out the volume, or area. Then it would run that part of the code. I really dont know how this would work, so i tried having a go, but im really stumped. I thought that i could somehow use a variable as a letter, and wait for that to be pressed, but I really dont have a clue. I also thought that when they press the letter, I would turn the boolean selection variable (whichever one they picked) to true, which would make it run that part of the program. Again, I really have no idea how this works. :/
So, how would you make the program wait for a specific button press? ("v" to work out the volume, "a" to work out the area)
My code so far: (its most likely messy.. and havn't started with the volume yet.) The program doesnt wait for the choice because I havn't put anything inbetween there, so it just goes straight onto the area. What I had in my head was wait for a key press, if a is pressed then run the area code, etc.. But I dont know.
#include <iostream>
using namespace std;
int main() {
int length, width, area; // variables
char volumeselectionletter, areaselectionletter; // variables
bool areaselection, volumeselection; // variables
areaselection = false; // variables
volumeselection = false; // variables
areaselectionletter = 'a'; // variables
volumeselectionletter = 'v'; // variables
cout << "What would you like to do? \n \n";
cout << "Press A to work out the area of a rectangle, \nor V to work out the volume of a triangular prism. \n"; // choice, (doesnt wait here, havn't got round to it yet)
cout << "Enter the length of the rectangle: ";
cin >> length;
cout << "Enter the width of the rectangle: ";
cin >> width;
area = length * width;
cout << "\n \n";
cout << length << " x " << width << " = " << area;
cout << "\nThe area of the rectangle is: " << area << "\n \n";
cout << "Press the enter key to exit. \n";
cin.get();
cin.get();
return 0;
}
It would be REALLY helpful if someone could walk me through it, but any help is appreciated. Thanks.