for the program im writing this is what i have to do:

i have to write a C program that maintains a table of productID and price in a .txt file.

The following menu shows the functionality of the program:
1. Display all products
2. Search product by ID
3. Sort the product list by price
4. Sort the product list by ID
5. List the average price
6. List max and min price
7. Exit

Specifications:
1. The product IDs and product prices are stored in a table format in a file named
products.txt. Create a text file in the same folder as your .c file and type in two columns
(ID and price) separated by a tab. product ID and price are integers
2. Main program will be main.c. Define the following functions in func.h and func.c files
(refer to lab 5, question 1).
 displayMenu( ): displays the menu options 1. -7. on the screen, and asks the user
to ‘Enter an option:’.
 readFile ( ) – opens the file ‘products.txt’ and reads contents from the file is read
in to a 2-D array named ‘products’.
 displayProducts ( ) – displays the contents of the products list
 searchProduct( ) – searches for a product by its ID and displays whether it is
found or not.
 sortProductsPrice ( ) – sorts the products list in ascending order of the price, and
displays the sorted list
 sortProductsID ( ) – sorts the products list in ascending order of the ID, and
displays the sorted list
 averagePrice ( ) – calculates and displays the average price
 maxMinPrice( ) – calculates the maximum and minimum price in the list and
displays the max and min values

so far i have this i dont know how to take input from a .text file helpp!!

#include <stdio.h>
#include <stdlib.h>

void displayMenu();
void readFile();
void displayProducts();
void searchProduct();
void sortProductsPrice();
void sortProductsID();
void averagePrice();
void maxMinPrice();

int main(int argc, char *argv[])
{   
    int userChoice = 0;
    printf("Inventory Program\n\n");
    displayMenu();    

     printf(" \nPick an option\n");
    scanf("%d",&userChoice); 

     switch(userChoice)
     {
        case 1: displayProducts(); 
        break;
        case 2: readFile();  
        break;               
        case 3: searchProduct();     
        break; 
        case 4: sortProductsPrice();
        break; 
        case 5: sortProductsID();   
        break; 
        case 6: averagePrice();      
        break; 
        case 7: maxMinPrice();       
        break; 
    }

  system("PAUSE");  
  return 0;
}
void displayMenu()
{    
       printf("1 To Display Products Enter 1\n");
       printf("2 To Extract Products From Txt File Enter 2\n");
       printf("3 Search By  Product I.D Enter 3 \n");
       printf("4 Search And Sort Product By Prices 4\n");
       printf("5 Search And Sort By  Product I.D Enter 5\n");
       printf("6 Search Average Price Enter 6\n");
       printf("7 Calculate Maximum And Minimum Prices And Displays Them 7\n");

}
void readFile()
{

}
void displayProducts()
{

}
void searchProduct()
{

}
void sortProductsPrice()
{

}
void sortProductsID()
{

}
void averagePrice()
{

}
void maxMinPrice()
{

}

Have you created the products.txt file yet, with the necessary spec's as you describe here?

  1. The product IDs and product prices are stored in a table format in a file named
    products.txt. Create a text file in the same folder as your .c file and type in two columns
    (ID and price) separated by a tab. product ID and price are integers

You can't read a file's data correctly, until you have the file. As I understand it, you will manually type this data into the file, and keep it. I would make a backup copy, just in case - you don't want to have to enter it again if it gets scrambled by your program.

You'll need a FILE *pointer, (of course, *) as use an fopen() to open the file in read mode. Then fscanf() should be all you need, since the data is in a strict format, which will not change.

Give that a try.

thanks for the respounse
this is what my list looks like
products.txt

headphones     10
mic            15
speakers       25
screen         200
mouse          20

i know i have to use fopen i dont know the sytax to using it tho

Edited 4 Years Ago by omarelbeik

That won't do, doesn't meet specs. Get a cup of coffee or splash some water on your face, or soemthing. You need to get firing on all cylinders here!

Product ID and price, are both integers, with a tab between them, in the data file.

FILE *fp;
fp = fopen("products.txt", "rt");

if(!fp) {
   printf("Error! File not opened\n");
   return 0;
}


//when done reading the file
fclose(fp);

fp is short for file pointer, but the name can be whatever you like, as long as it's not a keyword in C.

You should work through a few C tutorials. Many are available on the net. One of them is here, (click on the C Tutorial tab)
http://cboard.cprogramming.com/c-programming/

thank you so much for the help i appreciate it i got it... i think

#include <stdio.h>
#include <stdlib.h>

void displayMenu();
void readFile();
void displayProducts();
void searchProduct();
void sortProductsPrice();
void sortProductsID();
void averagePrice();
void maxMinPrice();

/*
product.txt

1   10
2   20
3   30
4   40
5   50
*/


int main(int argc, char *argv[])
{   

     int row, col, x;  
     int userChoice = 0;
    printf("Inventory Program\n\n");
    readfile()   
     displayMenu();    

     printf(" \nPick an option\n");
    scanf("%d",&userChoice); 

     switch(userChoice)
     {
        case 1: displayProducts(); 
        break;
        case 2: readFile();  
        break;               
        case 3: searchProduct();     // searches for a product by its ID and displays whether it is found or not. 
        break; 
        case 4: sortProductsPrice();// sorts the products list in ascending order of the price, and displays the sorted list
        break; 
        case 5: sortProductsID();   // sorts the products list in ascending order of the ID, and displays the sorted list
        break; 
        case 6: averagePrice();     // calculates and displays the average price 
        break; 
        case 7: maxMinPrice();       // calculates the maximum and minimum price in the list and displays the max and min values
        break; 
    }

  system("PAUSE");  
  return 0;
}
void displayMenu()
{    
       printf("1 To Display Products Enter 1\n");
       printf("2 To Extract Products From Txt File Enter 2\n");
       printf("3 Search By  Product I.D Enter 3 \n");
       printf("4 Search And Sort Product By Prices 4\n");
       printf("5 Search And Sort By  Product I.D Enter 5\n");
       printf("6 Search Average Price Enter 6\n");
       printf("7 Calculate Maximum And Minimum Prices And Displays Them 7\n");

}
void readFile()
{
    FILE *FP
    Char infile [10]
    fp = fopen ("products.txt", "r+");

    for (row = 0; row < 2; row++)
    {
        for (col = 0; col <= 5; col++)
        {
            x = fscanf (fp, "%d", &n);
            if (x == EOF)
                break;
                products[row][col] = n;
        }
    }
    fclose (fp);
    products = row;
    price = col;


}
void displayProducts()
{
    printf("product 1 a mouse\n");
   printf("product 2 a web cam\n");
   printf("product 3 a keyboard \n");
   printf("product 4 a camera\n");
   printf("product 5 a speakers\n");

    for (row = 0; row < 2; row++)
    {
        for (col = 0; col <= 5; col++)
        {
            printf("%c", products[row][col])

        }
        printf("\n");
    }
}
void searchProduct()
{
    int choice;
    printf {"enter product number you want to search for\n"};
    scanf("%d",&choice);
    if choice == 

}
void sortProductsPrice()
{

}
void sortProductsID()
{

}
void averagePrice()
{

}
void maxMinPrice()
{

}

First thing that I see is line 78 uses x, but x needs to be declared within that function.
Check your syntax with frequent compiles, as you work along. It's hell having done a few hundred lines of code, only to find when you compile them, you have a bucketfull of errors.

This article has been dead for over six months. Start a new discussion instead.