![]() |
| | #16 | |
| Registered User Join Date: Oct 2009
Posts: 13
| Quote:
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');
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);
}
| |
| casper87 is offline | |
| | #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);
}
|
| Adak is offline | |
| | #18 | |
| Registered User Join Date: Oct 2009
Posts: 13
| Quote:
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 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 | |
| | #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 | |
| | #20 | |
| Registered User Join Date: Oct 2009
Posts: 13
| Quote:
making so much more progress with your help then sitting and random read a book after answers. | |
| casper87 is offline | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |