9 Replies - 421 Views - Last Post: 09 November 2018 - 12:48 AM Rate Topic: -----

#1 jleake   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-November 18

Unknown reason for program ending

Posted 06 November 2018 - 01:22 PM

I have no idea why I can't restart the code once I have pressed yes I want to continue shopping, it just ends the program some reason, i've tried to solve this but im lost.
int main()
{
    //Fruit Type
    int type;
    
    //Confirmation Code Yes|No
    int confirm1;
    
    //Total Weight
    double weight;
    
    //Item Price
    double price;
    double finalprice;
    double tax;
    double subtotal;

    
    //Restart weight code Broadcast
    int restart=1;
    int retrigger1=0;
    //Starting prompt
    char ch;
    printf("Press ENTER key to Continue\n");
    scanf("%c",&ch);
    //Intro Text
    system("clear");
    printf("\033[01;33m");
    printf("Made by Jaxon, Leake\n");
    sleep(3);
    system("clear");
    printf("\033[01;33m");
    printf("Recorces used:\n");
    printf("\033[0m");
    printf(" 1.https://www.includehelp.com/c-programming-questions/how-to-clear-output-screen-in-c.aspx\n");
    printf(" 2.http://web.theurbanpenguin.com/adding-color-to-your-output-from-c/\n");
    printf(" 3.https://stackoverflow.com/questions/10922900/is-it-possible-to-wait-a-few-seconds-before-printing-a-new-line-in-c\n");
    printf(" 4.https://stackoverflow.com/questions/18801483/press-any-key-to-continue-function-in-c\n");
    sleep(5);
    system("clear");
    printf("\033[01;33m");
    printf("Welcome to Yo'belle, may i take your order?\n");
    //Start&Restart
    while (restart==1) {
        restart=0;
        printf("\033[0m");
        printf("Enter a number:\n");
        printf("1.Strawberry, 2.Orange, 3.Bananna, 4.Apple, 5.Grapes, 6.Apricot, 7.Plum, 8.Pear, 9.Peach\n");
        scanf("%d", &type);
        system("clear");
        printf("\033[01;33m");
        printf("Did you select ");
        if(type==1) {
            printf("Strawberry ");
        }
        if(type==2) {
            printf("Orange ");
        }
        if(type==3) {
            printf("Bananna ");
        }
        if(type==4) {
            printf("Apple ");
        }
        if(type==5) {
            printf("Grape ");
        }
        if(type==6) {
            printf("Apricot ");
        }
        if(type==7) {
            printf("Plum ");
        }
        if(type==8) {
            printf("Pear ");
        }
        if(type==9) {
            printf("Peach ");
        }
        printf("Flavoured Yogurt?\n");
        printf("\033[0m");
        printf("1.Yes | 2.No\n");
        scanf("%d", &confirm1);
        system("clear");
        //Proceed(Yes)
        if(confirm1==1) {
            system("clear");
            retrigger1=1;
            confirm1=0;
        }
        //Retry(no)
        if(confirm1==2) {
            system("clear");
            restart=1;
            confirm1=0;
        }
    }
    while(retrigger1==1) {
        retrigger1=0;
        printf("\033[01;33m");
        printf("How many ounces of ");
        if(type==1) {
        printf("Strawberry ");
        }
        if(type==2) {
            printf("Orange ");
        }
        if(type==3) {
            printf("Bananna ");
        }
        if(type==4) {
            printf("Apple ");
        }
        if(type==5) {
            printf("Grape ");
        }
        if(type==6) {
            printf("Apricot ");
        }
        if(type==7) {
            printf("Plum ");
        }
        if(type==8) {
            printf("Pear ");
        }
        if(type==9) {
            printf("Peach ");
        }
        printf("yogurt would you like?\n ");
        printf("\033[0m");
        if(type==1) {
            printf("Strawberry flavoured yogurt cost 0.39$ ");
            price=weight*0.39;
        }
        if(type==2) {
            printf("Orange flavoured yogurt cost 0.39$ ");
            price=weight*0.39;
            
        }
        if(type==3) {
            printf("Bananna flavoured yogurt cost 0.39$ ");
            price=weight*0.39;
        }
        if(type==4) {
            printf("Apple flavoured yogurt cost 0.39$ ");
            price=weight*0.39;
        }
        if(type==5) {
            printf("Grape flavoured yogurt cost 0.39$ ");
            price=weight*0.39;
        }
        if(type==6) {
            printf("Apricot flavoured yogurt cost 0.39$ ");
            price=weight*0.39;
        }
        if(type==7) {
            printf("Plum flavoured yogurt cost 0.39$ ");
            price=weight*0.39;
        }
        if(type==8) {
            printf("Pear flavoured yogurt cost 0.39$ ");
            price=weight*0.39;
        }
        if(type==9) {
            printf("Peach flavoured yogurt cost 0.39$ ");
            price=weight*0.39;
        }
        printf("an ounce\n");
        scanf("%lf", &weight);
        system("clear");
        printf("\033[01;33m");
        printf("did you say %lf ounces\n", weight);
        printf("\033[0m");
        printf(" 1.Yes | 2.No\n");
        scanf("%d", &confirm1);
        system("clear");
        //Yes
        subtotal=subtotal+price;
        tax=subtotal*0.0825;
        finalprice=subtotal+tax;
        printf("\033[01;33m");
        printf("Recipt\n");
        printf("\033[0m");
        printf("Subtotal: $%lf\n",subtotal);
        printf("Tax: $%lf\n",tax);
        printf("Total: $%lf\n\n",finalprice);
        printf("\033[01;33m");
        printf("Would you like to purchase another item?\n");
        printf("\033[0m");
        printf(" 1.Yes | 2.No \n");
        scanf("%d", &confirm1);
        //Continue Shopping(Yes)
        if(confirm1==1) {
            system("clear");
            confirm1=0;
            restart=1;
            //printf("\033[01;33mSorry\n\033[0mwe're working on this function...");
        }
        //Complete Order(No)
        if(confirm1==2) {
            confirm1=0;
            system("clear");
            printf("\033[01;33m");
            printf("Recipt\n");
            printf("\033[0m");
            printf("Subtotal: $%lf\n",subtotal);
            printf("Tax: $%lf\n",tax);
            printf("Total: $%lf\n\n",finalprice);
            printf("\033[01;33m");
            printf("Thanks for shopping at Yo'Belle");
        }
        //Debug Menu
        if(confirm1==3) {
            system("clear");
            confirm1=0;
            type=0;
            weight=0;
            price=0;
            printf("INFORMATION:\n");
            printf("type: %d\n", type);
            printf("confirm1: %d\n", confirm1);
            printf("weight: %lf\n", weight);
            printf("price: %lf\n", price);
            printf("finalprice: %lf\n", finalprice);
            printf("tax: %lf\n", tax);
            printf("subtotal: %lf\n", subtotal);
            printf("restart: %d\n", restart);
            printf("retrigger1: %d\n", retrigger1);
        }
    }
    //No
    if(confirm1==2) {
        retrigger1=1;
        confirm1=0;
    }
return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Unknown reason for program ending

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14498
  • View blog
  • Posts: 58,117
  • Joined: 12-June 08

Re: Unknown reason for program ending

Posted 06 November 2018 - 01:29 PM

Think about which WHILE loop you are in when asking that question.
Was This Post Helpful? 0
  • +
  • -

#3 snoopy11   User is online

  • Engineering ● Software
  • member icon

Reputation: 1549
  • View blog
  • Posts: 4,930
  • Joined: 20-March 10

Re: Unknown reason for program ending

Posted 06 November 2018 - 03:35 PM

This is the nub of your problem,

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int restart =1;
    int input   =0;
    int retrigger =1;
    while(restart == 1)
    {

        restart =0;
        while(retrigger ==1)
        {
            retrigger =0;

            printf("1.Stay 2.Exit ?(1or2)");
            scanf("%d",&input);
            if(input==1)
            {
                restart =1;
            }
        }
    }

    return 0;
}




