C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-26-2009, 06:05 AM   #16
Registered User
 
Join Date: Oct 2009
Posts: 13
Quote:
Originally Posted by Adak View Post
That code looks unchanged. I don't see a 'q' to quit, anywhere. Also, that's the sub menu, not the main menu, so it's not the menu I was referring to.

Last time I ran your program, you could make a choice from the main menu, and then make a choice from the sub menu. But you couldn't loop back to the top of the main menu, because there was no loop coded up for the main menu function. The whole program just ended.

It won't be happy until it gets one.

yeah il know, sorry to confuse:P i didd try to add the code you posted but didnt get it to work.
Code:
pick = 'a';  //(anything but 'q') this "primes the pump" for the do loop
do {
   //all the main menu code goes inside this loop
   //including the display, the switch statement
   //etc.
   
}while(pick != 'q');
then il read about saveing info to textfiles so ive just tryed it

the code il added was this:


Code:
void addProduct(void) {
  char addP[30];
  FILE*outfile;
  outfile = fopen("products.txt", "w");
	  printf("\add product:");
	  gets(addP);
	  while(addP[0]!='\0')
	  {
		  fputs(addP, outfile);
		  fprintf(outfile, "\n");

		  printf("add product:");
		  gets(addP);
	  }
	  fclose(outfile);
}
that i fought would save the product i type in to thew textfile: products.txt
casper87 is offline   Reply With Quote
Old 10-26-2009, 06:24 AM   #17
Registered User
 
Join Date: Sep 2006
Posts: 3,139
Code:
void addProduct(void) {
  char addP[30];
  FILE*outfile;
  outfile = fopen("products.txt", "w");   //"at" 
  //change this to append mode, so the new product being added, won't erase the 
  //old file's contents.

  //Also, it's always good to add the "t" to the mode, to specify text mode or a "b" to 
  //specify binary mode. That eliminates any guessing about which mode is being used.

  if(outfile == NULL) {
     printf("\n Error opening products.txt file");
     return 1;
  }
  	  printf("\add product:");
//	  gets(addP); not here, 
	  while(addP[0]!='\0')
	  {
                 //gets() is so unsafe to use, make fgets() a habit, in it's place.
                 fgets(addP, sizeof(addP) - 1, outfile); 
		  fputs(addP, outfile);
		  //not needed, fgets() does this automatically. fprintf(outfile, "\n");

		 //removed two lines trying to add another product. Don't do that. 
                 //if there is another product to add, they'll choose #1 from the sub menu(),
                //when they loop back to it. Only one product added per visit to this function.
	  }
	  fclose(outfile);
}
I did not run this code, and it may have errors. Use it as pseudo-code to improve this function, not ready to run code.
Adak is offline   Reply With Quote
Old 10-26-2009, 09:10 AM   #18
Registered User
 
Join Date: Oct 2009
Posts: 13
Quote:
Originally Posted by Adak View Post
That code looks unchanged. I don't see a 'q' to quit, anywhere. Also, that's the sub menu, not the main menu, so it's not the menu I was referring to.

Last time I ran your program, you could make a choice from the main menu, and then make a choice from the sub menu. But you couldn't loop back to the top of the main menu, because there was no loop coded up for the main menu code, in main(). The whole program just ended.

It won't be happy until it gets one.

i couldnt get the file saveing to work, and i think is good to take one pice at a time.

now i added the q to quit. but when i choose q it just says i picked order..
i also tryed to get the loop right but didnt..:P god i must be a pain in the ass atm:/

the thing i want with this program is that it should be wery simple. just be able to add edit and delete products, orders etc.

question: what does the "*" mean? you use it in
Code:
 char *wordptr
and some other code.




Code:
//basic menu program 

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>          //for toupper

int subMenu(char *); //list of the functions prototypes


void addProduct(void); 
void viewProduct(void);
void editProduct(void); 
void deleteProduct(void); 

void addCustomer(void);
void viewCustomer(void);
void editCustomer(void);
void deleteCustomer(void);



