how to detrmine if a variable has a value in C++

Questions about programming languages and debugging
Post Reply
User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

how to detrmine if a variable has a value in C++

Post by maboroshi »

is there a way to check if a value has been assigned to a variable in C++ like php's isset() function

if so is this the correct way

Code: Select all

     if (total1 != 0) {
                     total1 += total1;
                     }
what I am doing is creating a continuous loop on each iteration it asks add or multiply if I replace the value that was added (total1), thats it its replaced I need a way to keep a reference to total1 in memory so I can than continue adding values to it over and over

Any ideas

Cheers

Maboroshi

User avatar
Nerdz
The Architect
The Architect
Posts: 1127
Joined: 15 Jun 2005, 16:00
18
Location: #db_error in: select usr.location from sucko_member where usr.id=63;
Contact:

Post by Nerdz »

Hmm usually with create variable with default value like -1 or 0... and checked that it's our default value...

Number: -1 or 0
String/char*: "" or specific for string: isempty()
pointer: 0
Give a man a fish, you feed him for one day.
Learn a man to fish, you feed him for life.

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

Wouldn't total1 != NULL work ? i mean "0" is a value after all.
"The best place to hide a tree, is in a forest"

User avatar
Nerdz
The Architect
The Architect
Posts: 1127
Joined: 15 Jun 2005, 16:00
18
Location: #db_error in: select usr.location from sucko_member where usr.id=63;
Contact:

Post by Nerdz »

nha, usually there is #define NULL 0 ...

Type* p = 0;

The address of p is 0 ans it's value is not set.
Give a man a fish, you feed him for one day.
Learn a man to fish, you feed him for life.

User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

ok

Post by maboroshi »

Ok here is my base code I want to keep a reference to total1 in memory on each successive loop but can't figure out how

Please explain if you help what you did

edit guess I should use suck-o code poster

http://code.suck-o.com/7

User avatar
Nerdz
The Architect
The Architect
Posts: 1127
Joined: 15 Jun 2005, 16:00
18
Location: #db_error in: select usr.location from sucko_member where usr.id=63;
Contact:

Post by Nerdz »

So each time total get a new value you want to keep the previous one? Why not using an array? :-k

EDITED:
I guess I don't really understand your problem... if it's a calculator you want to do you can do:

total1 += num1 + num2;

EDITED2:

Hmm I edited the code... but I don't think that what you have in mind... is it?
http://code.suck-o.com/8
Give a man a fish, you feed him for one day.
Learn a man to fish, you feed him for life.

User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

Alright

Post by maboroshi »

Alright Nerdz You win the prize

You solved my problem god that was so simple to ty buddy

:)

User avatar
Nerdz
The Architect
The Architect
Posts: 1127
Joined: 15 Jun 2005, 16:00
18
Location: #db_error in: select usr.location from sucko_member where usr.id=63;
Contact:

Post by Nerdz »

Glad I could help, if you need any other help feel free to ask :wink:
Give a man a fish, you feed him for one day.
Learn a man to fish, you feed him for life.

Post Reply