Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C++] Array help

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
whoarere
How do I cheat?
Reputation: 0

Joined: 10 Jan 2009
Posts: 9

PostPosted: Sun Feb 15, 2009 4:33 pm    Post subject: [C++] Array help Reply with quote

Ok... so I've just read about arrays, and planned to make a program that listed numbers entered from least to greatest.

Here's my code:

Code:

#include <cstdlib>
#include <iostream>

using namespace std;

int set [2][5] = {};
int tempNum [2][5] = {};
int tempCount [2][5] = {};

// This is an array program that was supposed to print out numbers from
// greatest to least, but ended up only printing out the third entry and
// completely killing the CPU.
// Made by AcroMace on Febuary 14 2009

void setFirstSetArray()
{
// Sets the arrays from 1-5
     cout << "Please type the first number you want for the array" << endl;
     cin >> set[0][0];
     cout << "\n";
     cout << "Please type the second number you want for the array" << endl;
     cin >> set[0][1];
     cout << "\n";
     cout << "Please type the third number you want for the array" << endl;
     cin >> set[0][2];
     cout << "\n";
     cout << "Please type the fourth number you want for the array" << endl;
     cin >> set[0][3];
     cout << "\n";
     cout << "Please type the fifth number you want for the array" << endl;
     cin >> set[0][4];
     cout << "\n";
}

void setSecondSetArray()
{
// Sets the arrays from 6-10
     cout << "Please type the sixth number you want for the array" << endl;
     cin >> set[1][0];
     cout << "\n";
     cout << "Please type the seventh number you want for the array" << endl;
     cin >> set[1][1];
     cout << "\n";
     cout << "Please type the eighth number you want for the array" << endl;
     cin >> set[1][2];
     cout << "\n";
     cout << "Please type the ninth number you want for the array" << endl;
     cin >> set[1][3];
     cout << "\n";
     cout << "Please type the tenth number you want for the array" << endl;
     cin >> set[1][4];
     cout << "\n";
}

// tempNum   = gets subtracted everytime it is called,
//             starts off same as the set arrays
// tempCount = 0 is when tempNum is over 0 and still needs to be subtracted
//             1 is when tempNum is first 0 and the set value and ", "
//              is printed
//             2 is set right after 1 so the whole function stops working

void array1Calculate()
{
// Calculates the amount of time needed to run this function,
// prints the amount that the set value carries
// For array 1
     if (tempNum[0][0] > 0 && tempCount[0][0] == 0)
          tempNum[0][0]--;
     else if (tempCount[0][0] == 0)
          tempCount[0][0] = 1;
     if (tempNum[0][0] == 0 && tempCount[0][0] == 1)
         cout << set[0][0] << ", ";
         tempCount[0][0] = 2;
}

void array2Calculate()
{
// Calculates the amount of time needed to run this function,
// prints the amount that the set value carries
// For array 2
     if (tempNum[0][1] > 0 && tempCount[0][1] == 0)
          tempNum[0][1]--;
     else if (tempCount[0][1] == 0)
          tempCount[0][1] = 1;
     if (tempNum[0][1] == 0 && tempCount[0][1] == 1)
         cout << set[0][1] << ", ";
         tempCount[0][1] = 2;
}

void array3Calculate()
{
// Calculates the amount of time needed to run this function,
// prints the amount that the set value carries
// For array 3
     if (tempNum[0][2] > 0 && tempCount[0][2] == 0)
          tempNum[0][2]--;
     else if (tempCount[0][2] == 0)
          tempCount[0][2] = 1;
     if (tempNum[0][2] == 0 && tempCount[0][2] == 1)
         cout << set[0][2] << ", ";
         tempCount[0][2] = 2;
}