Solving the above problem will also solve your problem, its pretty obvious really.

Also, you should be aware that system("clear") on *nix systems is non-standard ie it is not implemented in the same way across all systems.

And Functions, Functions, Functions !!
Was This Post Helpful? 0
  • +
  • -

#4 jleake   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-November 18

Re: Unknown reason for program ending

Posted 07 November 2018 - 12:40 PM

tried applying this solution
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int restart =1;
    int input   =0;
    int retrigger =1;
    while(restart == 1)
    {
        retrigger=1;
        restart =0;
        while(retrigger ==1)
        {
            retrigger =0;

            printf("1.Stay 2.Exit ?(1or2)");
            scanf("%d",&input);
            if(input==1)
            {
                restart =1;
            }
        }
    }

    return 0;
}


and it didnt work
int main()
{
    //Fruit Type
    int type;
    
    //Confirmation Code Yes|No
    int confirm1;
    
    //Total Weight
    double weight;
    
    //Item Price
    double price;
    double finalprice;
    double tax;
    double subtotal;

    
    //Restart weight code Broadcast
    int restart=1;
    int retrigger1=0;
    //Starting prompt
    char ch;
    printf("Press ENTER key to Continue\n");
    scanf("%c",&ch);
    //Intro Text
    system("clear");
    printf("\033[01;33m");
    printf("Made by Jaxon, Leake\n");
    sleep(3);
    system("clear");
    printf("\033[01;33m");
    printf("Recorces used:\n");
    printf("\033[0m");
    printf(" 1.https://www.includehelp.com/c-programming-questions/how-to-clear-output-screen-in-c.aspx\n");
    printf(" 2.http://web.theurbanpenguin.com/adding-color-to-your-output-from-c/\n");
    printf(" 3.https://stackoverflow.com/questions/10922900/is-it-possible-to-wait-a-few-seconds-before-printing-a-new-line-in-c\n");
    printf(" 4.https://stackoverflow.com/questions/18801483/press-any-key-to-continue-function-in-c\n");
    sleep(5);
    system("clear");
    printf("\033[01;33m");
    printf("Welcome to Yo'belle, may i take your order?\n");
    //Start&Restart
    while (restart==1) {
        restart=0;
        printf("\033[0m");
        printf("Enter a number:\n");
        printf("1.Strawberry, 2.Orange, 3.Bananna, 4.Apple, 5.Grapes, 6.Apricot, 7.Plum, 8.Pear, 9.Peach\n");
        scanf("%d", &type);
        system("clear");
        printf("\033[01;33m");
        printf("Did you select ");
        if(type==1) {
            printf("Strawberry ");
        }
        if(type==2) {
            printf("Orange ");
        }
        if(type==3) {
            printf("Bananna ");
        }
        if(type==4) {
            printf("Apple ");
        }
        if(type==5) {
            printf("Grape ");
        }
        if(type==6) {
            printf("Apricot ");
        }
        if(type==7) {
            printf("Plum ");
        }
        if(type==8) {
            printf("Pear ");
        }
        if(type==9) {
            printf("Peach ");
        }
        printf("Flavoured Yogurt?\n");
        printf("\033[0m");
        printf("1.Yes | 2.No\n");
        scanf("%d", &confirm1);
        system("clear");
        //Proceed(Yes)
        if(confirm1==1) {
            system("clear");
            retrigger1=1;
            confirm1=0;
        }
        //Retry(no)
        if(confirm1==2) {
            system("clear");
            restart=1;
            confirm1=0;
        }
    }
    retrigger1=1;
    while(retrigger1==1) {
        retrigger1=0;
        printf("\033[01;33m");
        printf("How many ounces of ");
        if(type==1) {
        printf("Strawberry ");
        }
        if(type==2) {
            printf("Orange ");
        }
        if(type==3) {
            printf("Bananna ");
        }
        if(type==4) {
            printf("Apple ");
        }
        if(type==5) {
            printf("Grape ");
        }
        if(type==6) {
            printf("Apricot ");
        }
        if(type==7) {
            printf("Plum ");
        }
        if(type==8) {
            printf("Pear ");
        }
        if(type==9) {
            printf("Peach ");
        }
        printf("yogurt would you like?\n ");
        printf("\033[0m");
        if(type==1) {
            printf("Strawberry flavoured yogurt cost 0.39$ ");
            price=weight*0.39;
        }
        if(type==2) {
            printf("Orange flavoured yogurt cost 0.39$ ");
            price=weight*0.39;
            
        }
        if(type==3) {
            printf("Bananna flavoured yogurt cost 0.39$ ");
            price=weight*0.39;
        }
        if(type==4) {
            printf("Apple flavoured yogurt cost 0.39$ ");
            price=weight*0.39;
        }
        if(type==5) {
            printf("Grape flavoured yogurt cost 0.39$ ");
            price=weight*0.39;
        }
        if(type==6) {
            printf("Apricot flavoured yogurt cost 0.39$ ");
            price=weight*0.39;
        }
        if(type==7) {
            printf("Plum flavoured yogurt cost 0.39$ ");
            price=weight*0.39;
        }
        if(type==8) {
            printf("Pear flavoured yogurt cost 0.39$ ");
            price=weight*0.39;
        }
        if(type==9) {
            printf("Peach flavoured yogurt cost 0.39$ ");
            price=weight*0.39;
        }
        printf("an ounce\n");
        scanf("%lf", &weight);
        system("clear");
        printf("\033[01;33m");
        printf("did you say %lf ounces\n", weight);
        printf("\033[0m");
        printf(" 1.Yes | 2.No\n");
        scanf("%d", &confirm1);
        system("clear");
        //Yes
        subtotal=subtotal+price;
        tax=subtotal*0.0825;
        finalprice=subtotal+tax;
        printf("\033[01;33m");
        printf("Recipt\n");
        printf("\033[0m");
        printf("Subtotal: $%lf\n",subtotal);
        printf("Tax: $%lf\n",tax);
        printf("Total: $%lf\n\n",finalprice);
        printf("\033[01;33m");
        printf("Would you like to purchase another item?\n");
        printf("\033[0m");
        printf(" 1.Yes | 2.No \n");
        scanf("%d", &confirm1);
        //Continue Shopping(Yes)
        if(confirm1==1) {
            system("clear");
            confirm1=0;
            restart=1;
            //printf("\033[01;33mSorry\n\033[0mwe're working on this function...");
        }
        //Complete Order(No)
        if(confirm1==2) {
            confirm1=0;
            system("clear");
            printf("\033[01;33m");
            printf("Recipt\n");
            printf("\033[0m");
            printf("Subtotal: $%lf\n",subtotal);
            printf("Tax: $%lf\n",tax);
            printf("Total: $%lf\n\n",finalprice);
            printf("\033[01;33m");
            printf("Thanks for shopping at Yo'Belle");
        }
        //Debug Menu
        if(confirm1==3) {
            system("clear");
            confirm1=0;
            type=0;
            weight=0;
            price=0;
            printf("INFORMATION:\n");
            printf("type: %d\n", type);
            printf("confirm1: %d\n", confirm1);
            printf("weight: %lf\n", weight);
            printf("price: %lf\n", price);
            printf("finalprice: %lf\n", finalprice);
            printf("tax: %lf\n", tax);
            printf("subtotal: %lf\n", subtotal);
            printf("restart: %d\n", restart);
            printf("retrigger1: %d\n", retrigger1);
        }
    }
    //No
    if(confirm1==2) {
        retrigger1=1;
        confirm1=0;
    }
return 0;
}