int main(void) {        //always int main

  char pick; 
  char *wordptr;       //char pointer, used to point to and print
  char wordP[] = {"Products" };  // one of these words, in 
  char wordC[] = {"Customers"}; //the sub Menu function
  char wordO[] = {"Order"};
  int subpick;


do {

  printf("\n\n         Main Menu\n         =========\n\n");
  printf("     A. Products\n");
  printf("     B. Customers\n");
  printf("     C. Orders\n");
//after this, add 2 spaces and print Q to Quit choice

  printf("     Q. Quit\n");

  printf("\n     pick one [a,b,c or q]: ");  //needs a 'q' to quit added


  

  scanf("%c", &pick);
  pick = toupper(pick);   //changes lower case letter to upper case letter

  if(pick == 'A')          //gives the wordptr the right word's address
     wordptr = wordP; //the array's name is it's address
  else if(pick == 'B')
     wordptr = wordC;
  else
     wordptr = wordO;

 printf("\n   you chose %s", wordptr);
 subpick = subMenu(wordptr);


  switch (pick) { 
    case 'A':
      if(subpick == 1)   //menu choices logic, grouped by
        addProduct();    // Products, Customers, or Order
      else if(subpick == 2) //then by Add, View, Edit, Delete
        viewProduct();       //functions, below them
      else if(subpick == 3)
        editProduct();
      else if(subpick == 4)
        deleteProduct();
      break;
    case 'B':
      if(subpick == 1)
        addCustomer();
      else if(subpick == 2)
        viewCustomer();
      else if(subpick == 3)
        editCustomer();
      else if(subpick == 4)
        deleteCustomer();
      break;              //switch statements use "fall through" logic
    case 'C'://so every case should end with the break
	
      break;            //line of code. Fall through logic can be very

	
	case 'Q': 
		break;




// Needs a case 'Q': so the user can quit (just a break line will do)

}while(pick != 'q');

    default:            //useful, at times.
      printf("\n There was an error in the switch code");
  }                       //anything not handled by cases above will
  printf("\n\n\t\t\t     press enter when ready"); //come to the default case
  while((pick = getchar()) != '\n');  //pulls char's from the keyboard buffer
  pick = getchar();      //until a newline is reached. This gets one more char 
  return 0;                 //from the user. Effectively stopping the console window from 
}                         //closing before you can view it critically.
int subMenu(char *wordptr) {
  int subpick = 0;


  do {  
  printf("\n\n        %s Menu\n\n", wordptr); //here's our pointer at work
  printf("     1. add\n");
  printf("     2. view\n");
  printf("     3. edit\n");
  printf("     4. delete\n");

  printf("\n     pick one [1-4]: ");
  scanf("%d", &subpick);
  
  }while(subpick < 1 || subpick > 4);  //loop condition helps stop bad input from user

  return subpick;
}
char addP[30];
  FILE*outfile;
  outfile = fopen("products.txt", "w");   //"at" 
  //change this to append mode, so the new product being added, won't erase the 
  //old file's contents.

  //Also, it's always good to add the "t" to the mode, to specify text mode or a "b" to 
  //specify binary mode. That eliminates any guessing about which mode is being used.

  if(outfile == NULL) {
     printf("\n Error opening products.txt file");
     return 1;
  }
  	  printf("\add product:");
//	  gets(addP); not here, 
	  while(addP[0]!='\0')
	  {
                 //gets() is so unsafe to use, make fgets() a habit, in it's place.
                 fgets(addP, sizeof(addP) - 1, outfile); 
		  fputs(addP, outfile);
		  //not needed, fgets() does this automatically. fprintf(outfile, "\n");

		 //removed two lines trying to add another product. Don't do that. 
                 //if there is another product to add, they'll choose #1 from the sub menu(),
                //when they loop back to it. Only one product added per visit to this function.
	  }
	  fclose(outfile);
}


}
void viewProduct(void) {
  printf("\n     Enter a product number to view: ");
}
void editProduct(void) {
  printf("\n     Enter a product number to edit: ");
}
void deleteProduct(void) {
  printf("\n     Enter a product number to delete: ");
}

