Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6613647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:14:35+00:00 2026-05-25T20:14:35+00:00

I want to write a little program which should be used in supermarkets. everything

  • 0

I want to write a little program which should be used in supermarkets. everything is fictitious and it’s only for learning purposes.
However, The tool generate a new data for every new article. in the data there are 2 lines, the name and the prise.
The data is named as the article number of the product. So the user enter a articlenumber and the tool looks for a data with this number, if it found it, it reads the 2 lines and initiates the variables.
But for some reasons it does not convert and copy the strings correctly.
here is the part which loads the data.

int ware::load()
{    
    string inhalt;

    cout << "please insert article number" << endl;
    cin  >> articlenumber;

    productname.open(articlenumber, ios::in);

    if (!productname.is_open())
    {
        cout << "can't find the product." << endl;
        return 1;
    }

    if (productname.is_open())
    {
        while (!productname.eof())
        {
            getline(productname, inhalt);
            strcpy(name,inhalt.c_str());

            getline(productname, inhalt); 
            price = atoi (inhalt.c_str());

            cout << inhalt << endl;
        }

        warenname.close();    
    }

    cout << endl << endl <<
        "number: " << inhalt <<
        "  preis: " << price <<
        "  name: " << name <<
        endl << endl; //this is a test and will be deleted in the final

}

hope you can help me!

Here is the class:

    class ware{

private:
    char     articlenumber[9];
    char     name[20];
    int      price;
    fstream  warennamefstream;
    ifstream warenname;

public:
    void newarticle(); //this to make a new product.
    void scan();       //this to 'scan' a product (entering the article number ;D)
    void output();     //later to output a bill
    int  load();       //load the datas.


};

hope everything is fine now.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:14:36+00:00Added an answer on May 25, 2026 at 8:14 pm

    First, you have a using namespace std; somewhere in your code. This occasionally leads to subtle bugs. Delete it. ( Using std Namespace )

    int ware::load()
    {    
        string inhalt;
    
        cout << "please insert article number" << endl;
        cin  >> articlenumber;
    

    The type of articlenumber is incorrect. Declare it std::string, not char[]. ( What is a buffer overflow and how do I cause one? )

    productname.open(articlenumber, ios::in);
    

    There is no reason to have an ifstream lying around waiting to be used. Also, there is no point in providing ios::in — it is the default. Just use the one-argument form of the ifstream constructor.

    if (!productname.is_open())
    {
        cout << "can't find the product." << endl;
        return 1;
    }
    

    Don’t bother checking to see if the file opened. Your users don’t care if the file was present or not, they care whether the file was present AND you retrieved the essential data.

    if (productname.is_open())
    {
        while (!productname.eof())
        {
            getline(productname, inhalt);
            strcpy(name,inhalt.c_str());
    
            getline(productname, inhalt); 
            price = atoi (inhalt.c_str());
    
            cout << inhalt << endl;
        }
    
        warenname.close();    
    }
    

    This loop is just wrong.

    • Never invoke eof(). It doesn’t do what you think it does, and will cause bugs.
    • Why is this a loop? Aren’t there only two lines in the file?
    • There is no point in calling close. Just let the file close when the istream goes out of scope.
    • Why is warename different than productname?
    • Don’t store your data in char[]. This is the 21st century. Use std::string.

    .

        cout << endl << endl <<
            "number: " << inhalt <<
            "  preis: " << price <<
            "  name: " << name <<
            endl << endl; //this is a test and will be deleted in the final
    
    • Never use endl when you mean to say '\n'. Each of those endl manipulators invokes flush, which can be very expensive. ( What is the C++ iostream endl fiasco? )
    • You forgot to return a value.

    Try this instead:

    int ware::load()
    {    
        // This declaration should be local
        std::string articlenumber;
        cout << "please insert article number" << endl;
        cin  >> articlenumber;
    
        // This declaration should be local
        std::ifstream productname(articlenumber.c_str());
    
        // These declarations can be class members:
        std::string name;
        int price;
        std::string number;
        if(getline(productname, name) &&
            productname>>price &&
            productname>>number)
        {
            cout << "\n\n" <<
                "number: " number <<
                "  preis: " << price <<
                "  name: " << name <<
                "\n\n"; //this is a test and will be deleted in the final
            return 0;
        } else {
            cout << "can't find the product." << endl;
            return 1;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a little app for myself which notifies me if there
I want to write a little program that transforms my TeX files into HTML.
I am learning python and I am challenging myself to write a little program
I want to write a little vnc similar program that moves the Mac OS
I want to write a little DBQuery function in perl so I can have
I have SBCL running on a Ubuntu machine. I want to write a little
Hello I want write my own desktop sharing application in Java. The application should
I have a question. I want to write a chess like program applying the
I wrote a little program, which is working like a ping command(i use ICMPSendEcho2),
I'm writing a little program, and here is what it should do. In the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.