This post has been edited by Skydiver: 07 November 2018 - 02:20 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#5 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14498
  • View blog
  • Posts: 58,117
  • Joined: 12-June 08

Re: Unknown reason for program ending

Posted 07 November 2018 - 12:43 PM

Again.. look at how your WHILE loops are positioned. Are they one after the other, or one inside the other?
Was This Post Helpful? 3
  • +
  • -

#6 snoopy11   User is online

  • Engineering ● Software
  • member icon

Reputation: 1549
  • View blog
  • Posts: 4,930
  • Joined: 20-March 10

Re: Unknown reason for program ending

Posted 07 November 2018 - 11:00 PM

Well, you are close to a solution now, all you have to do as modi123_1 has already pointed out is put the second while loop inside the first while loop.

The fact you cannot see it is because your code is so long inside of main(), you should always have a fairly short main() with everything broken down into discrete functions.

Hence the Functions, Functions, Functions advice !!

This post has been edited by snoopy11: 07 November 2018 - 11:01 PM
Reason for edit:: double post

Was This Post Helpful? 2
  • +
  • -

#7 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2238
  • View blog
  • Posts: 4,325
  • Joined: 30-May 10

Re: Unknown reason for program ending

Posted 07 November 2018 - 11:38 PM

Another prime example of why indentation MATTERS!
#include <stdio.h>
int main()
{
  //Fruit Type
  int type;

  //Confirmation Code Yes|No
  int confirm1;

  //Total Weight
  double weight;

  //Item Price
  double price;
  double finalprice;
  double tax;
  double subtotal;

  //Restart weight code Broadcast
  int restart = 1;
  int retrigger1 = 0;
  //Starting prompt
  char ch;
  printf("Press ENTER key to Continue\n");
  scanf("%c", &ch);
  //Intro Text
  system("clear");
  printf("\033[01;33m");
  printf("Made by Jaxon, Leake\n");
  sleep(3);
  system("clear");
  printf("\033[01;33m");
  printf("Recorces used:\n");
  printf("\033[0m");
  printf(" 1.https://www.includehelp.com/c-programming-questions/how-to-clear-output-screen-in-c.aspx\n");
  printf(" 2.http://web.theurbanpenguin.com/adding-color-to-your-output-from-c/\n");
  printf(" 3.https://stackoverflow.com/questions/10922900/is-it-possible-to-wait-a-few-seconds-before-printing-a-new-line-in-c\n");
  printf(" 4.https://stackoverflow.com/questions/18801483/press-any-key-to-continue-function-in-c\n");
  sleep(5);
  system("clear");
  printf("\033[01;33m");
  printf("Welcome to Yo'belle, may i take your order?\n");
  //Start&Restart
  while (restart == 1) {
    restart = 0;
    printf("\033[0m");
    printf("Enter a number:\n");
    printf("1.Strawberry, 2.Orange, 3.Bananna, 4.Apple, 5.Grapes, 6.Apricot, 7.Plum, 8.Pear, 9.Peach\n");
    scanf("%d", &type);
    system("clear");
    printf("\033[01;33m");
    printf("Did you select ");
    if (type == 1) {
      printf("Strawberry ");
    }
    if (type == 2) {
      printf("Orange ");
    }
    if (type == 3) {
      printf("Bananna ");
    }
    if (type == 4) {
      printf("Apple ");
    }
    if (type == 5) {
      printf("Grape ");
    }
    if (type == 6) {
      printf("Apricot ");
    }
    if (type == 7) {
      printf("Plum ");
    }
    if (type == 8) {
      printf("Pear ");
    }
    if (type == 9) {
      printf("Peach ");
    }
    printf("Flavoured Yogurt?\n");
    printf("\033[0m");
    printf("1.Yes | 2.No\n");
    scanf("%d", &confirm1);
    system("clear");
    //Proceed(Yes)
    if (confirm1 == 1) {
      system("clear");
      retrigger1 = 1;
      confirm1 = 0;
    }
    //Retry(no)
    if (confirm1 == 2) {
      system("clear");
      restart = 1;
      confirm1 = 0;
    }
  }
  retrigger1 = 1;
  while (retrigger1 == 1) {
    retrigger1 = 0;
    printf("\033[01;33m");
    printf("How many ounces of ");
    if (type == 1) {
      printf("Strawberry ");
    }
    if (type == 2) {
      printf("Orange ");
    }
    if (type == 3) {
      printf("Bananna ");
    }
    if (type == 4) {
      printf("Apple ");
    }
    if (type == 5) {
      printf("Grape ");
    }
    if (type == 6) {
      printf("Apricot ");
    }
    if (type == 7) {
      printf("Plum ");
    }
    if (type == 8) {
      printf("Pear ");
    }
    if (type == 9) {
      printf("Peach ");
    }
    printf("yogurt would you like?\n ");
    printf("\033[0m");
    if (type == 1) {
      printf("Strawberry flavoured yogurt cost 0.39$ ");
      price = weight * 0.39;
    }
    if (type == 2) {
      printf("Orange flavoured yogurt cost 0.39$ ");
      price = weight * 0.39;
    }
    if (type == 3) {
      printf("Bananna flavoured yogurt cost 0.39$ ");
      price = weight * 0.39;
    }
    if (type == 4) {
      printf("Apple flavoured yogurt cost 0.39$ ");
      price = weight * 0.39;
    }
    if (type == 5) {
      printf("Grape flavoured yogurt cost 0.39$ ");
      price = weight * 0.39;
    }
    if (type == 6) {
      printf("Apricot flavoured yogurt cost 0.39$ ");
      price = weight * 0.39;
    }
    if (type == 7) {
      printf("Plum flavoured yogurt cost 0.39$ ");
      price = weight * 0.39;
    }
    if (type == 8) {
      printf("Pear flavoured yogurt cost 0.39$ ");
      price = weight * 0.39;
    }
    if (type == 9) {
      printf("Peach flavoured yogurt cost 0.39$ ");
      price = weight * 0.39;
    }
    printf("an ounce\n");
    scanf("%lf", &weight);
    system("clear");
    printf("\033[01;33m");
    printf("did you say %lf ounces\n", weight);
    printf("\033[0m");
    printf(" 1.Yes | 2.No\n");
    scanf("%d", &confirm1);
    system("clear");
    //Yes
    subtotal = subtotal + price;
    tax = subtotal * 0.0825;
    finalprice = subtotal + tax;
    printf("\033[01;33m");
    printf("Recipt\n");
    printf("\033[0m");
    printf("Subtotal: $%lf\n", subtotal);
    printf("Tax: $%lf\n", tax);
    printf("Total: $%lf\n\n", finalprice);
    printf("\033[01;33m");
    printf("Would you like to purchase another item?\n");
    printf("\033[0m");
    printf(" 1.Yes | 2.No \n");
    scanf("%d", &confirm1);
    //Continue Shopping(Yes)
    if (confirm1 == 1) {
      system("clear");
      confirm1 = 0;
      restart = 1;
      //printf("\033[01;33mSorry\n\033[0mwe're working on this function...");
    }
    //Complete Order(No)
    if (confirm1 == 2) {
      confirm1 = 0;
      system("clear");
      printf("\033[01;33m");
      printf("Recipt\n");
      printf("\033[0m");
      printf("Subtotal: $%lf\n", subtotal);
      printf("Tax: $%lf\n", tax);
      printf("Total: $%lf\n\n", finalprice);
      printf("\033[01;33m");
      printf("Thanks for shopping at Yo'Belle");
    }
    //Debug Menu
    if (confirm1 == 3) {
      system("clear");
      confirm1 = 0;
      type = 0;
      weight = 0;
      price = 0;
      printf("INFORMATION:\n");
      printf("type: %d\n", type);
      printf("confirm1: %d\n", confirm1);
      printf("weight: %lf\n", weight);
      printf("price: %lf\n", price);
      printf("finalprice: %lf\n", finalprice);
      printf("tax: %lf\n", tax);
      printf("subtotal: %lf\n", subtotal);
      printf("restart: %d\n", restart);
      printf("retrigger1: %d\n", retrigger1);
    }
  }
  //No
  if (confirm1 == 2) {
    retrigger1 = 1;
    confirm1 = 0;
  }
  return 0;
}



