简体   繁体   English

调试断言在运行时c ++上失败

[英]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. 在我刚开始编写java代码之前,我是新手在c ++programmıng中编写代码。 I try to solve teh txt file as database. 我尝试将teh txt文件解析为数据库。 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);
}

在此输入图像描述

fclose applied a parameter validation assertion . fclose应用了参数验证断言

The fclose function closes stream . fclose函数关闭stream If stream is NULL , the invalid parameter handler is invoked, as described in Parameter Validation. 如果streamNULL ,则调用无效参数处理程序,如参数验证中所述。 ... ...

In Debug builds, the invalid parameter macro usually raises a failed assertion and a debugger breakpoint before the dispatch function is called. 在Debug版本中,无效参数宏通常会在调用调度函数之前引发失败的断言和调试器断点。 ... ...

Move your fclose call to be within the if block that checked for NULL . fclose调用移动到检查NULLif块内。

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. 基于错误消息,我猜测问题是scanf将数据加载到name参数中。 1) do a clean build and make sure you do not get any build or linker errrors. 1)做一个干净的构建,并确保你没有得到任何构建或链接器错误。 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. 2)如果错误仍然发生,则按屏幕上的重试,调试器将显示导致问题的行。 Use the stack window to find your code on the stack. 使用堆栈窗口在堆栈上查找代码。

If you want it in C++, it is better to write C++ code instead of C code: 如果你想在C ++中使用它,最好编写C ++代码而不是C代码:

// 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: 如果你想要C,最好使用纯C代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM