Thread: How can I make my app exit...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    14

    Question How can I make my app exit...

    Hey, this is probably a simple thing to do. Basically, i'm doing a project for uni work. it is just a simple currency converter. I have managed to get the application so the data is checked and if it is not numercial, the user is alerted. But I would also like the program to exit when the user types exit.

    Here is what I have so far

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    void welcome(void); //displays welcome message and app info
    void convert(void); //converts users input to $
    int main()
    {
    	int count;
       welcome();
       count=0;
       while (count==0)
          {
    		convert();
          }
    }
    void welcome(void)  //this bit welcomes the user and shows some info
    {
    	printf ("Currency Converter\nBy Kieran Symes\n24/10/2006\n");
    }
    void convert(void)
    {
    	char line[80];
    	double input;  //users input
    	char *endptr;
    	int valid = 0;  //decides whether data is valid, this may not be required...
       double output; //output after calcs
    	do
       {
    		printf("\nPlease enter an amount you wish to convert without the &#163;.\n");
    		fgets(line, 80, stdin);  //get data
    		input = strtod(line, &endptr);
    		if (endptr == line)
    		{
    			printf("\nYou have entered incorrect data.  Please ensure you type numbers only!\n\n");
    			valid = 0;
    		}
    		else
    		{
    			valid = 1;
    		}
    	   } while (!valid);  //data is valid
         	output = input * 1.49;  //do the maths
       	printf("\n\&#163;%.2f is equal to %.2f euros.\n", input, output);  //print output
    }
    I think I can use strcmp but I am unsure how to use this correctly. If anybody would like to post an example with sample code, I would very much appreciate it. Also, where would I put this?

    I am quite new to programming so that is why my code may not be very efficient etc.

    thanks
    Last edited by kie; 11-01-2006 at 04:03 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You should ask yourself, how can you update the count variable if you don't pass it's address to convert? You're stuck in an infinite loop for this reason.

    At a minimum you should do something like
    Code:
    void convert( int *count );
    
    /* ... */
    
    int count = 0;
    welcome();
    while ( count == 0 )
    {
       convert(&count);
    }
    and handle count accordingly for however long you want the program to run.

  3. #3
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Try this:
    Code:
    if (line == "exit") //And yes, this should take uppercase;i'm lazy
    {
         printf("Exiting...");
         return 0;
    }
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Even if his program was working that way, it wouldn't matter because comparing string literals and character arrays with == gives incorrect results: You're actually comparing the pointer to string s == t. And it will always be wrong. This is because string literals are stored in anonymous read only memory which will never point to the same place as a character array.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    14
    hey manutd, I gave that a try and if you type exit, nothing happens, it just proceeds to the Enter an amount to convert part.

    this is the sort of method I was planning on using. so if anybody else can think of getting this method to work, I would appreciate any help

  6. #6
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    What OS are you using?
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > this is the sort of method I was planning on using.
    Then you have to rewrite your loop in main. Try implementing a case-insensitive strcmp() to do the work: break the loop when the comparison is true. And then look here.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    14
    I would like to get around this problem without changing code if I can, I am using Windows XP MCE

  9. #9
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    If you return a number it should kill you app (as main is a function, returning a value ends the function).
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    14
    when i compile, i get the error "convert() can not return a number"

  11. #11
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Ofcourse you are. Replace this:
    Code:
    if (line == "exit") //And yes, this should take uppercase;i'm lazy
    {
         printf("Exiting...");
         return 0;
    }
    with this:
    Code:
    if (line == "exit") //And yes, this should take uppercase;i'm lazy
    {
         printf("Exiting...");
         return;
    }
    And try to understand these simple errors next time.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  12. #12
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    I'm still a bit fuzzy on the whole comparing strings with == thing. . . I really don't think that will work the way you expect it to. . .

    Actually, I know that it will NOT work in C. maxorator, why do you keep using this, especially when citizen in post #4 has already stated that this DOES NOT WORK? Don't you read the entire thread before posting?

    If you are comparing strings you could use something like strcmp() or strncmp(). A simple == is not for char *'s or string literals. If you compare a char * to a string literal, and you have not assigned the string literal to the char *, you will ALWAYS get false.

  13. #13
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Whoops, I copied the code.
    Code:
    if (strcmp(line,"exit")==0) //And yes, this should take uppercase;i'm lazy
    {
         printf("Exiting...");
         return;
    }
    I just meant that he probably had that "return 0;" in where it supposed to be just "return;".
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    14
    it still seems to ignore that and just asks for another value stating that invalid data was entered

  15. #15
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Post your new code.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-21-2006, 07:52 AM
  2. Dynamic array of pointers
    By csisz3r in forum C Programming
    Replies: 8
    Last Post: 09-25-2005, 02:06 PM
  3. socket send() exits app unexceptively
    By Kleid-0 in forum C Programming
    Replies: 9
    Last Post: 07-25-2005, 08:29 AM
  4. What to make?
    By Caldus in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2005, 01:12 PM
  5. pasword app
    By GanglyLamb in forum C Programming
    Replies: 2
    Last Post: 06-07-2003, 10:28 AM