goto again...

This is a discussion on goto again... within the C++ Programming forums, part of the General Programming Boards category; I know you guys hate goto statements, but would you consider this a good use of a goto statment: Code: ...

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    goto again...

    I know you guys hate goto statements, but would you consider this a good use of a goto statment:

    Code:
    #include <iostream>
    using namespace std;
    
    int search(long id_nums[], int size, long value);
    
    int main()
    {
    	long id_nums[] = {5658845, 8080152, 1005231,
    		              4520125, 4562555, 6545231,
    					  7895122, 5552012, 3852085,
    					  8777541, 5050552, 7576651,
    					  8451277, 7825877, 7881200,
    					  1302850, 1250255, 4581002,};
    
    	long number;
    	int  result;
    
    	      cout << "Enter a number" << endl;
    RESTART:  cin  >> number;
    
    	result = search(id_nums, 18, number);
    
    	if (result == -1)
    	{
    		    cout << "Invalid number. Enter a valid number" << endl;
    			goto RESTART;
    	}
    		else
    			cout << "Your number is valid"  << endl;
    
    	return 0;
    }
    
    int search(long id_nums[], int size, long value)
    {
    	int i        = 0;
    	int position = -1;
    	int found    = 0;
    
    	while (i < size && !found)
    	{
    		if (id_nums[i++] == value)
    		{
    			found    = 1;
    			position = i;
    		}
    	}
    
    	return position;
    }

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    nope. Thats how microsoft think goto should be used in their code samples. The correct tool for what you are trying to do is either a while loop or a do/while loop. You dont need a goto to do that just slightly better structure.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    60

    do while

    the part after

    int result;

    could be written like this:

    do{
    cin >> number;
    result=search(id_nums,18,number);
    if(result==-1)
    cout << "Invalid number ..." << endl;
    else
    cout << "Your number is valid" << endl;
    }while(result==-1);

    For your program it doesn't matter whether you use goto or a loop. But if you write a program that would normally contain dozens of loops, and use goto instead, you will waste much time on searching all your labels.
    Code with to many gotos is also known to cause endless loops easier. (Though, your computer doesn't know anything else than goto. Every 'while', 'for', 'do', and even every function call is a 'goto' [jump] to your computer)

  4. #4
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Code:
    while ((search(id_nums, 18, (cin >> number)) == -1
        cout << "Invalid number. Enter a valid number" << endl;
    do
    cout << "Your number is valid"  << endl;
    return 0;
    I'm not sure if I did that right...the part with the cin >> number; is a bit questionable, but it's something like that.

    Edit: Somebody beat me, and their code will work for sure Use that
    Last edited by confuted; 07-20-2003 at 04:57 PM.
    Away.

  5. #5
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    i sometimes use goto, but only in rare cases. gotos are mostly annoying when theyre used like 5 billion times and they start to spider web all over the place

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  2. goto command
    By jhwebster1 in forum C Programming
    Replies: 3
    Last Post: 02-21-2006, 11:32 AM
  3. Does goto have a glitch or...?
    By Blackroot in forum C++ Programming
    Replies: 9
    Last Post: 02-18-2006, 09:40 AM
  4. helpppp
    By The Brain in forum C Programming
    Replies: 1
    Last Post: 07-27-2005, 07:05 PM
  5. Need some help with a basic tic tac toe game
    By darkshadow in forum C Programming
    Replies: 1
    Last Post: 05-12-2002, 04:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21