-
sorry its so long, but its due tomorrow...no time for good programming practice....I'm getting an access violation error every time i input the 12 numbers :mad:
PHP Code:
// TITLE: ACSL TEST #4 (Florida^3)
// AUTHOR: STEVE MACK
// DATE: April 5, 2001
// DUE DATE: April 6, 2001
#include <iostream.h>
#include <math.h>
#include <conio.h>
#include <string.h>
int i;
int votes[13];
int FinalVotes[5][5];
int Order[7];
class cCHART {
public:
~cCHART() {}
void setmag(int mag);
void setwinner(char winner);
void setloser(char loser);
char getLoser();
char getWinner();
int getMag();
protected:
int TheMag;
char TheWinner;
char TheLoser;
};
char cCHART::getWinner() {
return TheWinner;
};
char cCHART::getLoser() {
return TheLoser;
};
int cCHART::getMag() {
return TheMag;
};
void cCHART::setmag(int mag) {
TheMag = mag;
};
void cCHART::setwinner(char winner) {
TheWinner = winner;
};
void cCHART::setloser(char loser) {
TheLoser = loser;
};
cCHART row[7]; //it's an array for easy sorting =)
void CheckForWinner();
int GiveReturnMessage(int nExitCode);
void InputSequence();
void TallyVoteSequence();
void OrderList();
//===========================================================================================
int main() {
InputSequence();
TallyVoteSequence();
OrderList();
CheckForWinner();
cout<<endl<<"Press any key to quit."<<endl;
getch();
return 0;
}
//===========================================================================================
int GiveReturnMessage(int nExitCode) {
return nExitCode;
}
inline void InputSequence() {
for(i=1; i<=12; i++) {
cout<<"Enter Number "<<i<<": ";
cin>>votes[i];
if(votes[i]==-999) {
break;
GiveReturnMessage(0);
}
}
FinalVotes[1][2] = votes[1];
FinalVotes[1][3] = votes[2];
FinalVotes[1][4] = votes[3];
FinalVotes[2][1] = votes[4];
FinalVotes[2][3] = votes[5];
FinalVotes[2][4] = votes[6];
FinalVotes[3][1] = votes[7];
FinalVotes[3][2] = votes[8];
FinalVotes[3][4] = votes[9];
FinalVotes[4][1] = votes[10];
FinalVotes[4][2] = votes[11];
FinalVotes[4][3] = votes[12];
}
void TallyVoteSequence() {
//A-B
if(FinalVotes[1][2]>FinalVotes[2][1]) {
row[1].setwinner('A');
row[1].setloser('B');
row[1].setmag(FinalVotes[1][2]);
}
if(FinalVotes[2][1]>FinalVotes[1][2]) {
row[1].setwinner('B');
row[1].setloser('A');
row[1].setmag(FinalVotes[2][1]);
}
//A-C
if(FinalVotes[1][3]>FinalVotes[3][1]) {
row[2].setwinner('A');
row[2].setloser('C');
row[2].setmag(FinalVotes[1][3]);
}
if(FinalVotes[3][1]>FinalVotes[1][3]) {
row[2].setwinner('C');
row[2].setloser('A');
row[2].setmag(FinalVotes[3][1]);
}
//A-D
if(FinalVotes[1][4]>FinalVotes[4][1]) {
row[3].setwinner('A');
row[3].setloser('D');
row[3].setmag(FinalVotes[1][4]);
}
if(FinalVotes[4][1]>FinalVotes[1][4]) {
row[3].setwinner('D');
row[3].setloser('A');
row[3].setmag(FinalVotes[4][1]);
}
//B-C
if(FinalVotes[2][3]>FinalVotes[3][2]) {
row[4].setwinner('B');
row[4].setloser('C');
row[4].setmag(FinalVotes[2][3]);
}
if(FinalVotes[3][2]>FinalVotes[2][3]) {
row[4].setwinner('C');
row[4].setloser('B');
row[4].setmag(FinalVotes[3][2]);
}
//B-D
if(FinalVotes[2][4]>FinalVotes[4][2]) {
row[5].setwinner('B');
row[5].setloser('D');
row[5].setmag(FinalVotes[2][4]);
}
if(FinalVotes[4][2]>FinalVotes[2][4]) {
row[5].setwinner('D');
row[5].setloser('B');
row[5].setmag(FinalVotes[4][2]);
}
//C-D
if(FinalVotes[3][4]>FinalVotes[4][3]) {
row[6].setwinner('C');
row[6].setloser('D');
row[6].setmag(FinalVotes[3][4]);
}
if(FinalVotes[4][3]>FinalVotes[3][4]) {
row[6].setwinner('D');
row[6].setloser('C');
row[6].setmag(FinalVotes[4][3]);
}
}
void OrderList() {
int j;
for(j=1; j<=6; j++) {
for(i=1; i<=6; i++) {
if(i!=6) {
if(row[i+1].getMag() > row[i].getMag()) {
Order[i] = row[i+1].getMag();
Order[i+1] = row[i].getMag();
}
}
}
}
}
void CheckForWinner() {
bool IsThere = false;
int k;
char CheckChar;
char temporary[2];
for(k=1; k<=4; k++) {
if(k==1) { CheckChar = 'A'; }
if(k==2) { CheckChar = 'B'; }
if(k==3) { CheckChar = 'C'; }
if(k==4) { CheckChar = 'D'; }
for(i=1; i<=6; i++) {
if(strcmp((char*)row[i].getLoser(), (char*)CheckChar) == 0) {
IsThere = true;
k++;
}
if(IsThere = false) {
cout<<"The Winner Is: "<<CheckChar<<endl;
break;
}
CheckChar = false;
}
}
for(k=1; k<=4; k++) {
if(k==1) { CheckChar = 'A'; }
if(k==2) { CheckChar = 'B'; }
if(k==3) { CheckChar = 'C'; }
if(k==4) { CheckChar = 'D'; }
for(i=1; i<=5; i++) {
if(strcmp((char*)row[i].getLoser(),(char*)CheckChar) == 0) {
IsThere = true;
k++;
}
if(IsThere = false) {
cout<<"The Winner Is: "<<CheckChar<<endl;
break;
}
CheckChar = false;
}
}
for(k=1; k<=4; k++) {
if(k==1) { CheckChar = 'A'; }
if(k==2) { CheckChar = 'B'; }
if(k==3) { CheckChar = 'C'; }
if(k==4) { CheckChar = 'D'; }
for(i=1; i<=4; i++) {
if(strcmp((char*)row[i].getLoser(), (char*)CheckChar) == 0) {
IsThere = true;
k++;
}
if(IsThere = false) {
cout<<"The Winner Is: "<<CheckChar<<endl;
break;
}
CheckChar = false;
}
}
for(k=1; k<=4; k++) {
if(k==1) { CheckChar = 'A'; }
if(k==2) { CheckChar = 'B'; }
if(k==3) { CheckChar = 'C'; }
if(k==4) { CheckChar = 'D'; }
for(i=1; i<=3; i++) {
if(strcmp((char*)row[i].getLoser(), (char*)CheckChar) == 0) {
IsThere = true;
k++;
}
if(IsThere = false) {
cout<<"The Winner Is: "<<CheckChar<<endl;
break;
}
CheckChar = false;
}
}
for(k=1; k<=4; k++) {
if(k==1) { CheckChar = 'A'; }
if(k==2) { CheckChar = 'B'; }
if(k==3) { CheckChar = 'C'; }
if(k==4) { CheckChar = 'D'; }
for(i=1; i<=2; i++) {
if(strcmp((char*)row[i].getLoser(), (char*)CheckChar) == 0) {
IsThere = true;
k++;
}
if(IsThere = false) {
cout<<"The Winner Is: "<<CheckChar<<endl;
break;
}
CheckChar = false;
}
}
for(k=1; k<=4; k++) {
if(k==1) { CheckChar = 'A'; }
if(k==2) { CheckChar = 'B'; }
if(k==3) { CheckChar = 'C'; }
if(k==4) { CheckChar = 'D'; }
for(i=1; i<=1; i++) {
if(strcmp((char*)row[i].getLoser(), (char*)CheckChar) == 0) {
IsThere = true;
k++;
}
if(IsThere = false) {
cout<<"The Winner Is: "<<CheckChar<<endl;
break;
}
CheckChar = false;
}
}
}
BTW: I told dennis and he says: "Ok" *kick*
-
PHP Code:
void OrderList() {
int j;
for(j=1; j<=6; j++) {
for(i=1; i<=6; i++) { // problem here I think
if(i!=6) {
if(row[i+1].getMag() > row[i].getMag()) {
Order[i] = row[i+1].getMag();
Order[i+1] = row[i].getMag();
}
}
}
}
}
You are including the case when i=6 here. That means row[i+1] evaluates to row[7] but the array row only goes from 0 to 6.
PHP Code:
if(strcmp((char*)row[i].getLoser(), (char*)CheckChar) == 0)
DOes this compile okay for you? I thought you had to get a pointer to the character using the & operator before you could cast it to a different kind of pointer. Perhaps you are passing a value to strcmp() rather than a pointer. If it then uses that value as an address and tries to dereference it, you are pretty much guaranteed to get an access violation.
Do me a favour and kick him back ;)
-
I take it back on that first bit, I just noticed the if(i!=6) bit :rolleyes: I just saw the for loop and never thought youwould use something like that. Why not just use this? :
for(i=1; i<6; i++)
-
I still get an access violation...the debugger brings me to another text screen pointing to this:
Code:
00405BB4 mov al,byte ptr [edx]
and this is whats in the normal error box
Code:
First-chance exception in acsl.exe: 0xC0000005: Access Violation.
-
sorry my sister wants to go on (s***!!!) I'll be back in 15-30 minutes :mad:
-
Okay, what did you change?
I still think it's something to do with the lines that look like this:
PHP Code:
if(strcmp((char*)row[i].getLoser(), (char*)CheckChar) == 0)
Firstly, I'm not sure that you are sending the addresses of the characters as the arguments to strcmp().
Secondly, I'm not even sure that you're sending a string, since a string needs a null terminating character.
Thirdly, I think you may have problems with precedence when using the .getLoser() method. I would make sure ot parenthesise everything clearly to make sure it doesn't go wrong.
To fix the first problem you could use sprintf() to make yourself a string from the character and send the resulting string. To fix the second problem, well sprintf() would fix that too. To fix the 3rd problem I would add some brackets if the structure of the code wasn't going to be changed by sprintf, but the way I'm writing it at least it is getting changed. I don't have the MSDN docs on this computer so I'm not sure if sprintf() returns the formatted string, but it probably does so you could do this differently, but I'm not going to because it's off the top of my head :). Here's an example of what I would do:
PHP Code:
char buffer1[50];
char buffer2[50];
sprintf(buffer1, "%c", row[i].getLoser());
sprintf(buffer2, "%c", CheckChar);
if(strcmp(buffer1, buffer2)
// then strings are the same, do whatever
-
that works...but now I get no answer....if you look at the if statement below what you just helped me with...
PHP Code:
for(k=1; k<=4; k++) {
if(k==1) { CheckChar = 'A'; }
if(k==2) { CheckChar = 'B'; }
if(k==3) { CheckChar = 'C'; }
if(k==4) { CheckChar = 'D'; }
for(i=1; i<=6; i++) {
sprintf(buffer1, "%c", row[i].getLoser());
sprintf(buffer2, "%c", CheckChar);
if(strcmp(buffer1, buffer2)) {
IsThere = true;
k++;
}
if(IsThere = false) {
cout<<"The Winner Is: "<<CheckChar<<endl;
break;
}
IsThere = false;
}
}
I check to see if that number never came in that list...but my program is saying that letter is always in the list......here are the numbers I input
40, 22,13,37,50,50,30,35,25,20,60,20
it should output D
-
You've fallen for a classic C/C++ error - using the assignment operator ( = ) when you meant the equality operator ( == ).
if( IsThere = false )
will always evaluate to the right hand side of the = operator, which in this case is false. You just need to add another = :)
-
STUPID ME!!!!! dennis pointed one of those out to me a while ago! dumb dumb dumb
I also found it isn't sorting right! :mad:
it should sort in order from greatest to least magnitude (the getMag function). whats wrong with my order function too?
-
oh btw...is there a way to quit a funciton? because break only quits the if function so it gives me around 5 answers :p
-
I also just noticed that my sort function does nothing....I don't know how to do that....could you show me some way?
-
1. uhh well your order function doesn't seem to do a lot, I'm not sure what it's meant to be doing. A bubble sort maybe? Try this:
PHP Code:
void OrderList()
{ bool swaps = true;
int temp;
int x;
for(x=1; x<=6; x++)
order[x] = row[x].getMag();
while(swaps)
{ swaps = false;
for(x=1; x<6; x++)
{ if(order[x] < order[x+1])
{ temp = order[x];
order[x] = order[x+1];
order[x+1] = temp;
swaps = true;
}
}
};
}
2. To exit a function just use return; if the function returns void, or return <type>; where <type> is the return type of the function.
-
I'm sorry I think I have totally screwed up this order thing...all I want it to do is order them from greatest to least magnitude (mag).
-
Oh yeah, with data like integers, any basic datatype that is represented with just binary, not a struct or class or anything, you can swap two variables without a temporary variable by using XORs. Just like this:
A = A XOR B
B = A XOR B
A = A XOR B
which in C syntax is:
PHP Code:
A ^= B;
B ^= A;
A ^= B;
Just a bit of useful information, you can impress your teachers with it :)
-
Did you try the order function I gave?
-
the order function you gave really only orders the magnitudes into a seperate variable...I want the whole row variables re-odered by their magnitude
-
Oh, well that's easily changed. Well it would be easier if you overloaded the = operator for your class but to save it getting too complicated I'll make a swap function instead :)
PHP Code:
void SwapChart(cCHART &chart1, cCHART &chart2)
{ int nTemp;
char cTemp
//swap magnitudes
nTemp = chart1.getMag();
chart1.setmag(chart2.getMag());
chart2.setmag(nTemp);
//swap winner
cTemp = chart1.getWinner();
chart1.setwinner(chart2.getWinner));
chart2.setmag(cTemp);
//swap loser
cTemp = chart1.getLoser();
chart1.setloser(chart2.getLoser));
chart2.setloser(cTemp);
}
void OrderList()
{ bool swaps = true;
int temp;
int x;
for(x=1; x<=6; x++)
order[x] = row[x].getMag();
while(swaps)
{ swaps = false;
for(x=1; x<6; x++)
{ if(row[x].getMag() < order[x+1].getMag())
{ SwapChart(row[x], row[x+1])
swaps = true;
}
}
};
}
I hope you can get this working soon as it's gone 5 am and I relly need to go to bed :)
[edited because I realised I used the wrong name for my swap function, silly me :rolleyes:]
-
sorry for keeping you up...but after all that its still doesn't sort fully...
-
Yeah I just fixed that before you posted. Figured you could have fixed it yourself though really, it's not complicated. The code is a bubble sort, in case you haven't come across bubble sorts before. It is a very easy if slightly slow method of sorting data. Take note, it's very useful in lots of things :)
-
its sorts to this now:
30
20
50
60
25
68 (I never inputted 68? :confused: )
and I want:
20
25
30
50
60
(you know)
-
Oh, you changed your post too :rolleyes:
No idea why it doesn't work, my brain is getting tired. Maybe the references are mucking it up, who knows. Try it with pointers instead:
PHP Code:
void SwapChart(cCHART *chart1, cCHART *chart2)
{ int nTemp;
char cTemp
//swap magnitudes
nTemp = chart1->getMag();
chart1->setmag(chart2->getMag());
chart2->setmag(nTemp);
//swap winner
cTemp = chart1->getWinner();
chart1->setwinner(chart2->getWinner));
chart2->setmag(cTemp);
//swap loser
cTemp = chart1->getLoser();
chart1->setloser(chart2->getLoser));
chart2->setloser(cTemp);
}
void OrderList()
{ bool swaps = true;
int temp;
int x;
for(x=1; x<=6; x++)
order[x] = row[x].getMag();
while(swaps)
{ swaps = false;
for(x=1; x<6; x++)
{ if(row[x].getMag() < row[x+1].getMag())
{ SwapChart(&row[x], &row[x+1]);
swaps = true;
}
}
};
}
[edit - silly me, I hadn't changed it properly. I'll leave it as pointers now though because I'm lazy ;), change it back to references if you like]
-
still doens't work...can anyone who's awake help me?
-
Okay well here are the references back in.
PHP Code:
void SwapChart(cCHART &chart1, cCHART &chart2)
{ int nTemp;
char cTemp;
//swap magnitudes
nTemp = chart1.getMag();
chart1.setmag(chart2.getMag());
chart2.setmag(nTemp);
//swap winner
cTemp = chart1.getWinner();
chart1.setwinner(chart2.getWinner));
chart2.setmag(cTemp);
//swap loser
cTemp = chart1.getLoser();
chart1.setloser(chart2.getLoser));
chart2.setloser(cTemp);
}
void OrderList()
{ bool swaps = true;
int temp;
int x;
while(swaps)
{ swaps = false;
for(x=1; x<6; x++)
{ if(row[x].getMag() < row[x+1].getMag())
{ SwapChart(row[x], row[x+1]);
swaps = true;
}
}
};
}
I'm going to bed then. Can't think straight. Nearly 6 am. Urgh.
[edited for syntax errors and stupid crap]
-
Harry: Thank you so much for all of your help! I beared down for about 45 minutes...rewrote 3 functions and now it works! for the sort...we sorted it...but we never actually returned the value...so in the swap function, the real numbers never got changed, just "clones" in the other function....THANKS!!!!!!! :D :D :D :D :D :D :cool: :cool: :cool: :cool: :cool:
-
Thanks Harry! I got the highest score in my division (the school) :D :D :D :cool: :cool: :D :D :D
-
Nice one
Cool, glad to hear it :cool:
I just got back from the pub so I'm not really sure what you're talking about, but maybe I'll understand better tomorrow :rolleyes: