Iv only started a Persistent Data course and im struggling. I have to display customer and sales details.. but unsure what way to go about it.
Here is my layout so far..
I would include a variable named customer_id, and place it in your sales struct. Every sale will have the customer_id variable populated with the id of the customer who made that purchase. It's called a foreign key. This way, you can correlate the purchases with the customers who made them. This is common in databases and the concepts works the same here.
Also you open customers.txt in main() needlessly because you have it set to open when the user selects 1 as well.
I'm not sure what your main issue is, if you are trying to display the lines in the file, you can use getline();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
void display_cust(void)
{
string line;
fp=fopen("customers.txt","rb");
if(!fp)
{
printf("unable to open file\n");
} else {
while (fp.good())
{
getline(fp, line, '//delimiter goes here//');
cout << line << endl;
}
}
}
make sure to include #include <iostream> at the top of your file, this will allow you to use cout, cin, etc.... in your file.
cout = console output
<< line = output the variable 'line's contents to the console
<< endl = make a newline in the console.
cout << line << endl would display each field in the text file, where getline(fp, line, ' ') would read each piece of text into the string variable 'line', between the spaces in the line of text in the file.
If your customers file looked like this:
1,bob,smith,27,male,123 foo lane tampa fl,
2,jane,smith,23,female,123 foo lane tampa fl
then you would use
getline(fp, line, ',')
instead, because a comma is your delimiter, and each pass through the while loop will read the line of text within the two commas and put that into the string variable 'line'
Nothing. You can think of string line as a temporary box that holds items for each iteration of the while loop.
so if your customer file contains:
1,bob,smith,27,male,123 foo lane tampa fl,
than your code will be doing this:
1 2 3 4 5
while (fp.good())
{
getline(fp, line, ',');
cout << line << endl;
}
produces:
while (determined your file has more to read)
1 // this is line after 1st iteration of while loop
while (determined your file has more to read)
bob // this is line after 2nd iteration of while loop
while (determined your file has more to read)
smith // this is line after 3rd iteration of while loop
while (determined your file has more to read)
27 // etc..
while (determined your file has more to read)
male // etc..
while (determined your file has more to read)
123 foo lane tampa fl // etc..
while (determined your file has NO more to read)
I have to go to work, everything here is enough to get you going. Remember, this web-site has a complete and very useful reference for everything C++ as well as tutorials: http://www.cplusplus.com/reference/ and google is your friend.
I worked away with the code and got 2 functions done so far, there's no errors but im still not 100% on it.. Anyways i just have 2 functions left(Adding sales and customers) Heres what iv got so far..
printf("Please enter\n1. to search a customer or sale\n2. to add a customer\n3. to add a sale\n4. binary search\n");
scanf("%5d", &num1, &num2, &num3, &num4);
if ( fpcust == NULL)
{
printf("File cound not be opened.");
exit(0);
}
else
{
len_string = strlen(searchID);
while (!feof ( fpcust ) )
{
for (i = 0; i < len_string; i++)
{
temp[i] = fgetc ( fpcust );
}
temp[i] = '\0';
//strcmp used for comparing both strings
if ( strcmp ( searchID, temp ) == 0)
{
printf("The ID was found \n");
fclose( fpcust);
system ("PAUSE");
exit(1);
}
printf("Please enter\n1. to search a customer or sale\n2. to add a customer\n3. to add a sale\n4. binary search\n");
scanf("%5d", &num1, &num2, &num3, &num4);
if ( fpcust == NULL)
{
printf("File cound not be opened.");
exit(0);
}
else
{
len_string = strlen(searchID);
while (!feof ( fpcust ) )
{
for (i = 0; i < len_string; i++)
{
temp[i] = fgetc ( fpcust );
}
temp[i] = '\0';
//strcmp used for comparing both strings
if ( strcmp ( searchID, temp ) == 0)
{
printf("The ID was found \n");
fclose( fpcust);
system ("PAUSE");
exit(1);
}