Trumpet Posted 19 December 2013 Posted 19 December 2013 Anyone familiar with C++? I'm struggling with this bit of code, it's returning the last number entered rather than the max: #include <iostream> #include <string> using namespace std; int maxNum(int iNum[], int iCount) { int iMax=0; for(int i=0; i<iCount; i++) { if(iNum>=iMax); { iMax = iNum; } } return iMax; } int main() { int iNum[10]; int iStop = -1; int iCount = 0; cout<<"Please enter up to 10 numbers. To stop entering numbers, input -1: "<<endl; for(int i=0; i<10; i++) { cin>> iNum; iCount = i; if(iNum==iStop) { cout<<"The highest number entered is: "<<maxNum(iNum, iCount)<<endl<<endl; system("pause"); return 0; } cout<<endl; } cout<<"The highest number entered is: "<<maxNum(iNum, iCount)<<endl<<endl; system("pause"); return 0; }
filbertway Posted 19 December 2013 Posted 19 December 2013 int maxNum(int iNum[], int iCount) { int iMax=0; for(int i=0; i<iCount; i++) { if(iNum>=iMax); { iMax = iNum; } } return iMax; } You need an if statement there saying if(iNUM > iMax){ iMax = iNum;}
filbertway Posted 19 December 2013 Posted 19 December 2013 Oh wait, what am I talking about, you already have that! haha Ignore me
filbertway Posted 19 December 2013 Posted 19 December 2013 Is the semi-colon after the if statement causing the issue? Not sure if that's normal for c++
Haydos Posted 19 December 2013 Posted 19 December 2013 Is the semi-colon after the if statement causing the issue? Not sure if that's normal for c++ Haha, you're the worst.
Trumpet Posted 19 December 2013 Author Posted 19 December 2013 Is the semi-colon after the if statement causing the issue? Not sure if that's normal for c++ Turns out it was, made it a valid if statement, that does nothing so the program continued. Works a treat after removing the semi-colon, thanks!
Lobsterboyuk Posted 19 December 2013 Posted 19 December 2013 Fvck me, I thought I'd mistyped the url and ended up on stackoverflow for a minute there :xmaslaugh:
danny. Posted 19 December 2013 Posted 19 December 2013 nah, can't be stackoverflow - no pointless jquery questions
lavrentis Posted 19 December 2013 Posted 19 December 2013 my mate posted a question on stack overflow to do with his coursework and the lecturer emailed him and he had to prove it was his code Then apparently 14 people copied his code and 4 of those got 0%.
filbertway Posted 19 December 2013 Posted 19 December 2013 Haha, you're the worst. I was taking the million monkeys at a million typewriters route! It paid off in the end! Turns out it was, made it a valid if statement, that does nothing so the program continued. Works a treat after removing the semi-colon, thanks!
Haydos Posted 19 December 2013 Posted 19 December 2013 I was taking the million monkeys at a million typewriters route! It paid off in the end! A stopped clock is right twice a day
Trumpet Posted 19 December 2013 Author Posted 19 December 2013 Fvck me, I thought I'd mistyped the url and ended up on stackoverflow for a minute there :xmaslaugh: :xmaslaugh: I find with those websites and forums, a question from a beginner can often turn into a competition between the people answering as to who's code is best, which gets too complex for me at the moment.
Lobsterboyuk Posted 19 December 2013 Posted 19 December 2013 :xmaslaugh: I find with those websites and forums, a question from a beginner can often turn into a competition between the people answering as to who's code is best, which gets too complex for me at the moment. nah, can't be stackoverflow - no pointless jquery questions my mate posted a question on stack overflow to do with his coursework and the lecturer emailed him and he had to prove it was his code Then apparently 14 people copied his code and 4 of those got 0%. Loving it, stack overflow banter on FT :xmassmile:
Guest shearfox Posted 19 December 2013 Posted 19 December 2013 why have count as a parameter ? you can simply just pass an array of numbers and compare them one by one to see which is the max... set a variable to just get the length of your array and pass it into the for loop instead of iCount iMax = 0; For (i = 0; sizeof(iNum); i++) { if (iNum > iMax) { iMax = iNum; } } Return iMax; This is a nice generic function that will work with any amount of numbers entered. Also for efficiency place the sizeof() function outside of the loop and just store the value has a variable. This means the function will not be called everytime during the loop...
Trumpet Posted 20 December 2013 Author Posted 20 December 2013 I've used count to work out when or of the iser enters -1 which stops the numbers being stored in the array. The array is [10] so without the count it would add up the remainder of the array as it's undeclared amd would hold random values. Or atleast that's what I've been taught so far. Ahh stupid phone / fat fingers
Trumpet Posted 20 December 2013 Author Posted 20 December 2013 And I've misread your post, apologies. I'll make a few tweaks later, thanks
The Blur Posted 20 December 2013 Posted 20 December 2013 Hahaha I got desperate enough on couple of occasions when I did my computing degree to consider posting problems on here but I always decided against it cause I thought you all won't have a clue. Now I wish I did post problems! :xmaslaugh:
Basingstoke Fox Posted 20 December 2013 Posted 20 December 2013 Wow, I'm impressed with the number of geeks on here.
Trumpet Posted 23 February 2014 Author Posted 23 February 2014 Me again. I'm struggling with comparing the suits of a poker hand to determine a flush. Here's the code (hopefully it's enough for someone to understand): //function to determine flush int HandCheck(Card iHand[5]) { if(iHand[0].GetSuit() == iHand[1].GetSuit() && iHand[2].GetSuit() == iHand[3].GetSuit() && iHand[3].GetSuit() == iHand[4].GetSuit()) { cout << "Flush" <<endl; return 6; } } //sends cards to function else if(m_GameState == GameState::CompareHands) { for(int i=0; i<5; i++) { iHandValue = HandCheck(m_pHand1->GetCardAtPosition(i)); } } Basically, it's sending the card at position 0 to the function and the other cards are not sent to the function so the values of iHand[1] through to [4] is a random value of -1287425 or something. Whereas iHand[0] has a proper value such as Hearts. Does anyone know a way to send all cards to the function and put in the array to compare them?
lavrentis Posted 23 February 2014 Posted 23 February 2014 do you mind uploading the whole project please? Youve got missing methods and data types with just that code I'll try with just that though so no worries if you cant upload
Trumpet Posted 23 February 2014 Author Posted 23 February 2014 It's a pretty big file so probably wouldn't be able to. Plus it's for uni and I'm sure it's probably frowned upon! So I've got 5 cards in hand - from position 0-4. I want each one of these card objects to be sent to the function and take place in the array iHand[5]. I reckon once I've cracked this part I'm pretty much done as I can just put the code in for the other possible poker hands.
SantiagoFox Posted 23 February 2014 Posted 23 February 2014 You need to pass an array of five cards to the function; looks like you are passing just one value? I.e fill in an array, then pass it to the function.
Trumpet Posted 23 February 2014 Author Posted 23 February 2014 iHandValue = HandCheck(m_pHand1->GetCardAtPosition(i)); So that line needs adjusting?
Recommended Posts
Archived
This topic is now archived and is closed to further replies.