Hey everyone,
I've been working on a program that "flips" a stack of pancakes for the user depending on where they want to flip them.
the problem that I'm having with the code is that I can't quite figure out how to "error check" the users input for flip location. I tried making a separate function called flipLocation to check if the flip location is less than the amount of elements in the array and its not working.
Thanks for the help
//Roopak Mitra
//Program lets players flip the pancakes a number of times
#include <iostream>
using namespace std;
void fillArray(double a[], int size, int& numberUsed);
int flipLocation(int numberUsed);
void sort(double a[], int numberUsed, int flipLocation);
void swapValues(double& v1, double& v2);
void displayArray(double a[], int numberUsed);
int main( )
{
int acog = 0;
const int Max_Value = 50;
double sampleArray[Max_Value];
int numberUsed;
fillArray(sampleArray, Max_Value, numberUsed);
flipLocation(numberUsed);
sort(sampleArray, numberUsed, flipLocation);
displayArray(sampleArray, numberUsed);
cin >> acog;
return 0;
}
void fillArray(double a[], int size, int& numberUsed)
{
cout << "Enter pancake diameters (-1 to end entering) ";
double next = 0.0;
int index = 0;
cin >> next;
while ((next >= 0) && (index < size))
{
a[index] = next;
index++;
cin >> next;
}
numberUsed = index;
}
void sort(double a[], int numberUsed, int flipLocation)
{
int halfFlipSize = flipLocation/2;
swapValues(a[0], a[flipLocation]);
for (int index = 1; index <= halfFlipSize; index++)
{
swapValues(a[index], a[flipLocation-index]);
}
}
void swapValues(double& v1, double& v2)
{
double temp;
temp = v1;
v1 = v2;
v2 = temp;
}
void displayArray(double a[], int numberUsed)
{
cout << "After flipping the pancakes, the order is ";
for (int index = 0; index < numberUsed; index++)
cout << a[index] << " ";
cout << endl;
}
int flipLocation(int numberUsed)
{
cout << "Enter flip location ";
cin >> flipLocation;
if (flipLocation > numberUsed)
cout << "Invalid flip location";
cout << "Enter a new flip location ";
cin >> flipLocation;
return flipLocation;
}