void array4Calculate()
{
// Calculates the amount of time needed to run this function,
// prints the amount that the set value carries
// For array 4
     if (tempNum[0][3] > 0 && tempCount[0][3] == 0)
          tempNum[0][3]--;
     else if (tempCount[0][3] == 0)
          tempCount[0][3] = 1;
     if (tempNum[0][3] == 0 && tempCount[0][3] == 1)
         cout << set[0][3] << ", ";
         tempCount[0][3] = 2;
}

void array5Calculate()
{
// Calculates the amount of time needed to run this function,
// prints the amount that the set value carries
// For array 5
     if (tempNum[0][4] > 0 && tempCount[0][4] == 0)
          tempNum[0][4]--;
     else if (tempCount[0][4] == 0)
          tempCount[0][4] = 1;
     if (tempNum[0][4] == 0 && tempCount[0][4] == 1)
         cout << set[0][4] << ", ";
         tempCount[0][4] = 2;
}

void array6Calculate()
{
// Calculates the amount of time needed to run this function,
// prints the amount that the set value carries
// For array 6     
     if (tempNum[1][0] > 0 && tempCount[1][0] == 0)
          tempNum[1][0]--;
     else if (tempCount[1][0] == 0)
          tempCount[1][0] = 1;
     if (tempNum[1][0] == 0 && tempCount[1][0] == 1)
         cout << set[1][0] << ", ";
         tempCount[1][0] = 2;
}

void array7Calculate()
{
// Calculates the amount of time needed to run this function,
// prints the amount that the set value carries
// For array 7
     if (tempNum[1][1] > 0 && tempCount[1][1] == 0)
          tempNum[1][1]--;
     else if (tempCount[1][1] == 0)
          tempCount[1][1] = 1;
     if (tempNum[1][1] == 0 && tempCount[1][1] == 1)
         cout << set[1][1] << ", ";
         tempCount[1][1] = 2;
}

void array8Calculate()
{
// Calculates the amount of time needed to run this function,
// prints the amount that the set value carries
// For array 8
     if (tempNum[1][2] > 0 && tempCount[1][2] == 0)
          tempNum[1][2]--;
     else if (tempCount[1][2]== 0)
          tempCount[1][2] = 1;
     if (tempNum[1][2] == 0 && tempCount[1][2] == 1)
         cout << set[1][2] << ", ";
         tempCount[1][2] = 2;
}

void array9Calculate()
{
// Calculates the amount of time needed to run this function,
// prints the amount that the set value carries
// For array 9
     if (tempNum[1][3] > 0 && tempCount[1][3] == 0)
          tempNum[1][3]--;
     else if (tempCount[1][3] == 0)
          tempCount[1][3] = 1;
     if (tempNum[1][3] == 0 && tempCount[1][3] == 1)
         cout << set[1][3] << ", ";
         tempCount[1][3] = 2;
}

void array10Calculate()
{
// Calculates the amount of time needed to run this function,
// prints the amount that the set value carries
// For array 10
     if (tempNum[1][4] > 0 && tempCount[1][4] == 0)
          tempNum[1][4]--;
     else if (tempCount[1][4] == 0)
          tempCount[1][4] = 1;
     if (tempNum[1][4] == 0 && tempCount[1][4] == 1)
         cout << set[1][4] << ", ";
         tempCount[1][4] = 2;
}

void checkRepeat1 (bool& repeat1)
{
// Checks to see if any of the tempNums are not zero
// in the arrays 1-5
// If all tempNums are zeros,
// then it reports back to not repeat for the first five arrays
     if (
         tempNum[0][0] != 0 ||
         tempNum[0][1] != 0 ||
         tempNum[0][2] != 0 ||
         tempNum[0][3] != 0 ||
         tempNum[0][4] != 0
     )
         repeat1 = true;
     else if (
         tempNum[0][0] == 0 &&
         tempNum[0][1] == 0 &&
         tempNum[0][2] == 0 &&
         tempNum[0][3] == 0 &&
         tempNum[0][4] == 0
     )
         repeat1 = false;
}

