Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualPwFClockWise

Posted 17 August 2013 - 03:50 PM


for(int index = 0; index < 10; index++)
{
tempA = person[index];
tempB = person[index + 1];

if(tempA > tempB)
{
highest = tempA; 
}

}
cout << highest << " was the most pancakes eaten\n"; // i need to some how edit this line to print out who ate the most
}

So this part is troublesome. When the for-loop hits the final index, i.e 9, your tempB variable will attempt to gain access to person[10] which would mean that you will try to access the 11th(arrays goes from index 0-9!) index of the array. Since you are looping through 10 iterations I'm assuming that person[] is 10 in size, which would mean that the 10th index is out of bounds thus crashing your application.

I'm assuming that person[] is an int array, otherwise highest would not be able to be given tempA value. First of all I would make the person array an array of playerInfo structs, but rename it a bit like the post before me suggested:
 

struct PancakeEater
{
    std::string name;
    int nrOfPancakes;
};

PancakeEater persons[10];

// fill PancakeEater with the correct data.

int highestIndex = 0; // Now this will be the index on which the person with most pancakes eaten would be at.
int mostPancakes = 0; // This will store the amount of pancakes eaten, only used for comparison reasons.

for(int index=0; index<10; index++)
{
    if(persons[index].nrofPancakes> mostPancakes)
    {
      highestIndex = index; //This will store on what index the pancake eater with most pancakes is located in the persons array.
      mostPancakes = persons[index].nrOfPancakes; // store the new highest amount of pancakes eaten.
    }

}

cout << persons[highestIndex].nrOfPancakes<< " was the most pancakes eaten\n and was done by " << persons[highestIndex].name;

Note that it's very late for me, and that this code won't work completely copied. This is also not the best naming convention or the most good looking way to do it, but it doesn't seem like that is your major concern at the moment. I gave you the essential stuff here, hopefully this will help you complete the task. Good luck! smile.png


#3PwFClockWise

Posted 17 August 2013 - 03:49 PM


for(int index = 0; index < 10; index++)
{
tempA = person[index];
tempB = person[index + 1];

if(tempA > tempB)
{
highest = tempA; 
}

}
cout << highest << " was the most pancakes eaten\n"; // i need to some how edit this line to print out who ate the most
}

So this part is troublesome. When the for-loop hits the final index, i.e 9, your tempB variable will attempt to gain access to person[10] which would mean that you will try to access the 11th(arrays goes from index 0-9!) index of the array. Since you are looping through 10 iterations I'm assuming that person[] is 10 in size, which would mean that the 10th index is out of bounds thus crashing your application.

I'm assuming that person[] is an int array, otherwise highest would not be able to be given tempA value. First of all I would make the person array an array of playerInfo structs, but rename it a bit like the post before me suggested:
 

struct PancakeEater
{
    std::string name;
    int nrOfPancakes;
};

PancakeEater persons[10];

// fill PancakeEater with the correct data.

int highestIndex = 0; // Now this will be the index on which the person with most pancakes eaten would be at.
int mostPancakes = 0; // This will store the amount of pancakes eaten, only used for comparison reasons.

for(int i=0; i<10; i++)
{
    if(persons[index].nrofPancakes> mostPancakes)
    {
      highestIndex = index; //This will store on what index the pancake eater with most pancakes is located in the persons array.
      mostPancakes = persons[index].nrOfPancakes; // store the new highest amount of pancakes eaten.
    }

}

cout << persons[highestIndex].nrOfPancakes<< " was the most pancakes eaten\n and was done by " << persons[highestIndex].name;

Note that it's very late for me, and that this code won't work completely copied. This is also not the best naming convention or the most good looking way to do it, but it doesn't seem like that is your major concern at the moment. I gave you the essential stuff here, hopefully this will help you complete the task. Good luck! smile.png


#2PwFClockWise

Posted 17 August 2013 - 03:49 PM


for(int index = 0; index < 10; index++)
{
tempA = person[index];
tempB = person[index + 1];

if(tempA > tempB)
{
highest = tempA; 
}

}
cout << highest << " was the most pancakes eaten\n"; // i need to some how edit this line to print out who ate the most
}

So this part is troublesome. When the for-loop hits the final index, i.e 9, your tempB variable will attempt to gain access to person[10] which would mean that you will try to access the 11th(arrays goes from index 0-9!) index of the array. Since you are looping through 10 iterations I'm assuming that person[] is 10 in size, which would mean that the 10th index is out of bounds thus crashing your application.

I'm assuming that person[] is an int array, otherwise highest would not be able to be given tempA value. First of all I would make the person array an array of playerInfo structs:
 

struct PancakeEater
{
    std::string name;
    int nrOfPancakes;
};

PancakeEater persons[10];

// fill PancakeEater with the correct data.

int highestIndex = 0; // Now this will be the index on which the person with most pancakes eaten would be at.
int mostPancakes = 0; // This will store the amount of pancakes eaten, only used for comparison reasons.

for(int i=0; i<10; i++)
{
    if(persons[index].nrofPancakes> mostPancakes)
    {
      highestIndex = index; //This will store on what index the pancake eater with most pancakes is located in the persons array.
      mostPancakes = persons[index].nrOfPancakes; // store the new highest amount of pancakes eaten.
    }

}

cout << persons[highestIndex].nrOfPancakes<< " was the most pancakes eaten\n and was done by " << persons[highestIndex].name;

Note that it's very late for me, and that this code won't work completely copied. This is also not the best naming convention or the most good looking way to do it, but it doesn't seem like that is your major concern at the moment. I gave you the essential stuff here, hopefully this will help you complete the task. Good luck! smile.png


#1PwFClockWise

Posted 17 August 2013 - 03:47 PM


for(int index = 0; index < 10; index++)
{
tempA = person[index];
tempB = person[index + 1];

if(tempA > tempB)
{
highest = tempA; 
}

}
cout << highest << " was the most pancakes eaten\n"; // i need to some how edit this line to print out who ate the most
}

So this part is troublesome. When the for-loop hits the final index, i.e 9, your tempB variable will attempt to gain access to person[10] which would mean that you will try to access the 11th index of the array. Since you are looping through 10 iterations I'm assuming that person[] is 10 in size, which would mean that the 10th index is out of bounds(going from 0-9) thus crashing your application.

I'm assuming that person[] is an int array, otherwise highest would not be able to be give tempA value. First of all I would make the person array an array of playerInfo structs:
 

struct PancakeEater
{
    std::string name;
    int nrOfPancakes;
};

PancakeEater persons[10];

// fill PancakeEater with the correct data.

int highestIndex = 0; // Now this will be the index on which the person with most pancakes eaten would be at.
int mostPancakes = 0; // This will store the amount of pancakes eaten, only used for comparison reasons.

for(int i=0; i<10; i++)
{
    if(persons[index].nrofPancakes> mostPancakes)
    {
      highestIndex = index; //This will store on what index the pancake eater with most pancakes is located in the persons array.
      mostPancakes = persons[index].nrOfPancakes; // store the new highest amount of pancakes eaten.
    }

}

cout << persons[highestIndex].nrOfPancakes<< " was the most pancakes eaten\n and was done by " << persons[highestIndex].name;

Note that it's very late for me, and that this code won't work completely copied. This is also not the best naming convention or the most good looking way to do it, but it doesn't seem like that is your major concern at the moment. I gave you the essential stuff here, hopefully this will help you complete the task. Good luck! :)


PARTNERS