By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,053 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

Illegal breaks

P: 17
I have this function within my program, it allows the user to enter things into a database, the only problem is, when i use the program i cannot get out of the enter operational amplifier stage, when i add a break into the code i get an error of illegal break, how do i fix this??
Expand|Select|Wrap|Line Numbers
  1. void Enter(OpAmps& OpAmp, unsigned long& length)
  2. // if the database is full, inform the user
  3. {
  4.     char line[20];
  5.     unsigned int Pin;
  6.     double Slew;
  7.     bool cont=false;
  8.  
  9.     if (length == 10)
  10.     {
  11.         cerr << "The database is full" << endl;
  12.     }
  13.  
  14. // if the database is not full, get the data from the user and alter the database length
  15.  
  16.     else
  17.  
  18.     cout << "Enter Operational Amplifier name: ";
  19.     cin.ignore();
  20.     while(cin.getline(line, sizeof(line), '\n'));
  21.     {
  22.         strcpy_s(OpAmp.Name, line);
  23. //this is where the break gets added and error is produced.
  24.     }
  25.  
  26.     while (cont==false)
  27.     {
  28.         cout << endl << "Enter number of pins: ";
  29.         cin.getline(line, sizeof(line), '\n');
  30.  
  31.         if (scanf_s(line,"%u",&Pin), '\n')
  32.         {
  33.             OpAmp.PinCount=Pin; 
  34.             cont=true;
  35.         }
  36.         else 
  37.         {
  38.             cout << "Try again" << endl; 
  39.             cont=false;
  40.         }
  41.     }
  42.     cont = false;
  43.  
  44.     while (cont==false)
  45.         {
  46.             cout << endl << "Enter slew rate: ";
  47.             cin.getline(line, sizeof(line), '\n');
  48.     if (scanf_s(line,"%f",&Slew))
  49.     {
  50.         OpAmp.SlewRate=Slew; 
  51.         cont=true;
  52.     }
  53.     else
  54.     {
  55.         cout << "Try again" << endl; 
  56.         cont=false;
  57.     }
  58.     cout << endl;
  59.     length++;
  60.  
  61.     cout << "Operator Amplifier in memory, to save select option 2 from main menu" << endl;
  62. }
  63. }
Mar 15 '07 #1
Share this Question
Share on Google+
5 Replies


Savage
Expert 100+
P: 1,764
I have this function within my program, it allows the user to enter things into a database, the only problem is, when i use the program i cannot get out of the enter operational amplifier stage, when i add a break into the code i get an error of illegal break, how do i fix this??
Expand|Select|Wrap|Line Numbers
  1. void Enter(OpAmps& OpAmp, unsigned long& length)
  2. // if the database is full, inform the user
  3. {
  4.     char line[20];
  5.     unsigned int Pin;
  6.     double Slew;
  7.     bool cont=false;
  8.  
  9.     if (length == 10)
  10.     {
  11.         cerr << "The database is full" << endl;
  12.     }
  13.  
  14. // if the database is not full, get the data from the user and alter the database length
  15.  
  16.     else
  17.  
  18.     cout << "Enter Operational Amplifier name: ";
  19.     cin.ignore();
  20.     while(cin.getline(line, sizeof(line), '\n'));
  21.     {
  22.         strcpy_s(OpAmp.Name, line);
  23. //this is where the break gets added and error is produced.
  24.     }
  25.  
  26.     while (cont==false)
  27.     {
  28.         cout << endl << "Enter number of pins: ";
  29.         cin.getline(line, sizeof(line), '\n');
  30.  
  31.         if (scanf_s(line,"%u",&Pin), '\n')
  32.         {
  33.             OpAmp.PinCount=Pin; 
  34.             cont=true;
  35.         }
  36.         else 
  37.         {
  38.             cout << "Try again" << endl; 
  39.             cont=false;
  40.         }
  41.     }
  42.     cont = false;
  43.  
  44.     while (cont==false)
  45.         {
  46.             cout << endl << "Enter slew rate: ";
  47.             cin.getline(line, sizeof(line), '\n');
  48.     if (scanf_s(line,"%f",&Slew))
  49.     {
  50.         OpAmp.SlewRate=Slew; 
  51.         cont=true;
  52.     }
  53.     else
  54.     {
  55.         cout << "Try again" << endl; 
  56.         cont=false;
  57.     }
  58.     cout << endl;
  59.     length++;
  60.  
  61.     cout << "Operator Amplifier in memory, to save select option 2 from main menu" << endl;
  62. }
  63. }
I think that your break is illegal because you don't have what to break.The while
loop is loop that executes only if condition is true.What you need is to add within a loop another condition (for example if(something) break; ) so that your
break become legal.

Savage
Mar 15 '07 #2

P: 7
I think that your break is illegal because you don't have what to break.The while
loop is loop that executes only if condition is true.What you need is to add within a loop another condition (for example if(something) break; ) so that your
break become legal.

Savage
isnt that what "else" did?
Mar 15 '07 #3

Savage
Expert 100+
P: 1,764
isnt that what "else" did?

No,because the condition for break must be inside a loop in this case it's
a while loop
Expand|Select|Wrap|Line Numbers
  1.      while(cin.getline(line, sizeof(line), '\n'));
  2.     {
  3.         strcpy_s(OpAmp.Name, line);
  4. //Here is where he needes to insert some condition like:
  5.                                 if(something) break;
  6.     }
Mar 15 '07 #4

P: 5
Hi there!

I've been looking at your code. Take a look at the comment I wrote in the quoted message below.

I think that may be helpful.

Kind regards,

Fabiano.
//----------------------------------------------------
I have this function within my program, it allows the user to enter things into a database, the only problem is, when i use the program i cannot get out of the enter operational amplifier stage, when i add a break into the code i get an error of illegal break, how do i fix this??
Expand|Select|Wrap|Line Numbers
  1. void Enter(OpAmps& OpAmp, unsigned long& length)
  2. // if the database is full, inform the user
  3. {
  4.     char line[20];
  5.     unsigned int Pin;
  6.     double Slew;
  7.     bool cont=false;
  8.  
  9.     if (length == 10)
  10.     {
  11.         cerr << "The database is full" << endl;
  12.     }
  13.  
  14. // if the database is not full, get the data from the user and alter the database length
  15.  
  16.     else
  17.  
  18.     cout << "Enter Operational Amplifier name: ";
  19.     cin.ignore();
  20.     while(cin.getline(line, sizeof(line), '\n')); // <<<<<<<<<<<<<< Try removing the semi-colon from the end of the line, as it will prevent the code inside the  '{ }' before the WHILE loop finishes.  Not sure that will solve the problem as this is only a portion of the code.
  21.  
  22.     {
  23.         strcpy_s(OpAmp.Name, line);
  24. //this is where the break gets added and error is produced.
  25.     }
  26.  
  27.     while (cont==false)
  28.     {
  29.         cout << endl << "Enter number of pins: ";
  30.         cin.getline(line, sizeof(line), '\n');
  31.  
  32.         if (scanf_s(line,"%u",&Pin), '\n')
  33.         {
  34.             OpAmp.PinCount=Pin; 
  35.             cont=true;
  36.         }
  37.         else 
  38.         {
  39.             cout << "Try again" << endl; 
  40.             cont=false;
  41.         }
  42.     }
  43.     cont = false;
  44.  
  45.     while (cont==false)
  46.         {
  47.             cout << endl << "Enter slew rate: ";
  48.             cin.getline(line, sizeof(line), '\n');
  49.     if (scanf_s(line,"%f",&Slew))
  50.     {
  51.         OpAmp.SlewRate=Slew; 
  52.         cont=true;
  53.     }
  54.     else
  55.     {
  56.         cout << "Try again" << endl; 
  57.         cont=false;
  58.     }
  59.     cout << endl;
  60.     length++;
  61.  
  62.     cout << "Operator Amplifier in memory, to save select option 2 from main menu" << endl;
  63. }
  64. }
Mar 15 '07 #5

P: 5
Err. Sorry for double posting, just wanted to add some comments for your future reference.

Keep in mind that syntax errors in C, C++, J++ under structured programming paradigm are usually related to prior code lines and you may become very successful while trying to debug your codes.

Kind regards,

Fabiano Antunes.
//----------------------------------------------------
Mar 15 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.