void checkRepeat2 (bool& repeat2)
{
     if (
         tempNum[1][0] != 0 ||
         tempNum[1][1] != 0 ||
         tempNum[1][2] != 0 ||
         tempNum[1][3] != 0 ||
         tempNum[1][4] != 0
     )
         repeat2 = true;
     else if (
         tempNum[1][0] == 0 &&
         tempNum[1][1] == 0 &&
         tempNum[1][2] == 0 &&
         tempNum[1][3] == 0 &&
         tempNum[1][4] == 0
     )
         repeat2 = false;
}

void declareTempNums()
{
// Sets all the tempNums to it's set value
// The set value will be stored to be called upon for cout in the calc functions
// The tempNum is needed as an active variable in the calc functions
     tempNum[0][0] = set[0][0];
     tempNum[0][1] = set[0][1];
     tempNum[0][1] = set[0][2];
     tempNum[0][3] = set[0][3];
     tempNum[0][4] = set[0][4];
     tempNum[1][0] = set[1][0];
     tempNum[1][1] = set[1][1];
     tempNum[1][2] = set[1][2];
     tempNum[1][3] = set[1][3];
     tempNum[1][4] = set[1][4];
}

void declareTempCounts()
{
// Sets all the tempCount variables to 0
// tempCounts will be used in the calc functions
     tempCount[0][0] = 0;
     tempCount[0][1] = 0;
     tempCount[0][2] = 0;
     tempCount[0][3] = 0;
     tempCount[0][4] = 0;
     tempCount[1][0] = 0;
     tempCount[1][1] = 0;
     tempCount[1][2] = 0;
     tempCount[1][3] = 0;
     tempCount[1][4] = 0;
}

void calculate()
{
// All main variable are set and calls upon the calc functions and the repeat
//  functions
// Repeats are true to begin with, and is set otherwise by the checkRepeat
//  functions
     bool repeat1 = true, repeat2 = true;
     while (repeat1 = true || repeat2 == true)
     {
          array1Calculate();
          array2Calculate();
          array3Calculate();
          array4Calculate();
          array5Calculate();
          array6Calculate();
          array7Calculate();
          array8Calculate();
          array9Calculate();
          array10Calculate();
          checkRepeat1(repeat1);
          checkRepeat2(repeat2);
     }
}

int main(int argc, char *argv[])
{
    cout << "This program will list the numbers you enter\n"
    << "from least to greatest\n" << endl;
    setFirstSetArray();
    setSecondSetArray();
    declareTempNums();
    declareTempCounts();
    calculate();
    system("PAUSE");
    return EXIT_SUCCESS;
}


The only problem is, the program only returns the third number entered (no matter what number you put in),
and then goes ahead and kills the CPU.

