Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a piece of code were i have used goto, but i need not have to use it. Can anyone suggest me a method to avoid it.
C++
start:
while (std::getline(file, line))
	{
		std::istringstream iss(line);
	
		std::size_t space = line.find(" ", 0);
		if (!isspace(line[0]))
		{
			iss.clear();
		}
		else
		{
			int i;
			for (i = 0; i <= 12; i++)
			{
				if (isalpha(line[i]))
				{
					iss.clear();
					goto start;
				}
			}

			iss >> abc.hdhf;


What I have tried:

I tried by using function but its not working as needed
Posted
Updated 27-Mar-18 5:26am
v2
Comments
Patrice T 15-Dec-17 14:29pm    
Try to post complete code.
phil.o 15-Dec-17 15:17pm    
Please try also to tell us what you are trying to achieve, because some parts of what you showed present some inconsistencies. For example, you test whether !isspace(line[0]); so far so good. But if it is a whitespace (as the semantics of the code seems to indicate), then you test it again whether it is an alphanumeric character; for the first character of line, it can never happen.
Anyway, my point is that you should try to tell us what you want to achieve, and show us the missing part of the code - at least until the closing bracket of the while loop -, so that we could give you a meaningful answer.
[no name] 15-Dec-17 16:07pm    
Alternative:
while (Expression_1)
{
while(Expression_2)
{
// do something here
// break instead of goto
}
}
Member 13475664 16-Dec-17 14:02pm    
i found the solution by comparing length of the string, i am finally able to avoid the unwanted lines.

1 solution

a simple break and a bool should do be enough :-O
C++
bool flagAlpha = true;
for (int i = 0; i <= 12; i++) {
  if (isalpha(line[i]))
  {
	iss.clear();
	flagAlpha = false;
	break;//goto start;
  }
}

if(flagAlpha)
{
	iss >> abc.hdhf;
}
 
Share this answer
 
Comments
Maciej Los 28-Mar-18 2:34am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900