Thread: wanna return to main function after command input, but this exits to windows...help!

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

    wanna return to main function after command input, but this exits to windows...help!

    hi ppl i have a simple question, i'm starting to learn C and i spotted this problem . my program after any command that i input just exits, and it's very annoying to just restart the whole program. i'll put here the source code(it's far from finished but the start and end are there) so that you can easily tell me what to put and where to put exactly:
    #include <stdio.h>
    #include <stdlib.h>


    int main(int argc, char *argv[])
    {

    int x;
    int CErnesto;
    int CZeManel;

    int Andre;
    int Angelo;
    int Diogo;
    int Ernani;
    int Jorge;
    int Rui;
    int Sergio;
    int Tiago;

    int Bruno;
    int Filipa;
    int Filipe;
    int Lara;
    int Mafalda;
    int SofiaE;
    int SofiaP;
    int Susana;

    int EquipaGolfinho;
    int EquipaLeao;

    int Ajuda;


    CErnesto=1;
    CZeManel=2;
    Andre=3;
    Angelo=4;
    Diogo=5;
    Ernani=6;
    Jorge=7;
    Rui=8;
    Sergio=9;
    Tiago=10;
    Bruno=11;
    Filipa=12;
    Filipe=13;
    Lara=14;
    Mafalda=15;
    SofiaE=16;
    SofiaP=17;
    Susana=18;
    EquipaGolfinho=19;
    EquipaLeao=20;
    Ajuda=118;

    printf(" Introduza o numero correspondente a informacao que pretende ou\npressione 118 para ajuda com os numeros correspndentes: ");
    scanf( "%d", &x );
    if (x==Ajuda)
    {
    printf("\nCErnesto=1\nCZeManel=2\nAndre=3\nAngelo= 4\nDiogo=5\nErnani=6\nJorge=7\nRui=8\nSergio=9\nTi ago=10\nBruno=11\nFilipa=12\nFilipe=13\nLara=14\nM afalda=15\nSofiaE=16\nSofiaP=17\nSusana=18\nEquipa Golfinho=19\nEquipaLeao=20\n\n");
    }
    if (x==Diogo)
    {
    printf("Nome completo: Diogo Alexandre Nasceu a: 22/12/1987 \nTotem: Preguica Preguicosa\n");
    system("PAUSE");
    }
    else
    system("PAUSE");
    }

    it's just that, for the moment. please i'd appreciate some help as as far that i know it's very simple, but i just can't reach it....i've tried some switch and loops but i don't know exacly what to put nor where...

    Thanks

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    So you want to continue to let the user input various things. What you want is a while loop. Here is how it works and you can incorporate it into your program.

    Code:
    int num = 0;
    
    while( num != -1 ) // Here is the condition that has to be met
    {
      printf( "Enter a number (-1 quits): " );
      scanf( "%i", &num );
    
      printf( "You put %i", num );
    }
    So you want to continue taking input until a condition is met. This loop will go on as expected while the condition evaluates to TRUE.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    18
    so i should put it in the beginning, like this? :
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(int argc, char *argv[])
      {
    
    int x;
    int CErnesto;
    int CZeManel;
    
    int Andre;
    int Angelo;
    int Diogo;
    int Ernani;
    int Jorge;
    int Rui;
    int Sergio;
    int Tiago;
    
    int Bruno;
    int Filipa;
    int Filipe;
    int Lara;
    int Mafalda;
    int SofiaE;
    int SofiaP;
    int Susana;
    
    int EquipaGolfinho;
    int EquipaLeao;
    
    int Ajuda;
    
    
    CErnesto=1;
    CZeManel=2;
    Andre=3;
    Angelo=4;
    Diogo=5;
    Ernani=6;
    Jorge=7;
    Rui=8;
    Sergio=9;
    Tiago=10;
    Bruno=11;
    Filipa=12;
    Filipe=13;
    Lara=14;
    Mafalda=15;
    SofiaE=16;
    SofiaP=17;
    Susana=18;
    EquipaGolfinho=19;
    EquipaLeao=20;
    Ajuda=118;
    
    int num =0;
    while( num != -1 ) // Here is the condition that has to be met
    {
    
    
       printf("  Introduza o numero correspondente a informacao que pretende ou\npressione 118 para ajuda com os numeros correspndentes: "); 
       scanf( "%d", &x );
    if (x==Ajuda)
      {
       printf("\nCErnesto=1\nCZeManel=2\nAndre=3\nAngelo=4\nDiogo=5\nErnani=6\nJorge=7\nRui=8\nSergio=9\nTiago=10\nBruno=11\nFilipa=12\nFilipe=13\nLara=14\nMafalda=15\nSofiaE=16\nSofiaP=17\nSusana=18\nEquipaGolfinho=19\nEquipaLeao=20\n\n");
      }
    if (x==Diogo)
      {
       printf("Nome completo: Diogo Alexandre Nasceu a: 22/12/1987 \nTotem: Preguica Preguicosa\n");
       system("PAUSE");
      }
    else
       system("PAUSE");
      }
      }
    like this? or did i put it wrong? if wrong can you point exacly where do i have to put it in?

    thanks for help as for now
    Last edited by Slaught; 07-14-2003 at 05:46 PM.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Well a few things. First of all, you need to put a return 0 statement in as the last line in your main function. You specify that it will return an int but you never return anything. Even though it may compile it is incorrect. Also you are checking while( num != -1 ) but when you take input from the user you set it to the variable x. So take out the int num = 0; and change the while loop to while( x != -1 ) and it should be fine. You might want to also inform the user that -1 quits the program.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    18
    forget the last thread, i did some logical experimenting and found the way to do it.
    thanks a lot MrWizard . You were really a great help. BTW, do you know the command to clear screen?

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    This is an implementation dependent discussion.

    Read this and it should help:

    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    18
    Got it already. Thanks man i really appreciated your help, despite my questions being a bit noobish you helped me .

    Really thanks, now i can finish this goddamn program hehe.

  8. #8
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    some advice...

    use enum to declare your names. ie:

    enum colors {black=1, brown, blue, red};

    black would be 1, brown 2, blue 3, and red 4

    It's a bit more complicated, but not much.

  9. #9
    Registered User
    Join Date
    Jul 2003
    Posts
    18
    What's the point in doing that to declare them? i didnt get it...
    BTW, is it possible to, like, put a "kind of password" in the beginning of the program to multi users, giving each user a different pass, using the if and else command in the beginning, like:

    Code:
    int user1, pass1;
    user1=x
    pass1=y
    printf("Input user and pass ");
    scanf( "%d%d", &user1, &pass1);
    if (user1==x)
    continue;
    else
    break;
    if (pass1==y)
    continue;
    else
    break;
    i think this method works, but it is to only one user and pass. What have i to do to ask for more than 1 user and pass, being all users different and so the passes?
    Last edited by Slaught; 07-14-2003 at 05:45 PM.

  10. #10
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Originally posted by Slaught
    What's the point in doing that to declare them? i didnt get it...
    BTW, is it possible to, like, put a "kind of password" in the beginning of the program to multi users, giving each user a different pass, using the if and else command in the beginning, like:

    int user1, pass1;
    user1=x
    pass1=y
    printf("Input user and pass ");
    scanf( "%d%d", &user1, &pass1);
    if (user1==x)
    continue;
    else
    break;
    if (pass1==y)
    continue;
    else
    break;


    i think this method works, but it is to only one user and pass. What have i to do to ask for more than 1 user and pass, being all users different and so the passes?
    use a struct to hold the pw variable, user name, etc. Use the define statement to declare your set value for the password.

    typedef struct data1
    {
    int password;
    char name[20];
    } user;

    user mydata[MAXUSERS];

    loop through asking each of MAXUSERS his or her name and password, but I'd suspect that you'd be better off using a char array for the password than you would an int....
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  11. #11
    Registered User
    Join Date
    Jul 2003
    Posts
    18

    Question

    huh, like, i never worked with that kind of function, so i dont really know how to input users and passes in there for "making the loop"....can u put an example of that, with at least 3 different users and 1 unique pass for each one?

    Thanks!
    Last edited by Slaught; 07-14-2003 at 05:50 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM