Array problem

This is a discussion on Array problem within the C Programming forums, part of the General Programming Boards category; Hi, this is my first time posting here. So I'm working on this project for my final that is due ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    3

    Exclamation Array problem

    Hi, this is my first time posting here. So I'm working on this project for my final that is due in a few days for my C programming class. I have just one thing left. My program is a snack machine. The problem exists when the user puts in how much of something they want. Let's say I have 6 dollars and I want a quanity of 7 of an item (each item if 1 dollar) it says please reenter your choice but goes to the next item. I want it to reask for the same item. Sorry if I am doing something wrong. This is my source code.
    Code:
    /*
      Term Project I
      CSC 218, S1
      Spring, 2008
      Dr. Manki Min
      Nathan Nash
      1750669
    */
    
    #include <stdio.h>   //header file
    #include <stdbool.h> //header file
    #include <stdlib.h>  //header file
    #include <math.h>    //header file
    
    int money, choice1; // declaration of variables
    bool a = false; //bool statement
    char candy[8][11] = {"snickers","mars", "hershey", "chips", "crackers", "cookies", "skittles", "gummybears"}; //snacks
    int snacks[8]; // defined array for how many wanted
    bool b = false; //bool statement
    
    int main (void) //main program
    {  // opening of snack machine
      system ( "clear" );  //clears screen
      printf("\n\t\t\tWelcome to Nate's Tummy Yummies!\n\n"); //Title of snack machine
     while (a == false) //loop statement
     {
      printf("\nPlease enter the amount of money you have in dollars.  Ex: 35 dollars = 35. "); //Statement about money
       scanf("%d", &money); //Enter the ammount of money you have
      
         printf("\nYou have entered $%d, is this correct? Type '1' for yes, '2' to return, or '4' to exit.  ", money);  //Asks user if ammount entered was correct.
         scanf("%d", &choice1); //Let's user confirm ammount or have a chance to go back
       
      if (choice1 == 1) // choices
        {
         printf("\nYou have $%d.\n", money); //States how much money the user has left
         printf("You will get to choose from snickers, mars, hershey, chips, crackers, cookies, skittles, and gummybears.\n\n"); //shows choices
         printf("Each item costs $1\n"); //display cost of each item
         a = true;  //continues on with program
        }
      else if (choice1 == 2) //no choice
        {
         a = false; //goes back to enter money
        } 
      else if (choice1 == 4) //choice to quit
      {
         return 0; //terminates program
      } 
      else //else statement
      {
        printf("\nInvalid choice.  Please try again.\n"); //invalid choice
        a = false; //invalid entry returns to choice
      }
      }
      
      int row; //define row
      int col; //define col
      int x;   //define x
      FILE* spOut; //used for opening file
      spOut = fopen("receipt.dat", "w"); //opens receipt.dat
    
      for (row = 0; row < 8; row++) //for statement with how many like of candy
      { 
     	if (money >= 0) //if money is greater than or equal to 0
    	{ 
    	  printf("\nHow many of the %s would you like? ", candy[row]); //asks for how many snacks
              scanf("%d", &snacks[row]); //stores users input for snacks
      	  
    	  if (snacks[row] > money)
    	  {
    	    printf("\nYou don't have enough money please re-enter the amount you want.");
    
    	  }
    	  if (snacks[row] <= money)
    	  {
    	  fprintf(spOut, "\n%d %s ", snacks[row], candy[row]); //prints output to outside file
    	  money = money - snacks[row]; //subtracts money
    	  printf("\nYou have $%d remaining. ", money); //money remaining
    	  }
      	}
      }
      for(row = 0; row < 8; row++) //for statement for how many wanted
      {
          printf("\nYou wanted %d", snacks[row]); //tell how many snacks wanted
          printf(" %s.", candy[row]); //name of candy
      }
      
      printf("\n\nYour receipt is printed on the file receipt.dat\n");  //tells user that file will be made
      printf("\nThank you for choosing Nate's Tummy Yummies!\n"); //closing statement
    
      fprintf(spOut, "\nThank you!"); //prints thanks you to outside file
    
      return 0; //End program
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,468
    you have only 1 function - why so many global vars?
    I want it to reask for the same item
    use same loop idea you have with money asking question
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    3
    This is my first C class. You mean just do another while type statement?

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,468
    You mean just do another while type statement?
    yes... if you want to repeate several statements - wrap them inside some loop

    while seems to be a good choice here... but do{}while maybe even better - because you always want to execute the body at least once - and this is exactly what do..while does
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    3
    Could someone type up what it should look like? I am having trouble at this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Replies: 6
    Last Post: 02-15-2005, 10:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM

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