Archived

This topic is now archived and is closed to further replies.

gomeza54

how do you trap a consonant?

Recommended Posts

i wrote a program that takes your age and based of that it tells you what grade you''re in. it''s just to practice my if-then statements. the program works fine, but when i was trying to trap any errors, i realized that the program would crash if the user inputed a consonant instead of a number. how can i prevent this? here''s the code:
  
//this program asks the user for their age

//and based on that it tells them what grade they''re in


#include <iostream.h>

int main ()
  {
   int age;
   int agree;
   cout << "What grade are you in? - By Gomeza54" << endl;
   cout << endl;
   enter_age: //this is a prompt that asks the user how old they are (duh!)

   cout << "Please enter your age: ";
   cin >> age;
   confirm_age: //this prompt lets the user change the value of the variable age

   cout << "Confirm. You are " << age << " years old? (1 - Yes 2- No) "; 
   cin >> agree;
   cin.ignore();

   // begin the if statements for the confirmation


      if (agree==1) {       // if agree is 1 then goto the age calculations

      cout << endl;
      goto age_calculation;
      }

      else if (agree==2) {  // if agree is 2 then give 

      cout << endl;                   // the user a chance to enter it''s age again

      goto enter_age;
      }

      else {                                                            // if agree is not any of the above

      cout << "That is an invalid option. Please try again." << endl;   // indicate that it is an invalid

      goto confirm_age;              					//entry and return to confirm_age

      }      
      
age_calculation: //this is where the age calculation starts      

   // begin the if statements for the age calculation


// if the age is less 3 or else then print out this and end program

      if (age<=3) {
      cout << "You are to young to be in school!" << endl;
      goto program_end;
      }

// if the age is 4 print out junior kindergarden

      else if (age==4) {
      cout << "You are in Junior Kindergarden" << endl;
      goto program_end;
      }

// if the age is 5 print out senior kindergarden

      else if (age==5) {
      cout << "You are in Senior Kindergarden" << endl;
      goto program_end;
      }
      
// if the age is more than 19 or more then print out this and end program

      else if (age>=19) {
      cout << "You are too old to be in school!" << endl;
      goto program_end;
      }

// if the age can go to school then perform two math equations to figure out grade

      else {
      cout << "You are in Grade " << (age-6) << " or Grade " << (age-5) << "." << endl;
      goto program_end;
      }

// when the program is over, run this

program_end:
      cout << "Press enter. ";
      cin.get ();
      return 0;
  }
  
btw - any constructive critism about the program would be very much accepted

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Remove all gotos... (constructive critisism)

As for your actual question input a string and extract a number from the string (look in your fav. reference for more info)

Share this post


Link to post
Share on other sites
Absolutely right. GOTO is evil. Don''t ever use it until you come to the situation where it is neccessary. ( only needed it 2 times in 7 years so far )

you want something like this:
CHAR ch;
BOOL CH_IS_INVALID = FALSE;
WHILE( CH_IS_INVALID )
{
input to the ch;
if( isdigit( ch ) ) <------- ***
CH_IS_INVALID = false;
}

do your processing here.

*** you could also use
if( strchr( "...", ch ) )
instead of ..., enter every possible valid choice.
if( strchr( "12345x", ch ))
will return true if ch contains 1,2,3,4,or 5 for choices or X for exit.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
GoTo are fine. As long as you use them correctly.

Share this post


Link to post
Share on other sites
Yeah you shouldn''t use gotos unless you *really* know what you are doing. Instead you should use do-while loops like kittens suggested. Here''s one way you might do it with do-while loops:


  
int main ()
{
int age;
int agree;
cout << "What grade are you in? - By Gomeza54" << endl;
cout << endl;

// enter age loop

do
{
cout << "Please enter your age: ";
cin >> age;
// confirmation loop

do
{
cout << "Confirm. You are " << age << " years old? (1 - Yes 2- No) ";
cin >> agree;
cin.ignore();

// check for an invalid response

if( agree != 1 && agree != 2 )
{
cout << "That is an invalid option. Please try again." << endl; // indicate that it is an invalid

}
} while( agree != 1 && agree != 2 );
} while( agree != 1 );

//this is where the age calculation starts

// begin the if statements for the age calculation

// if the age is less 3 or else then print out this and end program

if (age<=3)
{
cout << "You are to young to be in school!" << endl;
//the gotos that were here are useless..

}
// if the age is 4 print out junior kindergarden

else if (age==4)
{
cout << "You are in Junior Kindergarden" << endl;
}
// if the age is 5 print out senior kindergarden

else if (age==5)
{
cout << "You are in Senior Kindergarden" << endl;
}
// if the age is more than 19 or more then print out this and end program

else if (age>=19)
{
cout << "You are too old to be in school!" << endl;
}
// if the age can go to school then perform two math equations to figure out grade

else
{
cout << "You are in Grade " << (age-6) << " or Grade " << (age-5) << "." << endl;
}

// when the program is over, run this

cout << "Press enter. ";
cin.get ();
return 0;
}

Share this post


Link to post
Share on other sites
also, just to point out, you are looking for a way to trap all non numbers, not just consonents. because vowels, puntuation, and mathmatic symbols can cause problems as well.

also instead of gotos, consider function is you feel placing all the cpde in the if statement is too much.

also, someone greater then 19 may be in college.

you may want to look into switch statments as well, since they handle conditions based on constant values (NOT strings, just numbers) much cleaner then an if statment can.

Share this post


Link to post
Share on other sites