Originally Posted by
Salem
Having a member variable called i, and a loop variable called i, will surely lead to confusion.
Also, main returns and int, not void.
Originally Posted by
GReaper
You have misunderstood how variable-length arrays work. In a nutshell, changing "i" won't change the size of the array after it was declared. You will have to use "realloc()" for that.
Alright. Now I am using the FILE to store the data.
Problem came after 1st loop where, run time error occur, after I put : fprintf(fp, "%d %s %d %f", a[i].id, a[i].name, a[i].quantity, a[i].price);
What is the problem actually ? Thanks in advance.
Code:
#include <stdio.h>#include <stdlib.h>
#include <windows.h>
#include <ctype.h>
#include <conio.h>
void gotoxy( int column, int line );
void addPRODUCT();
int displayPRODUCT();
struct product
{
int quantity, reorder, i;
char name[20];
float price, id ;
};
COORD coord = {0, 0};
void gotoxy (int x, int y)
{
coord.X = x; coord.Y = y; // X and Y coordinates
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
/*void main()
{
addPRODUCT();
displayPRODUCT();
}*/
int main()
{
FILE * fp;
int i=0;
struct product a[i];
system("cls");
char checker;
do
{
gotoxy(20,5);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 ADD PRODUCTS \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(20,15);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(20,7);
printf("Enter product ID : ");
scanf(" %d", &a[i].id);
gotoxy(20,9);
printf("Enter product name : ");
scanf(" %s", a[i].name);
gotoxy(20,11);
printf("Enter product quantity : ");
scanf(" %d", &a[i].quantity);
gotoxy(20,13);
printf("Enter product price : ");
scanf(" %f", &a[i].price);
fp = fopen("addproduct.txt","a");
fprintf(fp, "%d %s %d %f", a[i].id, a[i].name, a[i].quantity, a[i].price); //Without this line. No problem with looping.
gotoxy(20,17);
printf("Record saved!\n\n");
fclose(fp);
gotoxy(20,19);
printf("Do you want to enter new product? Y / N : ");
scanf(" %c", &checker);
checker = toupper(checker);
i++;
system("cls");
}
while(checker=='Y');
return(0);
}