Loop Entire Program?

This is a discussion on Loop Entire Program? within the C Programming forums, part of the General Programming Boards category; How do I loop an entire program using an if statement? At the end of my program, it prints a ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    6

    Loop Entire Program?

    How do I loop an entire program using an if statement? At the end of my program, it prints a question, depending on the answer (for example yes or no) I want the entire program to start from the beginning or to simply end. Any help would be appreciated very much!

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    >> How do I loop an entire program using an if statement?

    You don't (ok, technically you could with a goto, but I'm not about to recommend that!).

    >> At the end of my program, it prints a question, depending on the answer (for example yes or no) I want the entire program to start from the beginning or to simply end. Any help would be appreciated very much!

    Post an example piece of code to demonstrate what you're trying to do here.
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  3. #3
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    How do I loop an entire program using an if statement?
    Recursion.

    At the end of my program, it prints a question, depending on the answer (for example yes or no) I want the entire program to start from the beginning or to simply end. Any help would be appreciated very much!
    I don't see how what your trying to do involves looping a program over. Perhaps you meant something else, like skipping to the end of your program based on user input ? it would be better if you posted some code.

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    int main()
    {
    float course, pi, output;
    int start_over;

    printf("enter a course to travel in degrees: ")
    scanf("%f", course);

    // calculations here //
    pi = 3.14
    output = course * (pi/180);

    printf("your course in radians is: %.1f", output);

    printf("would you like to end program or enter another course\n
    enter 1 for another course, or 2 to end program")
    scanf("%d" , & start_over);

    if(start_over==1)
    // want to start program over here;
    else
    // want to end program here;

    }
    return(0);
    Last edited by seanminator; 07-22-2009 at 01:45 AM.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,658
    If you have redundant code that is a clear indicator you need to move it to another function. Follow the rules here, and your code will be clean; easy enough to loop with an actual control structure, like while.

  6. #6
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Well, you could just use a GOTO

    But first -
    Please use code tags.

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    whiteflags, on that link, what is the "return rc;" line about? Is that kinda what I want to do? Thanks

  8. #8
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    How does GOTO work? Im kind of a c programming dumbass...if you couldnt tell....

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,658
    The return statement forwards a value to the function that invoked it. This in turn gives back control to the point after the function call. For example, any simple math formulas you know could be functions that return a variable.

    The important value of this is that the redundant code is kept in one place (the function block) and you can invoke the function again, when necessary. Since you're not going to be bogged down by a wall of text, you can focus on looping the entire main function without affecting the rest of the program flow.

    To literally answer you: Note that like sean said, functions don't "loop" they "recurse," but you need to figure out at least one base case for that technique to work. I'm not recommending this approach for you yet.

    Also, goto is not funny, even as a joke. Forget it.

  10. #10
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    How does GOTO work? Im kind of a c programming dumbass...if you couldnt tell....
    Basically goto statement causes the cpu to jump from one part of the code to another. The place to jump to is defined by a label. Although goto is useful sometimes, it is frowned upon by most people and there usually are other better ways of achieving the same effect. Here is an example -

    Code:
    int main()
    {
    	int x = 3;
    	jump:
    	{
    		x = 4;
    		cout << "Hi" << endl;
    	}
    
    	if(x == 3)
    		goto jump;
    
    	return 0;
    }
    BTW, Goto was present in C as well.

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    >> How does GOTO work? Im kind of a c programming dumbass...if you couldnt tell....

    No!!! This is not the solution. Using goto has a tendency to produce code that is difficult to follow and can have unexpected side effects. ONLY USE THIS AS A LAST RESORT. Period.

    Anyway, you could probably just do something like this (notice the indentation!):

    Code:
    int main()
    {
    
    //-------------------------------------Identifying Variables------------------------------------------------------------//
    	float 
    		pi, 
    		phi, 
    		theta, 
    		theta_rad, 
    		phi_rad, 
    		air_speed, 
    		wind_speed,
    		wind_correction_rad, 
    		wind_correction, 
    		heading_you_should_take,
    		variation, 
    		distance, 
    		total_time, 
    		num_checkpoints, 
    		next_checkpoint, 
    		next_checkpoint_time, 
    		ground_speed,
    		back_actual, 
    		back_ground_speed, 
    		back_wind_correction_A, 
    		back_wind_correction_B, 
    		back_theta,
    		back_theta_rad,
    		back_wind_correction_rad, 
    		heading_you_should_take_back, 
    		back_time, 
    		num_checkpoints_back, 
    		next_checkpoint_back,
    		next_checkpoint_time_back, 
    		total_route_time_to_and_back, 
    		total_distance_to_and_back, 
    		new_theta, 
    		new_phi, 
    		new_wind_speed,
    		new_distance, 
    		new_phi_rad, 
    		new_theta_rad, 
    		new_wind_correction_rad, 
    		new_total_time, 
    		new_wind_correction, 
    		new_ground_speed,
    		new_heading_you_should_take, 
    		new, 
    		home;
    
    	int 
    		i, 
    		j, 
    		new_location;
    	
    	//---------------------------------------Initial Inputs-----------------------------------------------------------------//
    	printf("WELCOME TO SEAN'S E-7B! -its still a work in progress...\n\n");
    	
    	while(true)
    	{
    		printf("\nEnter your desired course in degrees:--------------------> ");
    		scanf("%f" , & theta);
    		
    		printf("Enter your desired air speed in knots:-------------------> ");
    		scanf("%f" , & air_speed);
    		
    		printf("Enter wind angle in degrees:-----------------------------> ");
    		scanf("%f" , & phi);
    		
    		printf("Enter wind speed in knots:-------------------------------> ");
    		scanf("%f" , & wind_speed);
    		
    		printf("Enter the distance you will travel in nautical miles:----> ");
    		scanf("%f" , & distance);
    		
    		printf("Enter the amount of magnetic variation:------------------> ");
    		scanf("%f" , & variation);
    		
    		//---------------------------------------Defining Equations---------------------------------------------------------------//
    		pi=3.1416;
    		
    		phi_rad = phi*(pi/180);
    		theta_rad = theta*(pi/180);
    		
    		wind_correction_rad = asin( sin( phi_rad - theta_rad ) * (wind_speed / air_speed));
    		
    		
    		if (wind_correction_rad >0 && wind_correction_rad < (pi/2) )
    		{
    			wind_correction = wind_correction*1;
    		}
    		else
    			wind_correction = wind_correction*(-1);
    		
    		
    		wind_correction = wind_correction_rad*(180/pi);
    		
    		ground_speed = sqrt( pow(air_speed,2) + pow(wind_speed,2) - (2*air_speed*wind_speed*cos(phi_rad - theta_rad - wind_correction_rad)));
    		
    		heading_you_should_take = variation + theta + wind_correction;
    		
    		total_time = (distance / ground_speed) *60;
    		
    		//----------------------------------------Return Data----------------------------------------------------------------------//
    		printf("\n\nYour wind correction angle is: %.1f ", wind_correction);
    		printf("\nThe course you should take is: %.1f ", heading_you_should_take);
    		printf("\nYour ground speed is: %.1f", ground_speed);
    		printf("\nIt should take you about %.1f minutes to reach your final destination", total_time);
    		
    		//---------------------------------------Loop to Get and Return Checkpoint Data--------------------------------------------//
    		printf("\n\n\nEnter the number of check points you will cross: " );
    		scanf("%f" , & num_checkpoints);
    		
    		printf("\n");
    		
    		for (i=0;
    		i< num_checkpoints;
    		i++)
    		{
    			printf("\nEnter nautical miles to next checkpoint: ");
    			scanf("%f" , & next_checkpoint);
    		
    			next_checkpoint_time = (next_checkpoint / ground_speed) * 60;
    		
    			printf("It will take you %.2f minutes to get to your next checkpoint.\n" , next_checkpoint_time);
    		}
    		
    		//-------------------------------------------Return Home or New course?-----------------------------------------------------//
    		printf("\n\n\nPress 1 to return home, or 2 to go to new location: ");
    		scanf("%d", &new_location);
    		
    		
    		//------------going back equations--------//
    		if (theta > 180)
    			back_theta = theta - 180;
    		else 
    			back_theta = theta + 180;
    		
    		back_actual = back_theta - wind_correction + variation;
    		
    		back_wind_correction_A = wind_correction * (-1);
    		
    		phi_rad = phi*(pi/180);
    		back_theta_rad = back_theta*(pi/180);
    		
    		back_wind_correction_rad = asin( sin( phi_rad - back_theta_rad ) * (wind_speed / air_speed));
    		
    		if (back_wind_correction_rad >0 && back_wind_correction_rad < (pi/2) )
    		{
    			back_wind_correction_B = back_wind_correction_A*1;
    		}
    		else
    			back_wind_correction_B = back_wind_correction_A*(-1);
    		
    		back_ground_speed = sqrt( pow(air_speed,2) + pow(wind_speed,2) - (2*air_speed*wind_speed*cos(phi_rad - back_theta_rad - back_wind_correction_rad)));
    		
    		heading_you_should_take_back = variation + back_theta + back_wind_correction_B;
    		
    		back_time = (distance/back_ground_speed) * 60;
    		
    		total_route_time_to_and_back = total_time + back_time;
    		
    		total_distance_to_and_back = 2 * distance;
    		//---GOING BACK CODE---//
    		if (new_location == 1)
    		//---inputs---//
    		{ 
    			printf("\n\nYour new wind correction angle is: %.1f", back_wind_correction_A);
    			printf("\nYour new course is: %.1f " , heading_you_should_take_back);
    			printf("\nYour new ground speed is: %.1f", back_ground_speed);
    			printf("\nIt should take about %.1f minutes to return.", back_time);
    		
    			printf("\n\n\nEnter the number of check points you will cross: " );
    			scanf("%f" , & num_checkpoints_back);
    		
    			printf("\n");
    		
    			//---loop for checkpoint distances---//
    			for (j=0;
    			j< num_checkpoints_back;
    			j++)
    			{
    				printf("\nEnter nautical miles to next checkpoint: ");
    				scanf("%f" , & next_checkpoint_back);
    				
    				next_checkpoint_time_back = (next_checkpoint_back / back_ground_speed) * 60;
    				
    				printf("It will take you %.2f minutes to get to your next checkpoint.\n" , next_checkpoint_time_back);
    			}
    			
    			//---total time and distance for whole trip---//
    			printf("\n\n\nYour total route should take about %.1f minutes.", total_route_time_to_and_back);
    			printf("\nTotal distance covered is about %.1f miles.", total_distance_to_and_back);
    		}
    		
    		//---NEW LOCATION CODE---//
    		else if (new_location == 2)
    		{
    			printf("You chose a new location.");
    			
    			printf("\n\nEnter the new TRUE heading from current location: ");
    			scanf("%f", new_theta);
    			
    			printf("Enter wind angle in degrees:-----------------------------> ");
    			scanf("%f" , & new_phi);
    			
    			printf("Enter wind speed in knots:-------------------------------> ");
    			scanf("%f" , & new_wind_speed);
    			
    			printf("Enter the distance you will travel in nautical miles:----> ");
    			scanf("%f" , & new_distance);
    			
    			//---equations for new location---//
    			new_phi_rad = new_phi*(pi/180);
    			new_theta_rad = new_theta*(pi/180);
    			
    			new_wind_correction_rad = asin( sin( new_phi_rad - new_theta_rad ) * (new_wind_speed / air_speed));
    			
    			
    			if (new_wind_correction_rad >0 && new_wind_correction_rad < (pi/2) )
    			{
    				new_wind_correction = new_wind_correction*1;
    			}
    			else
    				new_wind_correction = new_wind_correction*(-1);
    			
    			
    			new_wind_correction = new_wind_correction_rad*(180/pi);
    			
    			new_ground_speed = sqrt( pow(air_speed,2) + pow(new_wind_speed,2) - (2*air_speed*new_wind_speed*cos(new_phi_rad - new_theta_rad - new_wind_correction_rad)));
    			
    			new_heading_you_should_take = variation + new_theta + new_wind_correction;
    			
    			new_total_time = (new_distance / new_ground_speed) *60;
    			
    			//-----outputs for new location---//
    			printf("\n\nYour wind correction angle is: %.1f ", new_wind_correction);
    			printf("\nThe course you should take is: %.1f ", new_heading_you_should_take);
    			printf("\nYour ground speed is: %.1f", new_ground_speed);
    			printf("\nIt should take you about %.1f minutes to reach your final destination", new_total_time);
    		
    		}
    		else
    			return(0);
    	}	
    //-------------------------------------------End Program--------------------------------------------------------------------//
    	
    	return(0);
    }
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  12. #12
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    ok, so I simplified it a lot to show what I want, thought it would be easier on everyone

    int main()
    {
    float
    course,
    pi,
    output;

    int
    start_over;

    printf("enter a course to travel in degrees: ")
    scanf("%f", course);

    // calculations here //
    pi = 3.14
    output = course * (pi/180);

    printf("your course in radians is: %.1f", output);

    printf("would you like to end program or enter another course\n
    enter 1 for another course, or 2 to end program")
    scanf("%d" , & start_over);

    if(start_over==1)
    // want to start program over here;
    else
    // want to end program here;

    }
    return(0);
    Last edited by seanminator; 07-22-2009 at 01:53 AM.

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,658
    @Spidey: That's an infinite loop. Stop being cute, dammit. You're not even helping in an obfuscated sense.

  14. #14
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    @Spidey: That's an infinite loop. Stop being cute, dammit. You're not even helping in an obfuscated sense.
    Sorry, I just thought the OP should know.
    Also, how is that an infinite loop ?
    It runs only once on my compiler :S

  15. #15
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    >> ok, so I simplified it a lot to show what I want, thought it would be easier on everyone

    Did you see the while loop I added?
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program using structs to calculate grades
    By TampaTrinDM88 in forum C Programming
    Replies: 4
    Last Post: 07-06-2009, 12:33 PM
  2. Replies: 8
    Last Post: 12-01-2008, 09:09 AM
  3. Wondering about repeating an entire program
    By Chrisab508 in forum C++ Programming
    Replies: 3
    Last Post: 10-16-2003, 10:22 PM
  4. detect infinite loop in c program
    By abhivyakat in forum C Programming
    Replies: 19
    Last Post: 10-01-2003, 06:55 AM
  5. Need Help on Program Using For Loop
    By Unregistered in forum C++ Programming
    Replies: 10
    Last Post: 02-26-2002, 05:54 PM

Tags for this Thread


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