Your while (restart == 1) and while (retrigger1 == 1) loops are completely independent.

It's also another prime example of why writing balls of mud 100's of lines long leads to just that kind of confusion over what is supposed to happen when.

You desperately need to start using some functions in this code.


Then there is this problem as well.
    if (type == 9) {
      printf("Peach flavoured yogurt cost 0.39$ ");
      price = weight * 0.39;
    }
    printf("an ounce\n");
    scanf("%lf", &weight);
    system("clear");
    printf("\033[01;33m");
    printf("did you say %lf ounces\n", weight);
    printf("\033[0m");
    printf(" 1.Yes | 2.No\n");
    scanf("%d", &confirm1);
    system("clear");
    //Yes
    subtotal = subtotal + price;


price = weight * 0.39; is NOT re-evaluated at the point you do scanf("%lf", &weight);

Do you know about structures and arrays yet? If you do, you can massively simplify all the copy/paste you've managed to do already.
Was This Post Helpful? 1
  • +
  • -

#8 albert003   User is online

  • D.I.C Addict

Reputation: 20
  • View blog
  • Posts: 571
  • Joined: 15-December 14

Re: Unknown reason for program ending

Posted 08 November 2018 - 01:27 AM

I was about to say the same thing snoopy11 suggested. You need to use functions in this program. I also suggest you stop and make a flow chart to see on paper what you want the program to do. Because the way you have the program it doesn't seem like you made a flow chart.
Was This Post Helpful? 0
  • +
  • -

