1

I am new to write code in c++ programmıng before I just work on java coding. I try to solve teh txt file as database. But I taken this error I search on internet I cound't find the exact answer ? Please if you know help me. Thanks.

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

void menu() {

    puts("1. List the products");
    puts("2. Add a new product");
    puts("3. Sell the new product");
    puts("4. Search by Barcode");
    puts("5. Exit");
}
int main(int argc, char *argv[]) {
    FILE *fProduct;
    char name[20];
    int quantity;
    int barcode;
    double price;
    menu();
    fProduct = fopen("Product.txt", "a+");
    if (fProduct != NULL) {
        while (!feof(fProduct))
        {
            printf("Name :");
            scanf("%s" , name);
            printf("Quantity :");
            scanf("%d", &quantity);
            printf("Barcode Number :");
            scanf("%d", &barcode);
            printf("Price :");
            scanf("%lf", &price);
            printf("Are there any product ???");
        }
    }
    fclose(fProduct);
}

enter image description here

smac89
  • 39,374
  • 15
  • 132
  • 179
  • 1
    Please choose a language, either `C` or `C++`. What you wrote is practically all `C`. – PaulMcKenzie Oct 24 '17 at 22:23
  • I wrote C++ Language – Furkan52038 Oct 24 '17 at 22:25
  • What will happen if `fProduct` is NULL? – Jim Lewis Oct 24 '17 at 22:26
  • 1
    @Furkan52038 Well, if it's C++. get another book to learn from. What you're learning is `C` programming. Also, what if `name` is greater than 20 characters, like "Grandma's Best HomeMade Apple Pie"? For C++, `cin` and `std::string` would have been used, not char arrays. – PaulMcKenzie Oct 24 '17 at 22:27
  • it will create fProduct file, but the file open method ("a+") means that is if the file is created just write if not created and create file and write @JimLewis – Furkan52038 Oct 24 '17 at 22:30
  • Sorry Just I want to write C code but In these few days I am using a lot type of programming lang. @PaulMcKenzie – Furkan52038 Oct 24 '17 at 22:32
  • Since you say this is C++ and indeed your compiler shows it is supposed to be C++, perhaps you should be doing `if (fProduct != nullptr)`. This is the C++ way and it seems your compiler is compatible with C++11 – smac89 Oct 24 '17 at 22:53
  • 1
    Won't solve this problem (or it shouldn't, anyway) but it might help solve the next one: [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – user4581301 Oct 24 '17 at 22:58

3 Answers3

2

fclose applied a parameter validation assertion.

The fclose function closes stream. If stream is NULL, the invalid parameter handler is invoked, as described in Parameter Validation. ...

In Debug builds, the invalid parameter macro usually raises a failed assertion and a debugger breakpoint before the dispatch function is called. ...

Move your fclose call to be within the if block that checked for NULL.

jxh
  • 69,070
  • 8
  • 110
  • 193
  • I move the fclose method then It is given the same error @jxh – Furkan52038 Oct 24 '17 at 22:38
  • It may be that `scanf` is asserting on a `NULL` parameter. [Mike's answer](https://stackoverflow.com/a/46921103/315052) suggests your program did not compile, and you are actually running an old program. So your code may not correspond to the error you are seeing. – jxh Oct 25 '17 at 18:36
1

Based on your screenshot you have a linker error so you may not be running the correct version of your code. Based on the error message I am guessing that the problem is scanf loading data into the name parameter. 1) do a clean build and make sure you do not get any build or linker errrors. 2) if the error still happens then press retry on the screen and the debugger will show you the line that is causing the problem. Use the stack window to find your code on the stack.

Mike
  • 3,462
  • 22
  • 25
0

If you want it in C++, it is better to write C++ code instead of C code:

// The headers to include are different
#include <iostream>
#include <fstream>
#include <string>

void menu();

int main() {
  using namespace std;

  ofstream fProduct;

  string name;
  int quantity;
  int barcode;
  double price;

  // your menu, but for now we handle only the 
  // case 2: add a new product.
  menu();

  // as for now we deal only with the insertion of new 
  // products
  // Let's open a new file using the C++ standard library
  fProduct.open("Product.txt", ios::out | ios::app | ios::ate);
  // If the write can be opened we continue, otherwise 
  // we skip the next part of the code
  if (fProduct.is_open()) {
    string conts = "y";
    while (conts == "y") {
      cout << "Name: ";           cin >> name;
      cout << "Quantity: ";       cin >> quantity;
      cout << "Barcode Number: "; cin >> barcode;                  
      cout << "Price: ";          cin >> price;

      // Here we write in the file the information the user passed us.
      // since we are getting information that should be written as 
      // sequence of char in the file, we could avoid to use 
      // int/double variables. Let's write in the file, comma 
      // separated
      fProduct << name << "," << quantity << "," << barcode << "," << price << endl;

      // Here we have some way to interrupt the loop
      cout << "Add another product? [y/n]"; 
      cin >> conts;
    }
    // Finally we close the file. (only if it was open...)
    fProduct.close();
  }

  return 0;
}

// Your menu function using the standard streams
void menu() {
  using namespace std;
  cout << "1. List the products" << endl;
  cout << "2. Add a new product" << endl;
  cout << "3. Sell the new product" << endl;
  cout << "4. Search by Barcode" << endl;
  cout << "5. Exit" << endl;
}

and if you want it C it's better to use pure C code:

#include <stdio.h>
#include <string.h>

void menu();

int main() {

  FILE *fProduct;

  // again, all this variable could be simply char[].
  char name[20];
  int quantity, barcode;
  double price;

  // As for now our code handles only the insertion of
  // new element, not the other choices of the menu
  menu();

  // Do not check if it is different than NULL,
  // check only if it actually is something... 
  if (fProduct = fopen("Product.txt", "a")) {
    char conts = 'y';
    while (conts == 'y') {
      printf("Name :");           scanf("%s",  name);
      printf("Quantity :");       scanf("%d",  &quantity);     
      printf("Barcode Number :"); scanf("%d",  &barcode);
      printf("Price :");          scanf("%lf", &price);

      // Let's write in the file 
      fprintf(fProduct, "%s,%d,%d,%lf\n", name, quantity, barcode, price);

      getchar(); // chomps the last newline
      printf("Add another product? [y/n] "); 
      conts = getchar(); 
    }
    // At the end of our loop we need to close the file.
    fclose(fProduct);
  }

  return 0;
}

void menu() {
  puts("1. List the products");
  puts("2. Add a new product");
  puts("3. Sell the new product");
  puts("4. Search by Barcode");
  puts("5. Exit");
}
Matteo Ragni
  • 2,837
  • 1
  • 20
  • 34