I looked at the array3Calculate, and it's the same as all the other calc functions
(by calc functions I mean array#Calculate functions)

Yes, I know it's not the best programming in the world... but I'm still learning Smile

Can anyone please help me figure out this bug?
I've already done a trace, and I'm figuring it's just one of those stupid errors I always make, but I couldn't find the problem.

Thanks Smile
Back to top
View user's profile Send private message
manc
Grandmaster Cheater
Reputation: 1

Joined: 16 Jun 2006
Posts: 551

PostPosted: Sun Feb 15, 2009 5:15 pm    Post subject: Reply with quote

I havent worked enough to truly give you an solution but this is a good reference for arrays http://www.cplusplus.com/doc/tutorial/arrays.html

And explain your logic a bit more

_________________
Back to top
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Sun Feb 15, 2009 5:16 pm    Post subject: Reply with quote

Errr. Wow, I gotta say. You've been hella busy doing useless stuff :D

Anyway, too lazy to get familiar with the code, but I think this one might be a little wrong:
Code:
while (repeat1 = true || repeat2 == true)
. You're missing one '='? Debugging is always good way to find out what's wrong with your code.
Back to top
View user's profile Send private message
manc
Grandmaster Cheater
Reputation: 1

Joined: 16 Jun 2006
Posts: 551

PostPosted: Sun Feb 15, 2009 5:24 pm    Post subject: Reply with quote

Yeah I have to agree with Jani, im following it line by line right now but shit its all over the place you should have listed the functions in the order they are called

*Will provide insight once im done reading

Wtf are you trying to do in array1-10Calculate?

_________________
Back to top
View user's profile Send private message
sloppy
Expert Cheater
Reputation: 0

Joined: 17 Aug 2008
Posts: 123

PostPosted: Sun Feb 15, 2009 7:25 pm    Post subject: Reply with quote

Code:
void array1Calculate()
{
     <snip>

     if (tempNum[0][0] == 0 && tempCount[0][0] == 1)
         cout << set[0][0] << ", ";
         tempCount[0][0] = 2; // executed regardless of the above if statement
}

Use code braces to enclose multiple statements. if (foo == bar) { foo = 1; bar = 2; }.

Code:
void declareTempNums()
{
     tempNum[0][0] = set[0][0];
     tempNum[0][1] = set[0][1];
     tempNum[0][1] = set[0][2]; // <-- whoops
     tempNum[0][3] = set[0][3];
     ...
}

.. and as Jani pointed out, "while (repeat1 == true || repeat2 == true)" in calculate().

You should work on further simplifying your code, as you certainly don't need 10 calculate functions, multi-dimensional arrays, etc.. Also, take a look at the bubble sort algorithm.
Back to top
View user's profile Send private message
rapion124
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Mar 2007
Posts: 1095

PostPosted: Sun Feb 15, 2009 7:49 pm    Post subject: Reply with quote

Dude, why do you hard code everything? This is a version I made in 10 minutes.


The Extension 'rar' was deactivated by an board admin, therefore this Attachment is not displayed.

Back to top
View user's profile Send private message
whoarere
How do I cheat?
Reputation: 0

Joined: 10 Jan 2009
Posts: 9

PostPosted: Sun Feb 15, 2009 8:06 pm    Post subject: Reply with quote

Thanks for catching all the bugs.
I didn't know that the if statements needed brackets... I just looked at one example of them and saw it didn't have brackets, so I thought it was just different Smile

Now the program actually terminates (yay!),
and mostly does what it's supposed to,
except for the fact that it doesn't print out the last number.

I didn't expect this much help to already be there while I wrote out what the program is supposed to do...

@rapion: because I suck at programming Smile

-----

Alright, I'll explain how the whole code works (or is supposed to), even though I think all of you guys already figured it out...

1. main calls setFirstArray, which enters the first 5 numbers into set[0][0] to set[0][4]

2. main calls setSecondArray, which enteres the last 5 numbers into set[1][0] to set[1][4]

3. main calls declareTempNums which sets all the values from the set variable, and then puts it in the exact same places in the tempNum variables. (By exact same, I mean set[1][0] would be tempNum[1][0])

4. main calls declareTempCounts, which sets all numbers as 0 (I think by default it's already 0, but I put it there just to be safe)

5. main calls calculate

6. calculate sets repeat1 and repeat2 as true, so the while loop will start

7. The while loop calls all array#Calculate functions

8. The array#Calculate starts with the if loop, where if its given tempNum is over 0, and its given tempCount is 0, it subtracts one tempNum.

9. If the tempNum is 0, then tempCount becomes 1. Then it prints the given set variable, which should be the same as its tempCount variable as mentioned before, and then prints ", "

10. The tempCount is set to 2, so that the next time the while loop calls this function, it won't run anything.

11. checkRepeat# checks to see if tempNums are all zero. If any of the tempNums are not 0, then it returns repeat# = true.

12. Since the while loop says if either repeat#s are true then it will go on, the while loop should continue untill both of them are false.

13. The program ends.
Back to top
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Mon Feb 16, 2009 2:56 am    Post subject: Reply with quote

I recommend you go back and learn about for loops before continuing on.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
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
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites