Solved

"Protecting" variable values..

Posted on 2011-03-25
7
355 Views
Last Modified: 2012-05-11
I have read some initial values from a file, and put these values into variables.
All values in the file are positive (greater than zero).
Example:
int Value1 =  5   (read from file)
int Value2 =  3   (read from file)
int Value3 =  4   (read from file)

I want to be able to add or subtract values from these initial values (subtract with 1 or add with 1), but when the value reaches zero (NULL) or exceeds the value read from file the add/subtract operation should be aborted (or stopped).

One way of doing this is (of course) to have another set of values called InitValue1, initValue2 and InitValue3, put the initial values in each of them and then compare each of them for each add/subtract operation.

// Read from file -> InitValue1, initValue2, initValue3;
Value1 = initValue1;
value2 = initValue2;
value3 = initValue3;

// Add
if (Value1++) > initValue1   -> Abort operation

// Subtract
if (Value1--) = 0  -> Abort operation

Are there some intrinsic functions or other "smart" ways to achieve this goal in Visual C++ 2003 ?

0
Comment
Question by:toyboy61
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 5

Expert Comment

by:TomasP
ID: 35219385
If you load initValue1 from a file then assign that value to Value1 and add one to the value in Value1 won't the result always be greater than the initial value and the operation abort?

// Add
if (Value1++) > initValue1   -> Abort operation

0
 
LVL 40

Accepted Solution

by:
evilrix earned 300 total points
ID: 35219484
Just create yourself a bounded int class.
http://accu.org/index.php/journals/313
0
 

Author Comment

by:toyboy61
ID: 35219531
TomasP: You're right about that, but the actual situation is more complex than it looks.....

When the program starts it displays three columns and twelve rows with buttons.
Each row will have three possible choices (buttons) marked A, B and C.
Then you could have 7 possible combinations of choices:
- Single A     (stored in another array as binary value 4)
- Single B     (binary value 2)           ->     2
- Single C     (binary value 1)          ->     1
- A and B      (binary value 4+2)      ->     6
- A and C      (binary value 4+1)      ->     5
- B and C      (binary value 2+1)      ->     3
- A,B and C  (binary value 4+2+1)  ->     7
 
Binary:
A B C
4 2 1

The values read from the file tells how many triples (A-B-C), how many doubles (A-B,A-C,B-C) or
singles (A,B or C) the program will accept.

Value1 = number of triples allowed
Value2 = number of doubles allowed
Value3 = number of singles allowed

If we number the rows 1 to 12 (C++ style: 0 to 11) I want the program to do the following:

- If the user chooses A (or B or C) in row #1 this will be regarded as a single.
Value3 should then be decreased with 1.

- If the user then chooses B in row #1 the program will regard it as a double. Value3 should then be increased by 1 and Value2 should be decreased by 1.

- If the user then chooses C in row #1 the program will regard it as a triple. Value2 should then be increased by 1 and Value1 should be decreased by 1.

- If the user then chooses A again, the program will regard it as a double. Value1 should be decreased by 1 and Value2 should be increased by 1 (and so on..).

The same applies for all the other rows.. :-)

When the user clicks on a button it will display an "X" to show that the button is selected. If a selected button is clicked once again the "X" will be removed.

If Value1, Value2 or Value3 exceeds the limits set by the values read from the file a warning should be given and the current operation should abort. The same if the values goes below 0 (zero).
(I was a bit inaccurate in my first posting -> The value must be < 0 to trigger a stop of the operation).
[Example of a warning message: "Number of triples exceeded! Please try again!"].

I wonder if there is a clever way to do this using Visual C++ 2003 -> preferably using .NET-functionality.. :-)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:toyboy61
ID: 35219545
evilrix: Thanx. Seems to be a way to do it. Thanks. I will study it in more detail later on.
0
 
LVL 12

Assisted Solution

by:trinitrotoluene
trinitrotoluene earned 50 total points
ID: 35221850
so if I understand you correctly for each row you have a constant number of singles, doubles and triples. Now if each row has just 3 buttons then there can be only 1 triple. So this simplifies the problem a bit.

All you need to do is for each row maintain 3 counters : singlescnt, doublescnt, triplescnt

take a look at my pseudo code below. This will give you an idea, you will have to expand from here on.

so say

struct rowchecker{

int singlescnt;
int doublecnt;
int triplescnt;
};

now 

struct rowchecker myrowchecker;

myrowchecker* rcfor12rows = new myrowchecker[12];

rcfor12rows[0].singlescnt = value1 from file
rcfor12rows[0].doublescnt = value from file
rcfor12rows[0].triplesscnt = value from file

and so on for all 12 rows.

Now comes your checking logic

if user presses 1 button in row[i]
{
then
rcfor12rows[i].singlescnt--;
now check is rcfor12rows[i].singlescnt < 0 or > value from file
then abort;
}

else if user presses 2 button in row[i]
{
similar logic
}

else if user presses 3 buttons in row[i]
{
similar logic
}

Open in new window

0
 

Author Comment

by:toyboy61
ID: 35224150
trinitrotoluene: The total sum of Value1 (number of triples), Value2 (number of doubles) and Value3 (number of singles) are 12.

Example of values read from file:
Value1:  7 (number of triples)
Value2:  3 (number of doubles)
Value3:  2 (number of singles)

This could be set up as:
---------------------------------
Row01:    A       (single)
Row02:    AB    (double)
Row03:    ABC (triple)
---------------------------------
Row04:    BC    (double)
Row05:    ABC (triple)
Row06:    ABC (triple)
---------------------------------
Row07:    ABC (triple)
Row08:    C      (single)
Row09:    ABC (triple)
---------------------------------
Row10:    ABC (triple)
Row11:    ABC (triple)
Row12:    AC    (double)
---------------------------------

The rest of the problem is as described above.
0
 

Author Closing Comment

by:toyboy61
ID: 35231209
Both the given answers headed me in the right direction.
The solution was to create my own code which will be posted here later on.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C++ question 3 75
Exception thrown at 0x00007FFD5BC81F28 7 55
Safe conversion? 4 76
Adding items to a C# list incrementally 5 64
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question