Furkan52038
Furkan52038

Reputation: 11

Debug Assertion Failed on c++ in run time

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

Upvotes: 1

Views: 1741

Answers (3)

Matteo Ragni
Matteo Ragni

Reputation: 2956

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");
}

Upvotes: 0

Mike
Mike

Reputation: 3460

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.

Upvotes: 1

jxh
jxh

Reputation: 70382

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.

Upvotes: 2

Related Questions