here's my code:

cin>>figure;
switch (figure)
    {
           case '1': //figure is circle
           system("cls");
           cout<<"input radius: ";
           cin>>rad;
           cout<<"\nsurface area of your circle is "<<pi*rad*rad;
           break;
           case '2': //figure is rectangle
           system("cls");
           cout<<"input length: ";
           cin>>length;
           cout<<"\ninput width: ";
           cin>>width;
           cout<<"\nsurface area of your rectangle is "<<length*width;
           break;
     }

1)when i input for example "12" at CIN>>FIGURE,the program goes to case1 then won't ask the user to input a radius and treats second digit,which is "2",as it's radius.

question: how can i solve this one?

2)the program exits immediately when the user enters a non-numeric value on an input that needs to be computed.

question:how can i make the program ask the user to input a new value instead of exiting?

Is figure integer or character?

i declared figure as character

1) Assuming that "figure" is a character, the input statement only takes the first non-whitespace character from the input stream. So, in your example, the '2' is left to be processed, and it then becomes the first input to whichever figure is being calculated.

One way to handle this is to clear out the input stream after you get the user's choice:

cin >> figure;
cin.ignore( 10, '\n' );
switch( figure )

The ignore statement will throw away the next 10 characters on the input stream or until a newline is encountered, whichever comes first. You can use any number you want as the first argument. This guarantees you start with a fresh input to your next cin statement.

2) This one takes a bit more work - you have to evaluate every input and if necessary, clear the input stream and ask again. Here you will use the cin.ignore( ) as above, and also the cin.clear( ) to reset the error flag. How do you determine if a valid input has been entered? I will leave that as question for you for the moment - consult your text, probably in the section on loops.
Val

1)thanks it worked. it's ok but it'd probably be better if the program treats an input like '12' as with 'default'.

2)i tried solving this one having a condition but doesn't work:

switch (figure)
    {
           case '1': //figure is circle
           do
           {
           
           system("cls");
           cout<<"input radius: ";
           cin.clear();
           cin>>rad;
           }while ((rad<-32767)||(rad>32767));

if i entered for example 'a', program starts to 'blink' and not respond lol

Why don't make figure an integer since you want to get only the number.

to invisal: it's when entering a value to a variable that needs to be computed where my problem occurs.(rad,length,etc.)
i declared all of those variables as float.

anyway,
regarding my 2nd question,does that one need complicated stuffs to solve my prob?

the only solution i can think of is the one i posted above which is having a condition,which didn't work.

i have no idea on how to have a condition that does not accept non-numeric values

maybe i need to use a/some predefined libraries?

instead of cin.ignore

i used a dowhile instead:

do
{
statements;
}while ((figure<1)||(figure>2));

sweet.

2)i tried solving this one having a condition but doesn't work:

