11 Replies - 167 Views - Last Post: 24 February 2012 - 12:52 PM Rate Topic: -----

#1 Crazy_Learner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 139
  • Joined: 25-July 09

Item not return correct value

Posted 24 February 2012 - 09:20 AM

so i have an item class that work with an inventory class, and i have created an item. If i were to check the item before adding it to the inventory it would show the name as potion and the count as 15. after i add it though it shows the count as 0 and i cant seem to figure out why.
changed compiler and problem went away. Found the issue to be due to included file by the ide


This post has been edited by Crazy_Learner: 25 February 2012 - 01:32 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Item not return correct value

#2 jimblumberg  Icon User is offline

  • member icon

Reputation: 3058
  • View blog
  • Posts: 9,305
  • Joined: 25-December 09

Re: Item not return correct value

Posted 24 February 2012 - 09:33 AM

First I recommend that you stop using the C-strings and start using std::string. You can not use assignment with C-strings, you will need to use strcpy() from the cstring header. Second in your item class you have a pointer variable that you never allocate any memory ( char* ItemName;). How many characters will this variable be able to hold?


Jim
Was This Post Helpful? 0
  • +
  • -

#3 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 390
  • View blog
  • Posts: 1,349
  • Joined: 31-December 10

Re: Item not return correct value

Posted 24 February 2012 - 09:48 AM

You should check to see what level your errors/warnings are at because your compiler should be giving you warnings about the "deprecated use" of plain char*. You should be able to increase the level or warnings/errors and you should always try to get rid of any warnings there might be.
Was This Post Helpful? 0
  • +
  • -

#4 Crazy_Learner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 139
  • Joined: 25-July 09

Re: Item not return correct value

Posted 24 February 2012 - 10:05 AM

View Postjimblumberg, on 24 February 2012 - 05:33 PM, said:

First I recommend that you stop using the C-strings and start using std::string. You can not use assignment with C-strings, you will need to use strcpy() from the cstring header. Second in your item class you have a pointer variable that you never allocate any memory ( char* ItemName;). How many characters will this variable be able to hold?


Jim


taking in account your suggestion and adjusting the code to fit the std::string, i still receive a count of zero, when adding the item. even though the char* was deprecated it was always return the proper name. although i still changed it. what my issue lies in is that i am giving the inventory 15 of the same item, and then when i check it there are 0
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5676
  • View blog
  • Posts: 22,537
  • Joined: 23-August 08

Re: Item not return correct value

Posted 24 February 2012 - 10:34 AM

I think it's time to take your "crazy learning" into the realm of debugging. On what OS and with what IDE are you developing?
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon

Reputation: 3058
  • View blog
  • Posts: 9,305
  • Joined: 25-December 09

Re: Item not return correct value

Posted 24 February 2012 - 10:35 AM

Show your current code, along with any warning/error messages exactly as they appear in your development environment.

Jim
Was This Post Helpful? 0
  • +
  • -

#7 Crazy_Learner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 139
  • Joined: 25-July 09

Re: Item not return correct value

Posted 24 February 2012 - 10:40 AM

View Postjimblumberg, on 24 February 2012 - 06:35 PM, said:

Show your current code, along with any warning/error messages exactly as they appear in your development environment.

Jim


Code is updated in first post and the the only error i get is from debug mode, not the compiler of segmentation access error and it points to the Item class getItemCount() method
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon

Reputation: 3058
  • View blog
  • Posts: 9,305
  • Joined: 25-December 09

Re: Item not return correct value

Posted 24 February 2012 - 10:45 AM

Quote

Code is updated in first post and the the only error i get is from debug mode, not the compiler of segmentation access error and it points to the Item class getItemCount() method

Please don't change the code in you previous posts, it causes the responses to your questions seem incorrect. This will seem confusing anyone reading this topic. Always post your modified code in as a new post in the topic.

You have an error message, post it. If you get a segmentation fault run the program through your debugger it should show you the line where the error is detected.

Jim
Was This Post Helpful? 0
  • +
  • -

#9 Crazy_Learner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 139
  • Joined: 25-July 09

Re: Item not return correct value

Posted 24 February 2012 - 10:50 AM

I can do that from now on, but the error was exactly what i posted:

Quote

segmentation access error and it points to the Item class getItemCount() method


which is accessed from the inventory class during the getInventoryItemCount method
Was This Post Helpful? 0
  • +
  • -

#10 Crazy_Learner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 139
  • Joined: 25-July 09

Re: Item not return correct value

Posted 24 February 2012 - 10:55 AM

View PostJackOfAllTrades, on 24 February 2012 - 06:34 PM, said:

I think it's time to take your "crazy learning" into the realm of debugging. On what OS and with what IDE are you developing?


im using Windows 7 x64 and Dev-C++
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is offline

  • member icon

Reputation: 3058
  • View blog
  • Posts: 9,305
  • Joined: 25-December 09

Re: Item not return correct value

Posted 24 February 2012 - 10:57 AM

And what is the line where the problem was detected? The debugger will show you the actual line. Since this is not a complete program there is no real way I can tell why you are getting a segmentation fault. So you need to show the exact error message along with providing the exact line the error was detected. Otherwise post a complete program and maybe I can help.



Jim
Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5676
  • View blog
  • Posts: 22,537
  • Joined: 23-August 08

Re: Item not return correct value

Posted 24 February 2012 - 12:52 PM

Dev-C++ 


Ugh...there are so many better options available than an old, buggy, unsupported IDE like Dev-C++. Especially when you need to debug. Try Visual C++ Express, or Code::Blocks, or the more up-to-date version of Dev-C++, wxDev-C++.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1