6 Replies - 263 Views - Last Post: 26 December 2012 - 10:57 AM Rate Topic: -----

#1 Yaramohamed1  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 10
  • Joined: 25-December 12

what's the command to make my program close its self?

Posted 26 December 2012 - 10:32 AM

what's the command to make my program close its self?

I have tried the usual, end, close quit etc to no avail.

Thanks in advance
Is This A Good Question/Topic? 0
  • +

Replies To: what's the command to make my program close its self?

#2 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5191
  • View blog
  • Posts: 26,897
  • Joined: 10-May 07

Re: what's the command to make my program close its self?

Posted 26 December 2012 - 10:34 AM

The purpose of the return statement is to tell the calling function (usually the OS) if your program completed & will close successful, or with error.

With no example & no code, we can only guess what the issue may be.

Can you provide us with what ever error you are having, & your example code that you currently have in your program?
Was This Post Helpful? 0
  • +
  • -

#3 Yaramohamed1  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 10
  • Joined: 25-December 12

Re: what's the command to make my program close its self?

Posted 26 December 2012 - 10:42 AM

#include <stdio.h>
#include <stdlib.h>
#define SIZE 100
static int i=0;
struct Contact
{
    char First[SIZE];
    char Last[SIZE];
    char Address[SIZE];
    char City[SIZE];
    char Phone[SIZE];
};
struct Contact Entry[SIZE];
int main()
{
   Welcome();
    return 0;
}
void Welcome()
{
    int operation;

    printf(" 1)Load text file. \n 2)Search for a contact. \n 3)Add new contact. \n 4)Delete a contact. \n 5)Modify a contact \n 6)Print the contact list. \n 7)Save. \n 8)Quit. \n");
    printf(" \n Enter the no of operation you want to do : \n");
    scanf("%d",&operation);
    while(operation<=8)
    {
        switch(operation)
    {
    case 1:
        Load();
        break;
    case 2:
        Search();
        break;
    case 3:
        Add();
        break;
    case 6:
       Print1();
       break;
    case 8:
         exit(0);


        break;
    }
      printf(" \n Enter the no of operation you want to do : \n");
    scanf("%d",&operation);
    }



}
void Load()
{
    char path[SIZE];
    printf("Enter the path of the file : \n");
    scanf("%s",path);
    FILE *open;
    open = fopen(path,"r");
    if (open != NULL)
{
printf("File is found !\n");
while(!(feof(open)))
{
fscanf(open,"%[^,],%[^,],%[^,],%[^,],%[^,]",Entry[i].First,Entry[i].Last,Entry[i].Address,Entry[i].City,Entry[i].Phone);

i++;

}
}
else
{
    printf("File is not found \n ");
}
fclose(open);

}
void Print1()
{
    int j;
    for(j=0;j<i;j++)
    {
        printf("%s , %s , %s , %s , %s \n ",Entry[j].First,Entry[j].Last,Entry[j].Address,Entry[j].City,Entry[j].Phone);
    }
}
void Add()
{
    printf("Enter the information of the contact u want to add : \n");
    printf("First Name :  \n");
    scanf("%s",Entry[i].First);
    printf("Last Name :  \n");
    scanf("%s",Entry[i].Last);
    printf(" Address :  \n");
    scanf("%s",Entry[i].Address);
    printf("City :  \n");
    scanf("%s",Entry[i].City);
    printf("Phone :  \n");
    scanf("%s",Entry[i].Phone);
    printf("Contact is added !!");
    i++;
}
void Search()
{
    char lastname[21];
    int y;
    printf("Enter the last name to search for a contact ");
    scanf("%s",lastname);
    for(y=0;y<i;y++)
    {
   int c = 0;

   while( lastname[c] == Entry[y].Last[c] )
   {
      if(lastname[c] == '\0' || Entry[y].Last[c] == '\0' )
         break;
      c++;
   }
   if( lastname[c] == '\0' && Entry[y].Last[c] == '\0' )
      printf("%s ,%s ,%s , %s , %s",Entry[y].First,Entry[y].Last,Entry[y].Address,Entry[y].City,Entry[y].Phone);
   else
     printf("Not found ");
}
}



Here is my code ... I want the program to close when the user enter 8.
But i am not able to find the right code to do so,can u help me ?
Thanks in advance
Was This Post Helpful? 0
  • +
  • -

#4 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 651
  • View blog
  • Posts: 2,231
  • Joined: 31-December 10

Re: what's the command to make my program close its self?

Posted 26 December 2012 - 10:44 AM

On line 43, you're already using one of the "exit" functions.
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5191
  • View blog
  • Posts: 26,897
  • Joined: 10-May 07

Re: what's the command to make my program close its self?

Posted 26 December 2012 - 10:45 AM

This is not an issue with a 'command', it is a matter of logic.
I would change this :

while(operation<=8)

To this :

while(operation!=8)


Then on case 8 just break. Your while statement will then break, & return to main, where it will return 0 to the OS.

Hrm, why are you asking for the operation before & after you test for it?
Was This Post Helpful? 2
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,322
  • Joined: 25-December 09

Re: what's the command to make my program close its self?

Posted 26 December 2012 - 10:48 AM

What exactly is wrong with the code you provided. The exit() function should do exactly what you want.

Where are your function prototypes for the functions you are trying to use?

You should also stop using all the global variables and learn to properly pass values to and from your function.


Jim
Was This Post Helpful? 1
  • +
  • -

#7 Yaramohamed1  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 10
  • Joined: 25-December 12

Re: what's the command to make my program close its self?

Posted 26 December 2012 - 10:57 AM

Thank for everyone help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1