Thread: Noob question ( little dos program )

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    29

    Question Noob question ( little dos program )

    i'm making this little math program that lets you do diffrent things and it has this menu that you press arrow keys to change selection. That all works fine except when after using one of the tools, instead of asking for options again, it just goes to the next option there is and starts it... im a bigginer and i just started 3 days ago so plz dont laugh at me.

    heres the source code for the main program:
    Code:
    /*
    Name: Vortex Calculation tools
    Author: Reo ***** <-- edited out  by Kermi3
    Description: tools involving math
    Date: 01/04/03 21:24
    Copyright: Reo Baird 2003
    Version: 1.2
    
    Version history:
    1.0: 2 things, tax and prime number calc.
    1.1: added odd even and square root things
    1.2: Added Conversions, Changed name.
    */
    //main 
    #include
    #include
    #include "prime.h"
    #include "tax.h"
    #include "type.h"
    #include "square.h"
    #include "types.h"
    #include "consolelib.h"
    
    
    
    void main()
    {
    unsigned int sel = 1;
    void menu(int);
    void fct(int);
    system ("CLS");
    CONSOLE_CURSOR_INFO console = {1,0};
    SetConsoleCursorInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &console );
    menu(sel);
    int key;
    while (key != 27 && key != 13)
    {
    key = getch();
    switch(key)
    {
    case 72:
    if(sel != 1)
    {menu(--sel);}
    break;
    case 80:
    if(sel != 6)
    {menu(++sel);}
    break;
    default:
    break;
    }
    if(key == 13) {fct(sel);system("CLS");break;}
    }
    }
    void menu(int sel)
    {
    switch(sel)
    {
    case 1: //draw menu 1
    cputsxy(" Prime Checker ", 5, 2, BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
    cputsxy(" Tax Calculator ", 5, 3, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cputsxy(" Odd or Even ", 5, 4, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cputsxy(" Square Root Calculator ", 5, 5, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cputsxy(" Conversions ", 5, 6, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cputsxy(" Quit ", 5, 7, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    break;
    case 2: //draw menu 2
    cputsxy(" Prime Checker ", 5, 2, FOREGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
    cputsxy(" Tax Calculator ", 5, 3, BACKGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cputsxy(" Odd or Even ", 5, 4, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cputsxy(" Square Root Calculator ", 5, 5, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cputsxy(" Conversions ", 5, 6, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cputsxy(" Quit ", 5, 7, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    break;
    case 3: //draw menu 3
    cputsxy(" Prime Checker ", 5, 2, FOREGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
    cputsxy(" Tax Calculator ", 5, 3, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cputsxy(" Odd or Even ", 5, 4, BACKGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cputsxy(" Square Root Calculator ", 5, 5, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cputsxy(" Conversions ", 5, 6, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cputsxy(" Quit ", 5, 7, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    break;
    case 4: //draw menu 4
    cputsxy(" Prime Checker ", 5, 2, FOREGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
    cputsxy(" Tax Calculator ", 5, 3, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cputsxy(" Odd or Even ", 5, 4, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cputsxy(" Square Root Calculator ", 5, 5, BACKGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cputsxy(" Conversions ", 5, 6, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cputsxy(" Quit ", 5, 7, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    break;
    case 5: //draw menu 4
    cputsxy(" Prime Checker ", 5, 2, FOREGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
    cputsxy(" Tax Calculator ", 5, 3, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cputsxy(" Odd or Even ", 5, 4, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cputsxy(" Square Root Calculator ", 5, 5, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cputsxy(" Conversions ", 5, 6, BACKGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cputsxy(" Quit ", 5, 7, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    break;
    case 6: //draw menu 4
    cputsxy(" Prime Checker ", 5, 2, FOREGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
    cputsxy(" Tax Calculator ", 5, 3, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cputsxy(" Odd or Even ", 5, 4, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cputsxy(" Square Root Calculator ", 5, 5, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cputsxy(" Conversions ", 5, 6, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cputsxy(" Quit ", 5, 7, BACKGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    break;
    }
    }
    void fct(int sel)
    {
    switch(sel)
    {
    case 1:
    //selc1
    system("CLS");
    cout << "\n\n\n\n\n\n\n\n\n";
    prime_numbers(); 
    case 2:
    system("CLS");
    cout << "\n\n\n\n\n\n\n\n\n";
    tax(); 
    case 3:
    system("CLS");
    cout << "\n\n\n\n\n\n\n\n\n";
    oddeven(); 
    case 4:
    system("CLS");
    cout << "\n\n\n\n\n\n\n\n\n";
    square(); 
    case 5:
    system("CLS");
    cout << "\n\n\n\n\n\n\n\n\n";
    num(); 
    case 6:
    break;
    default:
    //debug selection
    break;
    }
    }


    ---------------

    and heres the source for 1 of the options (the 1 that when u press 0 and enter it should go back to menu, but right now goes to the next option instead)

    ---------------
    Code:
    // To find any prime number
    #include
    #include
    #include "consolelib.h"
    void prime(int i, int c, int n, int j) /* This function can be used to find prime numbers to any extent ( first 10 here) by simply changing the while statement */ 
    {
    while (i !=0)
    
    {
    n=0;
    cout << "Please enter a integer (0 to exit): ";
    cin >> i ; 
    cout << "Calculating, Please wait... \n";
    cout << "(This could take several minutes on larger numbers)\n";
    for (j=1 ; j<=i ; j++ )
    {
    if ( i%j == 0)
    {
    n +=1; 
    }
    }
    if (n <=2 && n != 0)
    {
    cout< c +=1;
    }
    else
    {
    cout < }
    }
    }
    
    void prime_numbers()
    {
    int i=1,c=1,n,j;
    prime(i,c,n,j);
    cout<<"\n\nPress ENTER to return to main";
    getch();
    system("CLS");
    }
    PLEASE HELP!! i really want to finish my program and this is really annoying.

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    73
    You should probably get rid of those unused #inlude statements (the one with no file to open after them). Also it's not really good practice to "void main", even though main technically doesn't have to return a value.

    The problem might be the fact your functions are also of type void, and it looks like your passing arguments to them. (which usually equals some type of return value). Try changing it to "int" and using "return 0;".

    Dunno if that will help, that's a lot of coding you posted up to look through.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    29
    Note about includes: the first one is actually iostream and sencond is conio but it thinks its html becuse of the brackets

    i changed it all to int but i still have the same problem

    and where do u mean to add return 0? :S

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    if you are truly not returning a value, then don't return an int. return void. i haven't looked over your code yet, but if there is no need to return something then don't. even if you pass parameters doesn't mean you need to return something. you can return void and still change a variables value through the use of reference parameters and pointers when passing as a parameter.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    29
    i dont have a clue what that means... i just started 3 days ago and im really confused... i read some tutorials but i only remember half of it... most of that code is just code i saw that i liked and then edited... so i really dont have a clue what exactly your saying i should do

  6. #6
    Code:
    for (j=1 ; j<=i ; j++ )
    {
    if ( i%j == 0)
    {
    n +=1; 
    }
    }
    if (n <=2 && n != 0)
    {
    cout< c +=1;
    }
    else
    {
    cout < }
    }
    Code:
    for (j = 1; j <= i; j++)
    {
    	if ( i % j == 0)
    		n += 1; 
    }                          
    if (n <= 2 && n != 0)
    	cout << c += 1;
    else
    	cout << "}";    //didnt know what you were doing here
    Maybe that will help a little, oh and if you just started 3 days ago don't get all wrapped up in using colors.

    EDIT: If you don't want your switch statement to fall through you might want to add a break statement in your cases.
    Last edited by Munkey01; 04-03-2003 at 06:07 AM.

  7. #7
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Posts
    361
    This might just be me, but if you've just started three days ago, should you really be using things like CONSOLE_CURSOR_INFO
    Stan The Man. Beatles fan

    When I was a child,
    I spoke as a child,
    I thought as a child,
    I reasoned as a child.
    When I became a man,
    I put childish ways behind me"
    (the holy bible, Paul, in his first letter to the Cor. 13:11)

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    29
    actually i downloaded that first code off source code planet and just edited the options and what happens when you click them... i still havnt figured out how to fix my problem though

    maybe i didnt make enough sense explaining it (yesterday i had no sleep so i wasnt really thinking that well) ill say it again:
    this is what happens:
    1. i use arrow keys to select first option and then i hit enter
    2. it works and sends me to the prime number script
    3. i use prime number script and everything works fine
    4. i type 0 and then enter, it works perfectly and says hit enter to return to main
    5. i hit enter
    6. instead of going back to the menu that allows me to select stuff with arrow keys, it goes straight to the next option (which happens to be tax()
    7. the next function works perfectly but after using it it does the same thing again and goes to the next function after that
    8. after going through all the functions it does the last 1 and exits

    so bascily i want to know how i make it go back to the menu rather then to the next function


    thanks!

    oh yeh and thanks munkey01 for that fix

  9. #9
    Origionaly posted by Munkey01

    EDIT: If you don't want your switch statement to fall through you might want to add a break statement in your cases.
    Try do that and see if it works. And if you do not understand what I am refering to-
    Code:
    case 1:
    //selc1
    system("CLS");
    cout << "\n\n\n\n\n\n\n\n\n";
    prime_numbers(); 
    break;   //I added this line it will keep your code from falling through
    Add the break statement at the end of the other cases.


    And-
    Code:
    default:
            break;
    Wouldn't you agree that this is unnessicary?

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    29
    Ok i edited it to have break After every option. But now when you click an option and then exit from the option, instead of going to the next option like it did before, it just quits.

    heres what i made the switch look like:
    Code:
                	switch(sel)
                	{
                	case 1:
                	     //selc1
                	     system("CLS");
                         cout << "\n\n\n\n\n\n\n\n\n";
                         prime_numbers(); 
                         break;
                	case 2:
                	     system("CLS");
                         cout << "\n\n\n\n\n\n\n\n\n";
                         tax();
                         break; 
                	case 3:
                	     system("CLS");
                         cout << "\n\n\n\n\n\n\n\n\n";
                         oddeven();
                         break; 
                	case 4:
                	     system("CLS");
                         cout << "\n\n\n\n\n\n\n\n\n";
                         square();
                         break; 
                	case 5:
                	     system("CLS");
                         cout << "\n\n\n\n\n\n\n\n\n";
                         num();
                         break; 
                	case 6:
                	     break;
                	}

  11. #11
    What exactly do you want to do? It to return to the menu again? If so then just call menu() after your switch.

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    29
    THANKS!! IT WORKED!!

    1 thing though... now my exit thing doesnt work properly, is there a command called like exit or somthing i could use?

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    29
    ok never mind i figured out how to make it work!
    Code:
                	switch(sel)
                	{
                	case 1:
                	     //selc1
                	     system("CLS");
                         cout << "\n\n\n\n\n\n\n\n\n";
                         prime_numbers(); 
                         main();
                         break; 
                	case 2:
                	     system("CLS");
                         cout << "\n\n\n\n\n\n\n\n\n";
                         tax();
                         main();
                         break; 
                	case 3:
                	     system("CLS");
                         cout << "\n\n\n\n\n\n\n\n\n";
                         oddeven();
                         main();
                         break;  
                	case 4:
                	     system("CLS");
                         cout << "\n\n\n\n\n\n\n\n\n";
                         square();
                         main();
                         break; 
                	case 5:
                	     system("CLS");
                         cout << "\n\n\n\n\n\n\n\n\n";
                         num();
                         main();
                         break; 
                	case 6:
                	     exit;
                	}
    so everything works fine now.. thanks munkey1 for all your help!

    som1 plz tell me though if thats a bad way of doing it

  14. #14
    It is more commonly seen as "exit(0)" or "exit 0" but they are all the same.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick noob question
    By thanatos1 in forum C# Programming
    Replies: 2
    Last Post: 06-17-2009, 08:28 PM
  2. program crashes + fscanf() question
    By happyclown in forum C Programming
    Replies: 27
    Last Post: 01-16-2009, 03:51 PM
  3. Replies: 26
    Last Post: 06-15-2005, 02:38 PM
  4. Question about K&R program
    By Aerie in forum C Programming
    Replies: 15
    Last Post: 04-24-2005, 07:09 AM
  5. using spawnl for a dos program
    By ronin in forum C Programming
    Replies: 6
    Last Post: 06-30-2002, 09:34 PM