I need to use 0 to quit a program

This is a discussion on I need to use 0 to quit a program within the C Programming forums, part of the General Programming Boards category; I am trying to finish an array and have every thing done except the program has a requirement to end ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    1

    Question I need to use 0 to quit a program

    I am trying to finish an array and have every thing done except the program has a requirement to end if the number 0 is typed in.
    See program below and advise:

    #include <stdio.h>
    int main()
    {
    int idpur [4], sold [4], left [4], i, id, sold1, sold2, sold3, sold4, left1, left2, left3, left4;
    printf("Welcome to the Pop4u store!\n");
    printf("Please enter the initial number of cases of Coca-Cola: \n");
    scanf("%d", &idpur [0]);
    printf("Please enter the initial number of cases of Pepsi: \n");
    scanf("%d", &idpur [1]);
    printf("Please enter the initial number of cases of Dr. Pepper: \n");
    scanf("%d", &idpur [2]);
    printf("Please enter the initial number of cases of Canada Dry: \n");
    scanf("%d", &idpur [3]);

    for (i = 1; i > 0; ++i)
    {
    printf("Please enter the ID number: \n");
    printf("1 = Coca-Cola\n");
    printf("2 = Pepsi\n");
    printf("3 = Dr. Pepper\n");
    printf("4 = Canada Dry\n");
    printf("0 = Quit\n");
    scanf("%d", &id);
    if (id == 1)
    {
    printf("Please enter the qunatity to sell: \n");
    scanf("%d", &sold1);
    {
    if (sold1 > idpur [i])
    printf("Not sold! There are only %d cases available.\n", idpur [i]);
    else
    printf("%d cases are sold and %d cases are left.\n", sold1, left1 = idpur[i] - sold1);
    idpur [i] = left1;
    }
    }
    else if (id == 2)
    {
    printf("Please enter the qunatity to sell: \n");
    scanf("%d", &sold2);
    {
    if (sold2 > idpur [i])
    printf("Not sold! There are only %d cases available.\n", idpur [i]);
    else
    printf("%d cases are sold and %d cases are left.\n", sold2, left2 = idpur[i] - sold2);
    idpur [i] = left2;
    }
    }
    else if (id == 3)
    {
    printf("Please enter the qunatity to sell: \n");
    scanf("%d", &sold3);
    {
    if (sold3 > idpur [i])
    printf("Not sold! There are only %d cases available.\n", idpur [i]);
    else
    printf("%d cases are sold and %d cases are left.\n", sold3, left3 = idpur[i] - sold3);
    idpur [i] = left3;
    }
    }
    else if (id == 4)
    {
    printf("Please enter the qunatity to sell: \n");
    scanf("%d", &sold4);
    {
    if (sold4 > idpur [i])
    printf("Not sold! There are only %d cases available.\n", idpur [i]);
    else
    printf("%d cases are sold and %d cases are left.\n", sold4, left4 = idpur[i] - sold4);
    idpur [i] = left4;
    }
    }
    else if (id == 0)
    {
    printf(" \n");
    }
    else
    {
    printf("You have entered ann invalid code!\n");
    }
    }
    printf("Final Counts: \n");
    printf("Coca-Cola: %d\n", left1);
    printf("Pepsi: %d\n", left2);
    printf("Sr. Pepper: %d\n", left3);
    printf("Canada Dry: %d\n", left4);

    return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > for (i = 1; i > 0; ++i)

    This will likely loop forever because you're incrementing i.

    You should really use a switch instead of all of those 'if/else' checks.

    switch( id )
    {
    case 0: ...all done! exit(0); break;
    case 1: ...do stuff... break;
    case 2: ...do stuff... break;
    default: ...bad command... break;

    }

    Additionally, you could use:

    id = getchar( );
    getchar( );
    switch( id )
    {
    case '0': ...do stuff...
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,703
    I cleaned it up a little, added some basic screen functions, "clrscr()" and "system("PAUSE")", and introduced the "continue" and "break" loop commands. My point is to show you there is an easier way than using all of those "hard-coded" variables.

    i hope it helps.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.c> 
    
    int main()
    {
    int idpur [4];
    int sold;     // <--now just one variable
    
    printf("Please enter the initial number of cases of Coca-Cola: \n");
    scanf("%d", &idpur [0]);
    printf("Please enter the initial number of cases of Pepsi: \n");
    scanf("%d", &idpur [1]);
    printf("Please enter the initial number of cases of Dr. Pepper: \n");
    scanf("%d", &idpur [2]);
    printf("Please enter the initial number of cases of Canada Dry: \n");
    scanf("%d", &idpur [3]);
    
    while(1)  // <--INFINITE LOOP
    {
    printf("Please enter the ID number: \n");
    printf("1 = Coca-Cola\n");
    printf("2 = Pepsi\n");
    printf("3 = Dr. Pepper\n");
    printf("4 = Canada Dry\n");
    printf("0 = Quit\n");
    
    scanf("%d", &id );
    
    if( id == 0 )
    break;        // <-- break out of while loop, print totals
    
    else if( id > 4 )
    {
    printf("You have entered ann invalid code!\n");
    continue;   // <-- Go back to the top of while loop
    }
    
    i = id - 1; //<-- set "i" to proper array element
    
    printf("Please enter the qunatity to sell: \n");
    
    scanf("%d", &sold );
    
    if( sold > idpur[i] )  //<-- i is equiv to coresponding array element 
    {
    printf("Not sold! There are only %d cases available.\n", idpur [i]);
    }
    
    else
    {
    idpur[i] = idpur[i] - sold;  // or...// idpur[i] -= sold;
     
    printf("%d cases are sold and %d cases are left.\n", sold, idpur[i]);
    }
    
    
    
    system("PAUSE"); //<-- you need this to pause screen
    
    clrscr();       //<-- you need this to clear the screen
    
    }  //<-- End of infinite loop
    
    clrscr();
    printf("Final Counts: \n");
    printf("Coca-Cola: %d\n", idpur[0]);
    printf("Pepsi: %d\n", idpur[1]);
    printf("Sr. Pepper: %d\n", idpur[2]);
    printf("Canada Dry: %d\n", idpur[3]);
    
    system("PAUSE");
    
    return 0;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool flip(bool value)
    {
           return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0)*(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  2. Press "Enter" key to quit the program
    By Vitamin_C in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2005, 07:25 PM
  3. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 09:49 PM
  4. Error when trying to quit program...
    By marCplusplus in forum C++ Programming
    Replies: 5
    Last Post: 07-08-2002, 07:03 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21