more efficient method?

I do not know why my method is not working.
Last edited on
Hello,

first - learn to use forum. You have special option for code to write there. It's much more clear.

Second:

If you want to know about constructors, search in internet. Generally constructor creates instance of class and initializes its variables to some values.

Also, it looks like you didn't pay attention during your learning. Large portion of your code is not needed and could be done much more simply. For example checking for quit could have been done with few lines of code.

And you shouldn't duplicate variables; this code:
1
2
3
4

int numberOfItems;
numberOfItems = updatedInventory.getItemQuantity();
cout << "The current inventory level is " << numberOfItems << endl;


Could have been simplified to:
1
2
3

cout << "The current inventory level is " << updatedInventory.getItemQuantity() << endl;


Post code again but with tags, and we may help. Remember - debugging your own program is really good way to learn things.
You do not need to repost code, you can edit your post.
Your code has numerous problems:
1) Your dataOrdered() function is declared to return int, but you are trying to return std::string
2) Your forward declaration are never defined. AActually you don't need thaem.
3) You are trying to call member function without any object
4) You a trying to access dateCounteed and dateOrdered which wasn't defined anywhere.
Updated program.
stream extraction operations leave newline symbols in input buffer where it will be read by getline, checked as inappropriate input and ask user to input again.
You should use ignore() to make getline work as it should.
Thanks that works to get the loop from repeating with an empty string.
I still can not get my program to function properly.
For example I want to replace itemQuantity in the constructor with the input when the user chooses option O
Last edited on
Please, go to tutorial section of this site and go again through lessons. You miss very basics, and you can't do well in harder things if you miss basics.

For example, this function:

1
2
3
4
    int itemCount(int countOfItems)     // replace itemQuanity
    {
        setItemQuantity(countOfItems);
    }


And this function

1
2
3
4
    void setItemQuantity(int itemQuanityNow)
    {
        this->itemQuantity = itemQuanityNow;
    }


What is the purpose of creating public function that will call another public function?

In any place of your code, when you call
 
someInventory.itemCount(someInteger);


You can use
 
someInventory.setItemQuantity(someInteger);


The second one creates clear code, the first one makes many unneccesary operations (question for you: how many times will program call copy constructor of integer?).

Also, third thing about this function: it should return int. However, there is no return statement. Therefore, this function should either be of void type, or return something( I believe you wanted it to be of void type).

These three things show that you don't understand functions, which is required for you to advance.

Seriously, if you don't know why something doesn't work - first try to describe, step by step, what program is doing. Think like you had to describe it to someone who doesn't understand much of C++ .

You will see how many things your code has that aren't needed, and are making it less and less readable(check KISS in wikipedia).

For example, If I were you, I would start describing your code like this:

"First, I'm including many headers:
<cstdlib>, because I want to use X function(s).
<iostream> to use cin and cout.
<time.h> because I really like to include things I don't need.
Later, I declare inventory Class.
It has two private members:
and Integer called itemQuantity, which I immediately set to zero because I have no idea about declaring members of a class. It's purpose is to [...], and
string dateCounted, which I also set to be empty, because yet again, I have no idea what I'm doing. I think I should have paid attention to C++ lessons."

I just started describing your code, and found few things that could be corrected. By the time you reach main function, you will have whole bunch of things to fix there, not even talking about main function( why check for every possibility of "quit"?? What if user had to write some spell, like "Asamadarathurkya!"(random word). Would you check every possibility again? If you include something, try to use it at least! Check toupper function from <cctype>!).

Fixing your code by yourself is really important at the beginning of your C++ journey.
You not only learn how to find and fix bugs, but you also make sure you know what you are doing, you can follow flow of program and you learn - and that is your priority now.

Before asking questions and saying your code isn't working, read tutorials again and try to fix your code by yourself. Find out WHY it isn't working(this code is actually small, so should be simple to debug), and if you don't know how to fix it - after trying - go on and ask, with explanation of error. If you follow my advice, you will fix 90%(at least!) of bugs by yourself.

Good luck!

PS. Oh, and one more advice: when you are learning, never copy code. If you are copying, you aren't learning.
Topic archived. No new replies allowed.