switch (figure)
    {
           case '1': //figure is circle
           do
           {
           
           system("cls");
           cout<<"input radius: ";
           cin.clear();
           cin>>rad;
           }while ((rad<-32767)||(rad>32767));

if i entered for example 'a', program starts to 'blink' and not respond lol

anyone who wouldn't mind telling me if my logic with my 2nd problem
was right or even just close enough

or was that just rubbish?if so,how can i fix that prob?HOW?!?! lol

Yep, it's rubbish.

What you've posted is not enough for us to really help. You don't tell us what the variable types are so we have to guess. You don't have a default case but you claim you want to use one. And what that loop is supposed to do only you can tell. It makes no sense to us.

Post the entire program and ask a full question complete with what you want to happen and what current;y does happen.

And please get rid of system("cls"); . They just get in the way. If you really want them, add them back when you get the program running.

here you go:

#include <iostream.h>

//declared variables:
float length,width,side,base,height,rad;
float pi=3.1416;
char number;
char x;//var for do while loop
int main()
{

do
{
here://location where switch default goes.
     do
     {
    cout<<"Choose a number: \n 1 - circle\n 2 - rectangle\n 3 - triangle\n 4 - parallelogram"
        <<"\n 5 - cube\n 6 - sphere\n 7 - cylinder\n 8 - cone\n 9 - exit\n\nENTER A NUMBER: ";
    cin>>number;//user inputs a number
    cin.clear();
     }while ((number<1)&&(number>9));
switch (number)
    {
           case '1': //figure is circle
           cout<<"input radius: ";
           cin>>rad;
           cout<<"\nsurface area of your circle is "<<pi*rad*rad;
           break;
           case '2': //figure is rectangle
           cout<<"input length: ";
           cin>>length;
           cout<<"\ninput width: ";
           cin>>width;
           cout<<"\nsurface area of your rectangle is "<<length*width;
           break;
           case '3': //figure is triangle
           cout<<"input height: ";
           cin>>height;
           cout<<"\ninput base: ";
           cin>>base;
           cout<<"\nsurface area of your triangle is "<<base*(height/2);
           break;
           case '4': //figure is parallelogram
           cout<<"input base: ";
           cin>>base;
           cout<<"\ninput height: ";
           cin>>height;
           cout<<"\nsurface area of your parallelogram is "<<base*height;
           break;
           case '5': //figure is cube
           cout<<"input side: ";
           cin>>side;
           cout<<"\nsurface area of your cube is "<<6*(side*side);
           break;
           case '6': //figure is sphere
           cout<<"input radius: ";
           cin>>rad;
           cout<<"\nsurface area of your sphere is "<<4*pi*(rad*rad);
           break;
           case '7': //figure is cylinder
           cout<<"input radius: ";
           cin>>rad;
           cout<<"\ninput height: ";
           cin>>height;
           cout<<"\nsurface area of your cylinder is "<<2*pi*rad*(height+rad);
           break;
           case '8': //figure is cone
           cout<<"input radius: ";
           cin>>rad;
           cout<<"\ninput slant height: ";
           cin>>height;
           cout<<"\nsurface area of your cone is "<<(pi*rad*rad)+(pi*rad*height);
           break;
           case '9': //goes to exit page
           goto exit;
           break;
           default: //if input figure not found in choices,user needs to input again a number
           goto here;
           break;
    }//end of case statements
	
	cout<<"\n\ncompute new figure?[y/n]:";
	cin>>x;

}while ((x=='y')||(x=='Y'));

    
}

what i want to happen is that when the user's input to variables such as rad,length,width,etc.
is non-numeric,the user should be asked again to input another value.

To make your numeric input more error-proof, what about:

case '1': //figure is circle
           cout<<"input radius: ";
           while ( ! (cin>>rad) )
           {
                cout << "Bad input, enter radius:  ";
                cin.clear( );
               cin.ignore( 10, '\n' );
            }
     
           cout<<"\nsurface area of your circle is "<<pi*rad*rad;
           break;

Now, tell us truly, have you successfully compiled and tested the code you posted? In Visual C++ 2003, I find several compile errors.

Your use of labels and goto's is not needed. Since you validate the operation selection, only legal choices ever make it to the switch, so default is not needed. That's a pretty ungraceful way of handling the problem, in any case. Your quit option jumps to label exit, which doesn't exist.

A better model for this is something like

do
{
      //display menu
      //get user choice
      switch( choice )
      {
           case '1':  //do stuff
                         break;
            case'2':  //do other stuff
                         break;
             //etc
            case '9': //the quit option
                         cout << "Goodbye";
                         break;
             default:  cout << "invalid option";
        }
} while ( choice != '9' );

Simpler, less jumping around, let the switch take care of validating user input.

i did what WaltP said to remove system("cls") and i also removed other output that won't affect the program

sorry i didn't realize i removed the exit for goto

here's my source,kindly tell me that what to's and not:

#include <iostream.h>

float length,width,side,base,height,rad;
float pi=3.1416;
char number;
char x; //to loop or not to loop...

int main()
{
      //there's an ASCII picture that will display here
do
{
here:
     do
     {
    cout<<"Choose a number: \n 1 - circle\n 2 - rectangle\n 3 - triangle\n 4 - parallelogram"
        <<"\n 5 - cube\n 6 - sphere\n 7 - cylinder\n 8 - cone\n 9 - exit\n\nENTER A NUMBER: ";
    cin>>number;//user inputs a number
    cin.clear();
     }while ((number<1)&&(number>9));
switch (number)
    {
           case '1': 
           cout<<"input radius: ";
           cin>>rad;
           cout<<"\nsurface area of your circle is "<<pi*rad*rad;
           break;
           case '2': 
           cout<<"input length: ";
           cin>>length;
           cout<<"\ninput width: ";
           cin>>width;
           cout<<"\nsurface area of your rectangle is "<<length*width;
           break;
           case '3':
           cout<<"input height: ";
           cin>>height;
           cout<<"\ninput base: ";
           cin>>base;
           cout<<"\nsurface area of your triangle is "<<base*(height/2);
           break;
           case '4':
           cout<<"input base: ";
           cin>>base;
           cout<<"\ninput height: ";
           cin>>height;
           cout<<"\nsurface area of your parallelogram is "<<base*height;
           break;
           case '5':
           cout<<"input side: ";
           cin>>side;
           cout<<"\nsurface area of your cube is "<<6*(side*side);
           break;
           case '6': //figure is sphere
           cout<<"input radius: ";
           cin>>rad;
           cout<<"\nsurface area of your sphere is "<<4*pi*(rad*rad);
           break;
           case '7':
           cout<<"input radius: ";
           cin>>rad;
           cout<<"\ninput height: ";
           cin>>height;
           cout<<"\nsurface area of your cylinder is "<<2*pi*rad*(height+rad);
           break;
           case '8':
           cout<<"input radius: ";
           cin>>rad;
           cout<<"\ninput slant height: ";
           cin>>height;
           cout<<"\nsurface area of your cone is "<<(pi*rad*rad)+(pi*rad*height);
           break;
           case '9': //goes to exit page
           goto exit;
           break;
           default: //if input figure not found in choices,user needs to input again a number
           goto here;
           break;
    }//end of case statements
	
	cout<<"\n\ncompute new figure?[y/n]:";
	cin>>x;

}while ((x=='y')||(x=='Y'));
exit: 
    //there's an ASCII picture that will disply here
    return 0;
    
}//end

why i validated a do while before the switch? so that if the user enters 12,program take it as an invalid choice because if i'll use cin.ignore without that loop,the program will go to case '1'.

why is there a default? so that if the user enters a non-numeric value,still the program take that as invalid.

my program does work and i'm using devC++
my program runs fine although i get these:
32:2 C:\Dev-Cpp\include\c++\3.4.2\backward\backward_warning.h #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.

15 C:\Dev-Cpp\include\c++\3.4.2\backward\iostream.h:31, from main.cpp In file included from C:/Dev-Cpp/include/c++/3.4.2/backward/iostream.h:31, from main.cpp

Well, your attempt at validating input is not really working

while ((number<1)&&(number>9));

Remember that number is a char type. You are comparing it to integer values. The ASCII values of the characters '1' - '9' are much larger than the numeric values 1-9.

Also, can any number be both less than 1 and at the same time greater than 9? Is AND (&&) what you want in this test?

As to the warnings you get, the version of DevC++ must be less picky about the iostream library. Correct current usage is #include <iostream> (note the lack of .h) With this, you should also include the line using namespace std; to gain access to the input and output operations you use.

Also, can any number be both less than 1 and at the same time greater than 9? Is AND (&&) what you want in this test?

oh yeah

lol that's weird

with my FIRST POST
about this do while loop,i put OR(||) :))

tss..my carelessness

//edit

wait,now i remember why
i used && because when i used ||,
and tried to input anything even numbers in the choices whice are 1-9 ,program takes it as invalid.

//edit

aghhh...it is not working now lol
what happened T_T

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.