Thread: Can't quit my program

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    49

    Question Can't quit my program

    Hi, I have a problem that involve global variables. Basicaly, i start in a while loop when a value is true but i cant make that value false when in another function.
    I have 2 source files and one header file below.
    Could some please tell me where to declare "active" to make this work.

    main.c
    Code:
    #include <stdio.h>
    #include <math.h>
    #include "functions.h"
    
    bool active = true;
    
    int main()
    {
    	while(active)
    	{
    		menu();
    	}
    }
    functions.h
    Code:
    int distance();
    void retry();
    int quit();
    int menu();
    lines.c
    Code:
    #include <stdio.h>
    #include <math.h>
    
    struct point2D
    {
    	float x;
    	float y;
    }pa,pb;
    
    int distance()
    {
    	float d;
    
    	printf("\nPlease enter the first point: ");
    	scanf("%f" "%f", &pa.x, &pa.y);
    	printf("Please enter the second point: ");
    	scanf("%f" "%f", &pb.x, &pb.y);
    
    	d = sqrt(pow(pb.x-pa.x,2)+pow(pb.y-pa.y,2));
    
    	printf("The distance between these points is: %f\n\a",d);
    
    	return(0);
    }
    
    int quit()
    {
    	active = false; //THIS IS THE ERROR!! active: undeclared variable
    	return(0);
    }
    
    int menu()
    {
    	int choice;
    	printf("\n****************Main Menu*******************\n");
    	printf("1.Distance between two points\n");
    	printf("2.Quit\n");
    	printf("********************************************\n");
    	printf("Please enter your choice: ");
    	scanf("%d", &choice);
    
    	switch(choice)
    	{
    	case 1:
    			  distance();
    		break;
    	case 2:
    			  quit();
    		break;
    	default:
    		//retry();
    		break;
    	}
    
    	return(0);
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    In your other source file, you can place:

    Code:
    extern bool active;
    The extern keyword just tells the compiler that the active variable is declared somewhere in another file, but to let this file use it also.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    49
    Thanks bithub
    That is handy
    Cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  2. Press "Enter" key to quit the program
    By Vitamin_C in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2005, 08:25 PM
  3. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM
  4. Error when trying to quit program...
    By marCplusplus in forum C++ Programming
    Replies: 5
    Last Post: 07-08-2002, 07:03 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM