FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
C++ PROGRAMMIN' r u any good?
Goto page Previous  1, 2, 3
 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    The Ville.org Half-Life Gaming Community Forum Index -> General Ramblings
View previous topic :: View next topic  
Author Message
South-American Fire Ant
Registered User


Joined: 25 Feb 2002

Posts: 26

PostPosted: Mon May 06, 2002 11:07 pm    Post subject: Wowie! Reply with quote

sheeeze this gots lots of responses which is really really good cuz i definately have a lot of problems in this class!!

uhhhh... lets see so many posts!

First to all who said something along the lines of "just get rid of the * ! then it works fine"
-- yes i discovered this, but then the exercise does what is not intended to. technically i could make the program just say "HI" and i would run fine, but not accomplish what is suppose to... which i dont' really know what it is. But i think i need to keep the pointers in!


This exercise is called a "bug buster", we get (i didnt' write this) code that usually has a stupid error in it, only i'm to st00pid to even know what it is... i'm working on it though!


Dahak
-- yes, java may be greater than C++, but i'm getting graded in this C++, so it dominates my life right now, i'm not even getting C++!


The ERROR!

"C:\homework\main.cpp(10) : error C2040: 'pVar' : 'int' differs in levels of indirection from 'int *'"

--uhhh... i'm sorry for my incredible lack of knowledge here, but isn't 'indirection' the way that the program accesses data or something....... i'm lost


Kilaak-
m3 2.

----------------------------------------------------------------
#include <iostream.h>
int main()
{
int SomeVariable = 5;
cout << "SomeVariable: " << SomeVariable << "\n";

int *pVar = & SomeVariable;

int pVar = 9;
cout << "SomeVariable: " << *pVar << "\n";

//***** do i need a (delete pVar;) here? *****

return 0;
}
----------------------------------------------------------------

oh well ill keep working on it! Thanks a lot guys! MORE TO COME STAY TUNED!


j00a4r phaiaer 4nt,
m3
Back to top
View user's profile Send private message AIM Address
South-American Fire Ant
Registered User


Joined: 25 Feb 2002

Posts: 26

PostPosted: Mon May 06, 2002 11:09 pm    Post subject: Reply with quote

you know what i'm just really pissed at it right now...

off to Ville III!
Back to top
View user's profile Send private message AIM Address
FingerDemon
Registered User


Joined: 24 Aug 2001
Location: Virginia
Posts: 551

PostPosted: Tue May 07, 2002 6:29 am    Post subject: Reply with quote

Fire Ant,

I think the main thing here is probably pointers. As others have said we don't know what the program is trying to do exactly. But I'm going to guess it is trying to create a normal memory allocated variable (and assign it a value like 5)
Then create a pointer variable (and assign in a value like 9).
Then assign the pointer variable the "value" of the memory allocated variable.
So, you DON'T want to assign it as a constant as someone said. Not because that won't compile, but because it would defeat the purpose of changing the value later. It would set forever as five and never change.

The key here is really to know how to declare a pointer variable, assign a pointer variable a value, change that value to something new and then access that pointer varaible to display the new value for display on the screen.

Look in your book for the section on pointers and I bet they go into exhaustive detail on those issues. Then check your code to make sure its done the same way. I'm confident it will be clear in your book because the majority of errors in C++ are from pointer mistakes, so it makes sense for them to cover it well in the beginning. If your book sucks, look online for C++ tutorials, there is tons of that kind of stuff, maybe something will address this better than your book.

Also, as I said, I don't know C++ well, but I do think you are using the int declaration too many times for the same variable. That might be the "false" error right there, but I don't have a compiler to test it for you.

Good luck

<TVB>FingerDemon-VS

FYI, Pointers are very powerful but can allow you to make pretty horrendous mistakes. It is thought that pointer errors are the cause of many "full machine lockup" errors in Windows. At least the earlier versions of Windows before NT and XP. That's why Java doesn't have pointers. But then Java isn't nearly as efficient either, so nobody's getting a free ride.
Back to top
View user's profile Send private message Send e-mail
Dominator
Registered User


Joined: 18 Apr 2002
Location: Unknown
Posts: 411

PostPosted: Tue May 07, 2002 10:56 am    Post subject: Re: C++ PROGRAMMIN' r u any good? Reply with quote

South-American Fire Ant wrote:
#include <iostream.h>
int main()
{
int SomeVariable = 5;
cout << "SomeVariable: " << SomeVariable << "\n";

int *pVar = & SomeVariable;

int pVar = 9;
cout << "SomeVariable: " << *pVar << "\n";


return 0;
}


does anyone know whats wrong with this code?
i'm in HS and i suck at C++.

j0r phiaer anth,
m3


as you are going down you list of codes this caught my attention
int *pVar = & SomeVariable; didnt seem to make much since, i would leave that out or you might have to modify something within
Back to top
View user's profile Send private message AIM Address
Lin Sivvi Defiant Hero
Registered User


Joined: 17 Jan 2002
Location: Some small boring city
Posts: 1205

PostPosted: Tue May 07, 2002 1:18 pm    Post subject: Reply with quote

Fireant, can you please read my original post in this thread. It has a source that works, can you tell me if that is what you're goal with this code is?
Back to top
View user's profile Send private message Send e-mail AIM Address
South-American Fire Ant
Registered User


Joined: 25 Feb 2002

Posts: 26

PostPosted: Tue May 07, 2002 1:48 pm    Post subject: I GOT IT! Reply with quote

#include <iostream.h>
int main()
{
int SomeVariable = 5;
cout << "SomeVariable: " << SomeVariable << "\n";

int *pVar = & SomeVariable;



int pVar = 9; THIS LINE SHOULD BE *pVar = 9;

// it must be like this in order to change the value of pVar using a pointer.

cout << "SomeVariable: " << *pVar << "\n";


return 0;
}


Thx 4 all ur help!

o and i dont' even have a book. So i'm using internet resources! including this forum!

j0004ar paehiir anit,
m3
Back to top
View user's profile Send private message AIM Address
South-American Fire Ant
Registered User


Joined: 25 Feb 2002

Posts: 26

PostPosted: Tue May 07, 2002 1:50 pm    Post subject: c++ Reply with quote

pish...

Last edited by South-American Fire Ant on Tue May 07, 2002 2:55 pm; edited 1 time in total
Back to top
View user's profile Send private message AIM Address
BitterBeerFace
Registered User


Joined: 17 Jan 2002
Location: Mankato, MinneSNOWta
Posts: 1416

PostPosted: Tue May 07, 2002 2:20 pm    Post subject: Reply with quote

Hey, thanks for answering the question Lin, next time the IRS calls me, I'll forward it to you, too

Yes, I did just hold a medkit out and 'naded a teammate... I noticed that a long time ago, and decided that since I'm now in TVB and such an all around 1337 player, a medkit suicide would be an appropriate signature pic
Back to top
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger
South-American Fire Ant
Registered User


Joined: 25 Feb 2002

Posts: 26

PostPosted: Tue May 07, 2002 2:58 pm    Post subject: Reply with quote

Lin,

your first code was right!

the line -- int pVar = 9;
should be -- *pVar = 9;


the goal i believe was to modify the data using pointers, not just declaring a constant. I think.

anyway gj!

hj0ra phiear ant,
n3
Back to top
View user's profile Send private message AIM Address
Lin Sivvi Defiant Hero
Registered User


Joined: 17 Jan 2002
Location: Some small boring city
Posts: 1205

PostPosted: Tue May 07, 2002 3:06 pm    Post subject: Reply with quote

I don't feel so stupid anymore
Back to top
View user's profile Send private message Send e-mail AIM Address
ApacheEyes
Registered User


Joined: 22 Aug 2001
Location: U.K.
Posts: 498

PostPosted: Wed May 08, 2002 12:15 pm    Post subject: Reply with quote

try this

#include <iostream.h>
int main()
{
int SomeVariable = 5;
cout << "SomeVariable: " << SomeVariable << "\n";

//int *pVar;
int *pVar = &SomeVariable;


*pVar = 9;
cout << "SomeVariable: " << SomeVariable << "\n";


return 0;
}

I changed the second output, so that when you assign the 9 to *pVar it changes the value at "SomeVariable" because thats where the address *pVar is pointing too.

Ap.
_________________
Death smiles at everyone, All a man can do is smile back.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    The Ville.org Half-Life Gaming Community Forum Index -> General Ramblings All times are GMT - 6 Hours
Goto page Previous  1, 2, 3
Page 3 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group