#9 jleake   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-November 18

Re: Unknown reason for program ending

Posted 09 November 2018 - 12:01 AM

thanks for the help, i redid the entire program and this is my final product
int main()
{
    char ch;
    printf("Press ENTER key to Continue\n");
    scanf("%c",&ch);
    system("clear");
    int confirm=0;
    int purchase=0;
    int a=1;
    int b=0;
    int c=0;
    int d=0;
    double weight=0;
    double itemcost=0;
    double finalcost=0;
    double tax=0;
    double finalcost2=0;
    int A=0;
    double apple=0;
    int B=0;
    double raspberry=0;
    int C=0;
    double orange=0;
    int D=0;
    double watermelon=0;
    int E=0;
    double strawberry=0;
    while(a==1) {
        a=0;
        printf("\033[01;33m");
        printf("What would you like to purchase?\n");
        printf("\033[0m");
        printf("1.Apple, 2.Rasperry, 3.Orange, 4.Watermelon, 5.Strawberry\n");
        scanf("%d",&purchase);
        system("clear");
        printf("\033[01;33m");
        printf("Did you select ");
        if(purchase==1) {
            printf("Apple\n");
        }
        if(purchase==2) {
            printf("Raspberry\n");
        }
        if(purchase==3) {
            printf("Orange\n");
        }
        if(purchase==4) {
            printf("Watermelon\n");
        }
        if(purchase==5) {
            printf("Strawberry\n");
        }
        printf("\033[0m");
        printf("1.Yes | 2.No\n");
        scanf("%d",&confirm);
        system("clear");
        if(confirm==1) {
            b=1;
        }
        if(confirm==2) {
            a=1;
        }
        while(b==1) {
            b=0;
            printf("\033[01;33m");
            printf("And how many ounces of this would you like?\n");
            printf("\033[0m");
            scanf("%lf",&weight);
            system("clear");
            printf("\033[01;33m");
            printf("Did you say %lf ounces?\n",weight);
            printf("\033[0m");
            printf("1.Yes | 2.No\n");
            scanf("%d",&confirm);
            system("clear");
            if(confirm==1) {
                c=1;
            }
            if(confirm==2) {
                b=1;
            }
            while(c==1) {
                c=0;
                if(purchase==1) {
                    itemcost=weight*0.39;
                    apple=apple+itemcost;
                    A=1;
                }
                if(purchase==2) {
                    itemcost=weight*0.39;
                    raspberry=raspberry+itemcost;
                    B=1;
                }
                if(purchase==3) {
                    itemcost=weight*0.39;
                    orange=orange+itemcost;
                    C=1;
                }
                if(purchase==4) {
                    itemcost=weight*0.39;
                    watermelon=watermelon+itemcost;
                    D=1;
                }
                if(purchase==5) {
                    itemcost=weight*0.39;
                    strawberry=strawberry+itemcost;
                    E=1;
                }
                finalcost=finalcost+itemcost;
                tax=finalcost*0.0825;
                finalcost2=finalcost+tax;
                /*printf("\033[01;33m");
                printf("the total cost will be %lf\n",finalcost);
                printf("\033[0m");
                sleep(2);*/
                system("clear");
                printf("\033[01;33m");
                printf("Would you like to continue shopping?\n");
                printf("\033[0m");
                printf("the total cost will be %lf\n",finalcost);
                printf("\033[0m");
                printf("1.Yes | 2.No\n");
                scanf("%d",&confirm);
                system("clear");
                if(confirm==1) {
                    a=1;
                }
                if(confirm==2) {
                    printf("\033[01;33m");
                    printf("Receipt\n");
                    printf("------------------------\n");
                    if(A==1) {
                        printf("\033[01;33mApple:\033[0m $%lf\n",apple);
                    }
                    if(B==1) {
                        printf("\033[01;33mRaspberry:\033[0m $%lf\n",raspberry);
                    }
                    if(C==1) {
                        printf("\033[01;33mOrange:\033[0m $%lf\n",orange);
                    }
                    if(D==1) {
                        printf("\033[01;33mWatermelon:\033[0m $%lf\n",watermelon);
                    }
                    if(E==1) {
                        printf("\033[01;33mStrawberry:\033[0m $%lf\n",strawberry);
                    }
                    printf("\033[01;33m");
                    printf("------------------------\n");
                    printf("\033[01;33mSubtotal:\033[0m $%lf\n",finalcost);
                    printf("\033[01;33mTax:\033[0m $%lf\n",tax);
                    printf("\033[01;33mTotal:\033[0m $%lf\n",finalcost2);
                }
            }
        }
    }
return 0;
}

Was This Post Helpful? 0
  • +
  • -

#10 albert003   User is online

  • D.I.C Addict

Reputation: 20
  • View blog
  • Posts: 571
  • Joined: 15-December 14

Re: Unknown reason for program ending

Posted 09 November 2018 - 12:48 AM

You have the following problems with the code because it looks confusing and messy. Read about functions, arrays and structs they will really help clean up your code. What I suggest you do is go back to a flow chart and draw out how you could use functions, arrays and structs in the program. If you're not sure what those things are look on youtube on go on line and search google. Trust me it will make a difference. Everyone here has given you the tools needed to help you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1