0

I'm pretty new in the coding world so i can't seem to understand this error. I'm creating a product management program wherein you enter product details and put that data on a file and then view it from the file via cmd but this error keeps popping up whenever I use fprintf or fputs.

my code:

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;

struct product{
    string Id,Name,Brand;
    int type, price, quan;
    product *next;
};
int main(){
    product *a,*b,*c,*d,*e,*f;
    a = new product;
    b = new product;
    c = new product;
    d = new product;
    e = new product;
    f = new product;
    FILE *productRecord;
    productRecord = fopen("Product Record","r");
    int choice;
    cout << "\nProduct Record\n1)Add Product\n2)Update Product Details\n3)Delete Product\n4)View Product By Brand or Type\n5)Exit\n\nEnter Choice: ";
    cin >> choice;
        if(choice == 1){
            char tempId[11],tempName[60],tempBrand[60];
            int temp_type, temp_price, temp_quan;
            productRecord = fopen("Product Record","a");
            cout << "Enter Product ID No.(Maximum of 10 Digits): ";
            cin >> tempId;
            a -> Id = tempId;
            a -> next = b;
            cout << "Enter Product Name(No Special Character): ";
            cin >> tempName;
            b -> Name = tempName;
            b -> next = c;
            cout << "Types:Canned(1),Frozen(2),Drinks(3),Produce(4),Meat/Seafood(5),Cleaning(6)\nEnter Product Type(1-6): ";
            cin >> temp_type;
            c -> type = temp_type;
            c -> next = d;
            cout << "Enter Product Brand(No spaces): ";
            cin >> tempBrand;
            d -> Brand = tempBrand;
            d -> next = e;
            cout << "Enter Product Price: ";
            cin >> temp_price;
            e -> price = temp_price;
            e -> next = f;
            cout << "Enter Product Quantity: ";     
            cin >> temp_quan;
            f -> quan = temp_quan;
            f -> next = NULL;

            fprintf(productRecord, "%s", a -> Id); //this is the error





            fclose(productRecord);
        }
        else if(choice == 2){
        }
        else if(choice == 3){
        }
        else if(choice == 4){
        }
        else if(choice == 5){
        }
    return 0;
}
5
  • Why are you using FILE and related instructions with c++? Commented Mar 6, 2020 at 13:25
  • Please copy-paste the entire error message. Most likely, it states exactly what's wrong. Commented Mar 6, 2020 at 13:26
  • 3
    Please get a couple of good C++ books and learn C++ properly. In short, don't try to use C++ object with the old inherited C functions (and that's your problem... fprintf doesn't know about C++ std::string objects). Commented Mar 6, 2020 at 13:26
  • 1
    Too many pointers, too much C. Don't use whatever you're learning from for learning. Commented Mar 6, 2020 at 13:48
  • Oh okay, thank you for all the help much appreciated. I will try to study C++ properly. I'm just following my prof's lessons/lectures and I think he is very sketchy mixing C and C++. Anyway, thank you for the help. Commented Mar 12, 2020 at 3:39

1 Answer 1

1

The error message is pretty clear. You cannot use a string with fprintf.

fprintf(productRecord, "%s", a -> Id);

Needs to be changed to

fprintf(productRecord, "%s", a->Id.c_str());

Or, better yet, don't mix C and C++, and instead use ofstream

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.