void addCustomer(void) {
  printf("\n     Enter the new customer's name: ");
}
void viewCustomer(void) {
  printf("\n     Enter the customer's name to view: ");
}
void editCustomer(void) {
  printf("\n     Enter the customer's name to edit: ");
}
void deleteCustomer(void) {
  printf("\n     Enter the customer's name to delete: ");  
}
casper87 is offline   Reply With Quote
Old 10-26-2009, 02:00 PM   #19
Registered User
 
Join Date: Sep 2006
Posts: 3,139
a * used after a data type name means it's a pointer. Pointers are variables that hold addresses, instead of a value.

You use them just like aimed mirrors. Once they're set to just the right target (the address in memory), then when you refer to them, you wind up seeing not the mirror, but what the mirror is aimed at.

this is how yo make the pointer:
char *mycharPointer;
or
int *myintPointer;

and this is how you aim them:

Code:
#include <stdio.h>

int main(void) {

  char mychar = 'A';  //declare a char;
  char *charptr;     //declare a ptr to char

  charptr = &mychar;  //aim the ptr.  giving it the address of mychar

  printf("%c", *charptr);  //print the ptr

  printf("\n\n\t\t\t     press enter when ready");
  mychar = getchar();  //hold the text window open
  return 0;
}

ptr is the standard abbreviation for "pointer", and frequently are used at the front of the variable's name, like ptrmychar, ptrmyint, ptrword, ptrmyName, ptrMySum etc.

Pointers are powerful things, but you have to watch out, using them. Never use a pointer that hasn't been "aimed" and given an address. That's a NULL pointer, and they can wreck havoc with any program that uses them

I'll look into your q to quit and file saving problem when I return (about 2 hours from now).

Last edited by Adak; 10-26-2009 at 02:03 PM.
Adak is offline   Reply With Quote
Old 10-27-2009, 04:07 AM   #20
Registered User
 
Join Date: Oct 2009
Posts: 13
Quote:
Originally Posted by Adak View Post
a * used after a data type name means it's a pointer. Pointers are variables that hold addresses, instead of a value.

You use them just like aimed mirrors. Once they're set to just the right target (the address in memory), then when you refer to them, you wind up seeing not the mirror, but what the mirror is aimed at.

this is how yo make the pointer:
char *mycharPointer;
or
int *myintPointer;

and this is how you aim them:

Code:
#include <stdio.h>

int main(void) {

  char mychar = 'A';  //declare a char;
  char *charptr;     //declare a ptr to char

  charptr = &mychar;  //aim the ptr.  giving it the address of mychar

  printf("%c", *charptr);  //print the ptr

  printf("\n\n\t\t\t     press enter when ready");
  mychar = getchar();  //hold the text window open
  return 0;
}

ptr is the standard abbreviation for "pointer", and frequently are used at the front of the variable's name, like ptrmychar, ptrmyint, ptrword, ptrmyName, ptrMySum etc.

Pointers are powerful things, but you have to watch out, using them. Never use a pointer that hasn't been "aimed" and given an address. That's a NULL pointer, and they can wreck havoc with any program that uses them

I'll look into your q to quit and file saving problem when I return (about 2 hours from now).
hehe ty man! love ya making so much more progress with your help then sitting and random read a book after answers.
casper87 is offline   Reply With Quote
Old 10-27-2009, 12:25 PM   #21
Registered User
 
Join Date: Sep 2006
Posts: 3,139
This is your program with a few wrinkles ironed out.


Code:
//basic menu program 

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>    //for toupper

int subMenu(char *);
void addProduct(void); 
void viewProduct(void);
void editProduct(void); 
void deleteProduct(void); 
void addCustomer(void);
void viewCustomer(void);
void editCustomer(void);
void deleteCustomer(void);


int main(void) {  

  char pick; 
  char *wordptr;
  char wordP[] = {"Products" };
  char wordC[] = {"Customers"};
  char wordO[] = {"Order"};
  int subpick;

  do {

    printf("\n\n         Main Menu\n         =========\n\n");
    printf("     A. Products\n");
    printf("     B. Customers\n");
    printf("     C. Orders\n\n");
    printf("     Q. Quit the Program\n");
    printf("\n     pick one [a,b,c,q]: ");

    scanf("%c", &pick);
    pick = toupper(pick);
  
    if(pick == 'A')
       wordptr = wordP;
    else if(pick == 'B')
       wordptr = wordC;
    else if(pick == 'C')
       wordptr = wordO;
    else if(pick == 'Q')
       break;
    else
       continue;
   printf("\n   you chose %s", wordptr);
   subpick = subMenu(wordptr);

    switch (pick) { 
      case 'A':
        if(subpick == 1)
          addProduct();
        else if(subpick == 2)
          viewProduct();
        else if(subpick == 3)
          editProduct();
        else if(subpick == 4)
          deleteProduct();
        break;
      case 'B':
        if(subpick == 1)
          addCustomer();
        else if(subpick == 2)
          viewCustomer();
        else if(subpick == 3)
          editCustomer();
        else
          deleteCustomer();
        break;
      case 'C':
          
        break;
      default:
        printf("\n That selection is not available");
    }       

  }while(pick != 'Q'); 
  printf("\n\n\t\t\t     press enter when ready");
  while((pick = getchar()) != '\n');
  pick = getchar();
  return 0;
}
int subMenu(char *wordptr) {
  int i, subpick = 0;
    
  do {  
    printf("\n\n        %s Menu\n\n", wordptr);
    printf("     1. add\n");
    printf("     2. view\n");
    printf("     3. edit\n");
    printf("     4. delete\n\n");
    printf("     5. return\n");
    printf("\n     pick one [1-5]: ");
    scanf("%d", &subpick);
    i = getchar(); i++;
  }while(subpick < 1 || subpick > 5);

  return subpick;
}
void addProduct(void) {
  char addP[40];
  FILE *outfile;
  outfile = fopen("products.txt", "at");   //"at" 

  if(outfile == NULL) {
     printf("\n Error opening products.txt file");
     return;
  }
 	printf("\n add product name and price [name <space> price]:");
  fgets(addP, sizeof(addP) - 1, stdin); 
  fprintf(outfile, "%s", addP);
	fclose(outfile);
}
void viewProduct(void) {
  FILE *fp;
  int i, quit;
  char view[40];
  if((fp = fopen("products.txt", "rt")) == NULL) {
    printf("\nError opening products file\n");
    return;
  }
  i = quit = 0;
  printf("\n");
  while((fgets(view, sizeof(view), fp)) != NULL) {
    printf(" %s", view);
    if(++i == 3) {
      printf("\n\n Press enter to continue viewing or Q to quit ");
      i = getchar();
      if(i == 'Q' || i == 'q') {
        quit = 1;
        break;
      }
      i = 0;
    }
  }
  if(quit == 0)
    printf("\n End of products file");
  fclose(fp);
}
void editProduct(void) {
  printf("\n     Enter a product number to edit: ");
}
void deleteProduct(void) {
  printf("\n     Enter a product number to delete: ");
}

void addCustomer(void) {
  printf("\n     Enter the new customer's name: ");
}
void viewCustomer(void) {
  printf("\n     Enter the customer's name to view: ");
}
void editCustomer(void) {
  printf("\n     Enter the customer's name to edit: ");
}
void deleteCustomer(void) {
  printf("\n     Enter the customer's name to delete: ");  
}
Adak is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Smooth walking on tile based map system abraham2119 C Programming 8 07-10-2009 10:33 AM
intereacting with fily system in c KBriggs C Programming 8 06-26-2009 02:28 AM
Laplace Expansion Leojeen C Programming 7 10-28-2008 11:26 PM
Best way to have an list of indices which changes order laertius C Programming 5 09-29-2008 12:45 AM


All times are GMT -6. The time now is 04:32 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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