Thread: Call for an exit in C

  1. #1
    Registered User litebread's Avatar
    Join Date
    Dec 2011
    Location
    California
    Posts
    21

    Post Call for an exit in C

    Hello guys this is my second post. I was wondering about this little Easter egg I'm putting in my win32 console app for a C Programming class at my college assignment.

    It's very simple, and by simple I mean the idea is that if the user puts in "Open the bay doors" in the first input line, using strncmp he will get an output of "I'm sorry Dave, I can't do that" (Its from 2001:Space Oddysey. I want to see if the teacher notices.) and closes the app.

    So my only question is, what line of code should I use to display the message after the user inputs the specific line long enough so that the user can read it, and than safely exit the app.

    I'm on win 7
    Using Visual c++ 2008 Express

    Here's the entire app(With strncmp already done, just need the timer and exit call):

    Code:
    #include<stdio.h>#include<stdlib.h>
    #include<string.h>
    /////////////////
    #define SIZE 5
    /////////////////
    struct Stock
    {
    	char name[10];
    	int numShares;
    	float buyShare,currPrice,fees;
    	float initCost,currCost,profit;
    };
    /* Load the data from the keyboard and calculates 
    the Initial Cost, Current Cost, and Profit 
    for each stock via an array */
    
    
    void load(struct Stock s[], int n) 
    {
    	char nameCompare[30] = "Open the bay doors";
    	int i;
    	for(i=0;i<n;i++)
    	{
    		printf("Enter the Stock Name\n");
    		printf(">");
    		gets(s[i].name);
    		/////////////////////////////////////
    		if(strncmp (s[i].name,nameCompare,10) == 0)
    		{
    			printf("\tI'm sorry, Dave, I'm afraid I can't do that\n");
    			printf("\n");
    			exit(1);
    		}
    		/////////////////////////////////////
    		printf("Enter the Number of Shares\n");
    		printf(">");
    		scanf("%d", &s[i].numShares);
    		printf("Enter the Buying Price Per Share\n");
    		printf(">");
    		scanf("%f", &s[i].buyShare);
    		printf("Enter the Current Price Per Share\n");
    		printf(">");
    		scanf("%f", &s[i].currPrice);
    		printf("Enter the Yearly Fees\n");
    		printf(">");
    		scanf("%f", &s[i].fees);
    
    
    		s[i].initCost = (float)s[i].numShares * s[i].buyShare;
    		s[i].currCost = (float)s[i].numShares * s[i].currPrice;
    		s[i].profit = s[i].currCost - s[i].initCost - s[i].fees;
    
    
    		fflush(stdin);
    	}
    }
    
    
    /* Sort the array of structures 
    on stock name and print the array 
    after the sort is completed */
    
    
    void sort(struct Stock s[], int n)
    {
    	Stock t;
    	for(int i=0;i<n-1;i++)
    		for(int j=0;j<n-1;j++)
    			if(strcmp(s[j].name, s[j+1].name)>0)
    			{
    				t=s[j];
    				s[j]=s[j+1];
    				s[j+1]=t;
    			}
    }
    
    
    /* Calculate and print the total profit for all of the stocks. 
    That is, find the sum of the 5 profits for each stock. In 
    addition, find and print out the number of stocks that 
    had a positive profit, the number of stocks that had a 
    negative profit, and the number of stocks that broke 
    even, that is had a profit of $0.00 */
    
    
    void calc(struct Stock s[],int n)
    {
    	float total=0;
    
    
    	int Pos=0;
    	int Neg=0;
    	int Even=0;
    
    
    	for(int i=0;i<n;i++)
    	{
    		total +=s[i].profit;
    		if (s[i].profit>0)
    			++Pos;
    		else
    		if (s[i].profit<0)
    			++Neg;
    		else
    			++Even;
    	}
    	
    	printf("\n");
    	printf("%d of stocks broke Positive\n",Pos);
    	printf("\t%d of stocks broke Negative\n",Neg);
    	printf("\t\t%d of stocks broke Even\n",Even);
    	printf("\n");
    	printf("The Total Trofit is $%f\n", total); //Check output
    	printf("\n");
    }
    //Output of the calc function
    void print(struct Stock s[], int n)
    	{
    		for(int i=0;i<n;i++)
    		{
    			printf("\n");
    			printf("The stock is %s\n", s[i].name);
    			printf("\tWith Initial cost of $%0.2f\n", s[i].initCost);
    			printf("\t\tCurrent cost is $%0.2f\n", s[i].currCost);
    			printf("\t\t\tAnd your Profit is $%0.2f\n", s[i].profit); //Check output
    			printf("\n");
    		}
    	}
    //Save the array of structures to a text file.
    void savetext(struct Stock s[], int n)
    {
    	FILE *f;
    	f = fopen("e:/final.txt", "w"); 
    	int i;
    	for(i=0;i<n;i++)
    	{
    	  fprintf(f,"%s\n",s[i].name);
    	  fprintf(f,"%d  %f  %f  %f  %f  %f  %f\n", &s[i].numShares, &s[i].buyShare, &s[i].currPrice, &s[i].fees, &s[i].initCost, &s[i].currCost, &s[i].profit);
    	}
    	fclose(f);
    }
    //Retrieve and print the text file.
    void loadtext(struct Stock s[], int n)
    {
    	FILE *f;
    	f = fopen("e:/final.txt", "r");
    	int i;
    	for(i=0;i<n;i++)
    	{
    	  fgets(s[i].name, sizeof(s[i].name), f);
    	  fscanf(f, "%d%f%f%f%f%f%f", &s[i].numShares, &s[i].buyShare, &s[i].currPrice, &s[i].fees, &s[i].initCost, &s[i].currCost, &s[i].profit);
    	}
    	fclose(f);
    }
    //Save the array of structures to a binary file.
    void savebin(struct Stock s[], int n)
    {
        FILE *f;
        f = fopen("e:\\final.bin", "wb");
        if (! f)
          { printf("Could not open the file");
             exit(1); }
        fwrite(&s, sizeof(struct Stock), n, f);
        fclose(f);
    }
    //Retrieve and print the binary file.
    void loadbin(struct Stock *s, int n)
    {
        FILE *f;
        f = fopen("e:\\final.bin", "rb");
        if (! f)
          { printf("Could not open the file");
             exit(1); }
        fread(s, sizeof(struct Stock), n, f);
        fclose(f);
    }
    
    
    void main()
    {
    	printf("Hello, Dave\n");
    	Stock s[SIZE];
    	load (s, SIZE);
    	sort (s, SIZE);
    	savetext (s, SIZE);
    	savebin (s, SIZE);
    	print (s, SIZE);
    	calc (s, SIZE);
    	loadtext (s, SIZE);
    	print (s, SIZE);
    	loadbin (s, SIZE);
    	print (s, SIZE);
    	system("PAUSE");
    }
    Last edited by litebread; 12-10-2011 at 03:46 PM.

  2. #2
    Registered User litebread's Avatar
    Join Date
    Dec 2011
    Location
    California
    Posts
    21
    Found the exit call: exit(1);

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by litebread View Post
    Found the exit call: exit(1);
    So the only remaining question is how badly your teacher is going to dock you for this!

    Oh and while were on the topic of exits, your main() function is setup incorrectly.
    The minimum program skeleton in C is this...
    Code:
    int main (void)
      {
    
       // your code here
    
       return 0;
    }
    Windows expects an integer return from any process it launches. Your program will return garbage which may affect the proper operation of batch files or parent processes. Your program may run fine, but it is incorrectly interfaced with the OS.... and VC should be warning you about that.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    And...you've got scanf's all over the place. Why did you suddenly switch to the unsafe and forbidden (by any serious programmer) gets() here?

    Code:
    gets(s[i].name);

  5. #5
    Registered User litebread's Avatar
    Join Date
    Dec 2011
    Location
    California
    Posts
    21
    Quote Originally Posted by rags_to_riches View Post
    And...you've got scanf's all over the place. Why did you suddenly switch to the unsafe and forbidden (by any serious programmer) gets() here?

    Code:
    gets(s[i].name);
    Just the way the teacher thoughts it. He did tell us that the IDE will freak about it, but he said to ignore it.

  6. #6
    Registered User litebread's Avatar
    Join Date
    Dec 2011
    Location
    California
    Posts
    21
    Quote Originally Posted by CommonTater View Post
    So the only remaining question is how badly your teacher is going to dock you for this!

    Oh and while were on the topic of exits, your main() function is setup incorrectly.
    The minimum program skeleton in C is this...
    Code:
    int main (void)
      {
    
       // your code here
    
       return 0;
    }
    Windows expects an integer return from any process it launches. Your program will return garbage which may affect the proper operation of batch files or parent processes. Your program may run fine, but it is incorrectly interfaced with the OS.... and VC should be warning you about that.

    Ummmmmmm never knew that lol. Thanks!

    Actually since we're on the subject of garbage, the program is giving me some messed up output for when it's reading both the .txt and .bin files with the data that was inputted earlier. Could you run it and see if the date comes out weird? Or at least see if there's anything wrong with loadtext, savetext, loadbin, savebin functions?

    Code:
    #include<stdio.h>#include<stdlib.h>
    #include<string.h>
    /////////////////
    #define SIZE 2
    /////////////////
    struct Stock
    {
    	char name[10];
    	int numShares;
    	float buyShare,currPrice,fees;
    	float initCost,currCost,profit;
    };
    /* Load the data from the keyboard and calculates 
    the Initial Cost, Current Cost, and Profit 
    for each stock via an array */
    
    
    void load(struct Stock s[], int n) 
    {
    	char nameCompare[30] = "Open the bay doors";
    	int i;
    	for(i=0;i<n;i++)
    	{
    		printf("Enter the Stock Name\n");
    		printf(">");
    		gets(s[i].name);
    		/////////////////////////////////////
    		if(strncmp (s[i].name,nameCompare,10) == 0)
    		{
    			printf("\tI'm sorry, Dave, I'm afraid I can't do that\n");
    			printf("\n");
    			exit(1);
    		}
    		/////////////////////////////////////
    		printf("Enter the Number of Shares\n");
    		printf(">");
    		scanf("%d", &s[i].numShares);
    		printf("Enter the Buying Price Per Share\n");
    		printf(">");
    		scanf("%f", &s[i].buyShare);
    		printf("Enter the Current Price Per Share\n");
    		printf(">");
    		scanf("%f", &s[i].currPrice);
    		printf("Enter the Yearly Fees\n");
    		printf(">");
    		scanf("%f", &s[i].fees);
    
    
    		s[i].initCost = (float)s[i].numShares * s[i].buyShare;
    		s[i].currCost = (float)s[i].numShares * s[i].currPrice;
    		s[i].profit = s[i].currCost - s[i].initCost - s[i].fees;
    
    
    		fflush(stdin);
    	}
    }
    
    
    /* Sort the array of structures 
    on stock name and print the array 
    after the sort is completed */
    
    
    void sort(struct Stock s[], int n)
    {
    	Stock t;
    	for(int i=0;i<n-1;i++)
    		for(int j=0;j<n-1;j++)
    			if(strcmp(s[j].name, s[j+1].name)>0)
    			{
    				t=s[j];
    				s[j]=s[j+1];
    				s[j+1]=t;
    			}
    }
    
    
    /* Calculate and print the total profit for all of the stocks. 
    That is, find the sum of the 5 profits for each stock. In 
    addition, find and print out the number of stocks that 
    had a positive profit, the number of stocks that had a 
    negative profit, and the number of stocks that broke 
    even, that is had a profit of $0.00 */
    
    
    void calc(struct Stock s[],int n)
    {
    	float total=0;
    
    
    	int Pos=0;
    	int Neg=0;
    	int Even=0;
    
    
    	for(int i=0;i<n;i++)
    	{
    		total +=s[i].profit;
    		if (s[i].profit>0)
    			++Pos;
    		else
    		if (s[i].profit<0)
    			++Neg;
    		else
    			++Even;
    	}
    	
    	printf("\n");
    	printf("%d of stocks broke Positive\n",Pos);
    	printf("\t%d of stocks broke Negative\n",Neg);
    	printf("\t\t%d of stocks broke Even\n",Even);
    	printf("\n");
    	printf("The Total Trofit is $%f\n", total); //Check output
    	printf("\n");
    }
    //Output of the calc function
    void print(struct Stock s[], int n)
    	{
    		for(int i=0;i<n;i++)
    		{
    			printf("\n");
    			printf("The stock is %s\n", s[i].name);
    			printf("\tWith Initial cost of $%0.2f\n", s[i].initCost);
    			printf("\t\tCurrent cost is $%0.2f\n", s[i].currCost);
    			printf("\t\t\tAnd your Profit is $%0.2f\n", s[i].profit); //Check output
    			printf("\n");
    		}
    	}
    //Save the array of structures to a text file.
    void savetext(struct Stock s[], int n)
    {
    	FILE *f;
    	f = fopen("e:/final.txt", "w"); 
    	int i;
    	for(i=0;i<n;i++)
    	{
    	  fprintf(f,"%s\n",s[i].name);
    	  fprintf(f,"%d  %f  %f  %f  %f  %f  %f\n", &s[i].numShares, &s[i].buyShare, &s[i].currPrice, &s[i].fees, &s[i].initCost, &s[i].currCost, &s[i].profit);
    	}
    	fclose(f);
    }
    //Retrieve and print the text file.
    void loadtext(struct Stock s[], int n)
    {
    	FILE *f;
    	f = fopen("e:/final.txt", "r");
    	int i;
    	for(i=0;i<n;i++)
    	{
    	  fgets(s[i].name, sizeof(s[i].name), f);
    	  fscanf(f, "%d%f%f%f%f%f%f", &s[i].numShares, &s[i].buyShare, &s[i].currPrice, &s[i].fees, &s[i].initCost, &s[i].currCost, &s[i].profit);
    	}
    	fclose(f);
    }
    //Save the array of structures to a binary file.
    void savebin(struct Stock s[], int n)
    {
        FILE *f;
        f = fopen("e:/final.bin", "wb");
        if (! f)
          { printf("Could not open the file");
             exit(1); }
        fwrite(&s, sizeof(s[10]), n, f);
        fclose(f);
    }
    //Retrieve and print the binary file.
    void loadbin(struct Stock *s, int n)
    {
        FILE *f;
        f = fopen("e:/final.bin", "rb");
        if (! f)
          { printf("Could not open the file");
             exit(1); }
        fread(&s, sizeof(s[10]), n, f);
        fclose(f);
    }
    
    
    int main (void)
      {
    	printf("Hello, Dave\n");
    	Stock s[SIZE];
    	load (s, SIZE);
    	sort (s, SIZE);
    	savetext (s, SIZE);
    	savebin (s, SIZE);
    	print (s, SIZE);
    	calc (s, SIZE);
    	loadtext (s, SIZE);
    	print (s, SIZE);
    	loadbin (s, SIZE);
    	print (s, SIZE);
    	system("PAUSE");
    	return 0;
    }

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by litebread View Post
    Just the way the teacher thoughts it. He did tell us that the IDE will freak about it, but he said to ignore it.
    Your teacher is an idiot!

    Try this...
    Code:
    #include <stdio.h>
    #incluce <conio.h>
    
    int main (void)
      {
         int x = 0;
         char str[8];
         int y = 0;
    
         gets(str);  // type in at least 20 characters
    
         printf("%d %d \n", x, y);  // oops!
    
         return 0;
    }

  8. #8
    Registered User litebread's Avatar
    Join Date
    Dec 2011
    Location
    California
    Posts
    21
    ..........ED UP OUTPUT:

    Code:
    Hello, DaveEnter the Stock Name
    >Clippers
    Enter the Number of Shares
    >150
    Enter the Buying Price Per Share
    >1.33
    Enter the Current Price Per Share
    >3.33
    Enter the Yearly Fees
    >5.00
    Enter the Stock Name
    >Spurs
    Enter the Number of Shares
    >215
    Enter the Buying Price Per Share
    >15.77
    Enter the Current Price Per Share
    >14.25
    Enter the Yearly Fees
    >3.50
    
    
    The stock is Clippers
            With Initial cost of $199.50
                    Current cost is $499.50
                            And your Profit is $295.00
    
    
    
    
    The stock is Spurs
            With Initial cost of $3390.55
                    Current cost is $3063.75
                            And your Profit is $-330.30
    
    
    
    
    1 of stocks broke Positive
            1 of stocks broke Negative
                    0 of stocks broke Even
    
    
    The Total Trofit is $-35.300049
    
    
    
    
    The stock is Clippers
    
    
            With Initial cost of $0.00
                    Current cost is $-1.#J
                            And your Profit is $-1.#J
    
    
    
    
    The stock is
    
    
            With Initial cost of $3390.55
                    Current cost is $3063.75
                            And your Profit is $-330.30
    
    
    
    
    The stock is Clippers
    
    
            With Initial cost of $0.00
                    Current cost is $-1.#J
                            And your Profit is $-1.#J
    
    
    
    
    The stock is
    
    
            With Initial cost of $3390.55
                    Current cost is $3063.75
                            And your Profit is $-330.30
    
    
    Press any key to continue . . .

  9. #9
    Registered User
    Join Date
    Jul 2011
    Posts
    25
    Quote Originally Posted by litebread View Post
    Just the way the teacher thoughts it. He did tell us that the IDE will freak about it, but he said to ignore it.
    Something I read elsewhere:

    "Define a 10 character buffer and gets() will happily accept the Declaration of Independance as its input."

    It may not be a problem right now, but you're absolutely going to be running into problems in the future. That's really not good that your teacher didn't tell you about alternate ways to do it.

    C Strings - Cprogramming.com

    The tutorial has a fantastic section about this sort of stuff.

  10. #10
    Registered User litebread's Avatar
    Join Date
    Dec 2011
    Location
    California
    Posts
    21
    Quote Originally Posted by CommonTater View Post
    Your teacher is an idiot!

    Try this...
    Code:
    #include <stdio.h>
    #incluce <conio.h>
    
    int main (void)
      {
         int x = 0;
         char str[8];
         int y = 0;
    
         gets(str);  // type in at least 20 characters
    
         printf("%d %d \n", x, y);  // oops!
    
         return 0;
    }
    Umm ok, but how to I apply it to my current Program? I don't need it in the main.....

    To be honest I'm not worried about that right now. I want to get this easter egg in, fix the I/O for the files and turn this ........er in. I still have two essays to write.

  11. #11
    Registered User litebread's Avatar
    Join Date
    Dec 2011
    Location
    California
    Posts
    21
    Quote Originally Posted by Amberlampz View Post
    Something I read elsewhere:

    "Define a 10 character buffer and gets() will happily accept the Declaration of Independance as its input."

    It may not be a problem right now, but you're absolutely going to be running into problems in the future. That's really not good that your teacher didn't tell you about alternate ways to do it.

    C Strings - Cprogramming.com

    The tutorial has a fantastic section about this sort of stuff.
    I'll be sure to check it put, and I will ask him about it on monday. Thanks!

    But my output is still ........ed, + how do I time the exit thingie? (i.e. give enough time for the user to read the output, than close)

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by litebread View Post
    To be honest I'm not worried about that right now. I want to get this easter egg in, fix the I/O for the files and turn this ........er in. I still have two essays to write.
    Ooooh... talk about messed up priorities.
    You should pay attention to the proper functioning of your assignment.
    The Easter Egg is a silly idea and probably will probably get you docked for the assignment.
    ESPECIALLY if you spend your time on childish things and turn in an assignment that doesn't work right.

    Really... think about it.

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    so to answer your question, the function to make your program wait is Sleep() or sleep() depending on your os.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  14. #14
    Registered User litebread's Avatar
    Join Date
    Dec 2011
    Location
    California
    Posts
    21
    Quote Originally Posted by CommonTater View Post
    Ooooh... talk about messed up priorities.
    You should pay attention to the proper functioning of your assignment.
    The Easter Egg is a silly idea and probably will probably get you docked for the assignment.
    ESPECIALLY if you spend your time on childish things and turn in an assignment that doesn't work right.

    Really... think about it.
    My teacher? Ha!

    We're talking about a dude who explains the NULL char by running into a door.
    He'll probably give me extra credit for throwing this in.

    So in any case, I don't see why my output comes out weird. Can you help me?

  15. #15
    Registered User
    Join Date
    Dec 2011
    Posts
    69
    Just a few little tips.
    1. fflush(stdin); is bad. Cprogramming.com FAQ > Why fflush(stdin) is wrong
    2. Use fgets() for getting strings. C Strings - Cprogramming.com

    Good luck with your program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help!! call-by-reference and call-by-value
    By geoffr0 in forum C Programming
    Replies: 14
    Last Post: 04-01-2009, 07:15 PM
  2. C system call and library call
    By Coconut in forum C Programming
    Replies: 6
    Last Post: 08-22-2002, 11:20 AM
  3. System call to call another C file from Existing C File
    By simly01 in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2002, 01:29 PM
  4. Call-by-value Vs. Call-by-reference
    By Wiz_Nil in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2002, 09:06 AM
  5. call by reference and a call by value
    By IceCold in forum C Programming
    Replies: 4
    Last Post: 09-08-2001, 05:06 PM