Our system detected that your browser is blocking advertisements on our site. Please help support FoxesTalk by disabling any kind of ad blocker while browsing this site. Thank you.
Jump to content
Trumpet

c++

Recommended Posts

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;
}
Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

: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 lol

 

Then apparently 14 people copied his code and 4 of those got 0%.

 

Loving it, stack overflow banter on FT  :xmassmile:

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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:

Link to comment
Share on other sites

  • 2 months later...

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...