Illegal breaks

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ConfusedAlot
    New Member
    • Mar 2007
    • 17

    Illegal breaks

    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??
    Code:
    void Enter(OpAmps& OpAmp, unsigned long& length)
    // if the database is full, inform the user
    {
    	char line[20];
    	unsigned int Pin;
    	double Slew;
    	bool cont=false;
    
    	if (length == 10)
    	{
    		cerr << "The database is full" << endl;
    	}
    
    // if the database is not full, get the data from the user and alter the database length
    
    	else
    
    	cout << "Enter Operational Amplifier name: ";
    	cin.ignore();
    	while(cin.getline(line, sizeof(line), '\n'));
    	{
    		strcpy_s(OpAmp.Name, line);
    //this is where the break gets added and error is produced.
    	}
    	
    	while (cont==false)
    	{
    		cout << endl << "Enter number of pins: ";
    		cin.getline(line, sizeof(line), '\n');
    
    		if (scanf_s(line,"%u",&Pin), '\n')
    		{
    			OpAmp.PinCount=Pin; 
    			cont=true;
    		}
    		else 
    		{
    			cout << "Try again" << endl; 
    			cont=false;
    		}
    	}
    	cont = false;
    
    	while (cont==false)
    		{
    			cout << endl << "Enter slew rate: ";
    			cin.getline(line, sizeof(line), '\n');
    	if (scanf_s(line,"%f",&Slew))
    	{
    		OpAmp.SlewRate=Slew; 
    		cont=true;
    	}
    	else
    	{
    		cout << "Try again" << endl; 
    		cont=false;
    	}
    	cout << endl;
    	length++;
    
    	cout << "Operator Amplifier in memory, to save select option 2 from main menu" << endl;
    }
    }
    Last edited by sicarie; Mar 15 '07, 01:57 PM. Reason: Added [code] and [/code] tags
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by ConfusedAlot
    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??
    Code:
    void Enter(OpAmps& OpAmp, unsigned long& length)
    // if the database is full, inform the user
    {
    	char line[20];
    	unsigned int Pin;
    	double Slew;
    	bool cont=false;
    
    	if (length == 10)
    	{
    		cerr << "The database is full" << endl;
    	}
    
    // if the database is not full, get the data from the user and alter the database length
    
    	else
    
    	cout << "Enter Operational Amplifier name: ";
    	cin.ignore();
    	while(cin.getline(line, sizeof(line), '\n'));
    	{
    		strcpy_s(OpAmp.Name, line);
    //this is where the break gets added and error is produced.
    	}
    	
    	while (cont==false)
    	{
    		cout << endl << "Enter number of pins: ";
    		cin.getline(line, sizeof(line), '\n');
    
    		if (scanf_s(line,"%u",&Pin), '\n')
    		{
    			OpAmp.PinCount=Pin; 
    			cont=true;
    		}
    		else 
    		{
    			cout << "Try again" << endl; 
    			cont=false;
    		}
    	}
    	cont = false;
    
    	while (cont==false)
    		{
    			cout << endl << "Enter slew rate: ";
    			cin.getline(line, sizeof(line), '\n');
    	if (scanf_s(line,"%f",&Slew))
    	{
    		OpAmp.SlewRate=Slew; 
    		cont=true;
    	}
    	else
    	{
    		cout << "Try again" << endl; 
    		cont=false;
    	}
    	cout << endl;
    	length++;
    
    	cout << "Operator Amplifier in memory, to save select option 2 from main menu" << endl;
    }
    }
    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

    Comment

    • isaacr
      New Member
      • Mar 2007
      • 6

      #3
      Originally posted by Savage
      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?

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

        #4
        Originally posted by isaacr
        isnt that what "else" did?

        No,because the condition for break must be inside a loop in this case it's
        a while loop
        Code:
         	while(cin.getline(line, sizeof(line), '\n'));
        	{
        		strcpy_s(OpAmp.Name, line);
        //Here is where he needes to insert some condition like:
                                        if(something) break;
        	}

        Comment

        • itgendevelopment
          New Member
          • Mar 2007
          • 5

          #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.
          //----------------------------------------------------
          Originally posted by ConfusedAlot
          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??
          Code:
          void Enter(OpAmps& OpAmp, unsigned long& length)
          // if the database is full, inform the user
          {
          	char line[20];
          	unsigned int Pin;
          	double Slew;
          	bool cont=false;
          
          	if (length == 10)
          	{
          		cerr << "The database is full" << endl;
          	}
          
          // if the database is not full, get the data from the user and alter the database length
          
          	else
          
          	cout << "Enter Operational Amplifier name: ";
          	cin.ignore();
          	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.
          
          	{
          		strcpy_s(OpAmp.Name, line);
          //this is where the break gets added and error is produced.
          	}
          	
          	while (cont==false)
          	{
          		cout << endl << "Enter number of pins: ";
          		cin.getline(line, sizeof(line), '\n');
          
          		if (scanf_s(line,"%u",&Pin), '\n')
          		{
          			OpAmp.PinCount=Pin; 
          			cont=true;
          		}
          		else 
          		{
          			cout << "Try again" << endl; 
          			cont=false;
          		}
          	}
          	cont = false;
          
          	while (cont==false)
          		{
          			cout << endl << "Enter slew rate: ";
          			cin.getline(line, sizeof(line), '\n');
          	if (scanf_s(line,"%f",&Slew))
          	{
          		OpAmp.SlewRate=Slew; 
          		cont=true;
          	}
          	else
          	{
          		cout << "Try again" << endl; 
          		cont=false;
          	}
          	cout << endl;
          	length++;
          
          	cout << "Operator Amplifier in memory, to save select option 2 from main menu" << endl;
          }
          }

          Comment

          • itgendevelopment
            New Member
            • Mar 2007
            • 5

            #6
            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.
            //----------------------------------------------------

            Comment

            Working...