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.

To get a character use cin>>var; to verify that character you can use an if such as if( var=='a' || var=='A') which would check for upper or lower case character a. You can also look into the character formatting functions such as toupper() which converts a character to upper case so you only have to check the upper case values and not upper and lower case.

I would suggest a loop something like the following to solve your problem

bool done = false;

while(!done)
{ 
   printInputOptions();

   char usrIn;
   //get the input 
   cin>>usrIn;
   //clean up the stream
   // see "how do i flush the input stream" post by narue for good information on this

   //do your job
   switch(usrIn) //like a big if/elseif block
   {
      case 'A':
      case 'a':
          {
              doAreaCalc();
          }
          break;

      case 'V'
      case 'v':
          {
              doVolumeCalc();
          }
          break;

      case 'q':
      case 'Q':
          {
              done = true; //quit condition
          }
          break;

      default:
          {
              printInvalidInput();
          }
          break;

   }//end switch

}//end while(!done)

This is a simple bit effective means by which to handle multiple inputs from the user that all do something unrelated and allows you to say to the user that thier input was incorrect and to try again if you want to with a fucntion like printInvalidInput() Of course i hope you will realise that each function i have there is a place where you would put all the code to do each job that has to be done.

Hope this helps you

Gah, so confusing. I'm gonna learn a bit more, then come back to this, because i dont really understand at the moment.

Thanks for the help anyway, ill refer back to this later.

To help you a bit more ill give you a few pointers, To understand that code you should google up on while loops, switch statements and writing functions. Thats all i have used here and they are simple enough concepts.

Once you have had a look at those concepts this should be easy to understand. Post back if you dont understand anything in my code once you read about those.

Gah, so confusing. I'm gonna learn a bit more, then come back to this, because i dont really understand at the moment.

Thanks for the help anyway, ill refer back to this later.

You should probably find a textbook or some sort and learn from that. If you follow it step by step, that code should seem simple in no time.

You should probably find a textbook or some sort and learn from that. If you follow it step by step, that code should seem simple in no time.

I do :) I have "C++ A beginners guide" by Herbert Schildt. I'm usually a fast learner, but this is kinda hard.

ok ill point you a couple of links, switch statements , while loops (in my code i have used while(! done) to say while my variable done is false do the code in the while loop (i just prefer it that way) and finally and the only complex part functions

I think these tutorials give a good description for a beginner and should help you out

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.