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;
}
Comments
Used code tags correctly on first post that required them.
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;
}

I see lines 6 through 8 are indented. I assume that means they are part of the if statement on line 5. However, they are not inside brackets, so lines 7 and 8 execute regardless. Do you want that? What if the user enters a bad flipLocation TWICE? They won't have a chance to correct their second bad answer. My suggestion would be to put lines 6 through 8 inside brackets and change the if-statment on line 5 to a while statement so the user can screw up many times and still have a chance to correct.

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;
}

I see lines 6 through 8 are indented. I assume that means they are part of the if statement on line 5. However, they are not inside brackets, so lines 7 and 8 execute regardless. Do you want that? What if the user enters a bad flipLocation TWICE? They won't have a chance to correct their second bad answer. My suggestion would be to put lines 6 through 8 inside brackets and change the if-statment on line 5 to a while statement so the user can screw up many times and still have a chance to correct.

Might want to clarify a do/while otherwise nothing will happen :)

Might want to clarify a do/while otherwise nothing will happen :)

I was actually thinking of a plain old while-loop. A do-while loop asks for input twice even if the original input is good since input is asked for in lines 3 and 4, before any loop would occur. Since an error message would be inside of the loop, you don't want the loop to execute at all if good data is entered initially in lines 3 and 4.

You could change the whole thing to a do-while loop if you put lines 3 and 4 in that loop, deleted lines 7 and 8, and kept lines 5 and 6 as they were, INSIDE the do-while loop.

Hey so one last question:

I'm trying to make it so that the person must keep performing the flip set of functions as long as the elements in the array are not sorted. The way that I'm trying to do that is by making an if statement that keeps running as long as the elements are not in order

do I use an int instead of a bool for isSorted?

and also is the isSorted function that I have correct? In my compiler I keep getting an error that "index" is not defined in that function

Thanks

//Roopak Mitra
//Program lets players flipLocation the pancakes a number of times

#include <iostream>

using namespace std;

void fillArray(double a[], int size, int& numberUsed);
//Precondition: size is the declared size of the array a.
//Postcondition: numberUsed is the number of values stored in a.
//a[0] through a[numberUsed - 1] have been filled with
//nonnegative integers read from the keyboard.
bool isSorted(double a[], int numberUsed);
int flipLocation(int numberUsed,  int flip);
void sort(double a[], int numberUsed, int variable);
//Precondition: numberUsed <= declared size of the array a.
//The array elements a[0] through a[numberUsed - 1] have values.
//Postcondition: The values of a[0] through a[numberUsed - 1] have
//been rearranged so that a[0] <= a[1] <= ... <= a[numberUsed - 1].
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);
	int flip = 0;
	if(!isSorted(sampleArray, numberUsed)) {
		flip = flipLocation(numberUsed, flip);
		int variable = flip;
		sort(sampleArray, numberUsed, variable);
		displayArray(sampleArray, numberUsed);
	}
	cout << "You sorted the pancakes!" << endl;
	cout << "Press any integer key to continue";
	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 variable)
{
	
	int halfFlipSize = variable/2;
	swapValues(a[0], a[variable]);

	for (int index = 1; index <= halfFlipSize; index++)
	{
		swapValues(a[index], a[variable-index]);
	}
}

void swapValues(double& v1, double& v2)
{
	double temp;
	temp = v1;
	v1 = v2;
	v2 = temp;
}

void displayArray(double a[], int numberUsed)
{
	for (int index = 0; index < numberUsed; index++)
	cout << a[index] << " ";
	cout << endl;
}

int flipLocation(int numberUsed, int flip)
{
	
	cout << "Enter flip location ";	
	cin >> flip;
	
	while (flip > numberUsed){
		cout << "Invalid flip location" << endl;
		cout << "Enter a new flip location ";
		cin >> flip;

		return flip;
	}

	return flip;
}

bool isSorted(double a[], int numberUsed) 
{
	for(int index = 0; index < numberUsed - 1 && a[index] < a[index+1]; index++);
		if (index != numberUsed) 
		{
		   return false;
		}
		else return true;
	
}
bool isSorted(double a[], int numberUsed) 
{
	for(int index = 0; index < numberUsed - 1 && a[index] < a[index+1]; index++);
		if (index != numberUsed) 
		{
		   return false;
		}
		else return true;
	
}

This isn't really a loop. Either your "if" statement will execute or your else statement will execute. Both have return statements, so your for-loop will be short-circuited immediately. That's a good thing in your case because if it DIDN'T short-circuit, you wouldn't have a return statement and you are supposed to always return something. In fact, the "else" part will never execute. In order to get INTO the loop, this must be true:

index < numberUsed - 1 && a[index] < a[index+1]

which means this must be true:

index < numberUsed - 1

which means this must be true:

index != numberUsed

You're on the right track as far as having a loop, but your loop is designed incorrectly. There should be no else statement. Have a loop. Inside the loop, see if anything is in the wrong order. If so, it's not sorted, so return false. If and only if it makes it ALL THE WAY THROUGH THE ENTIRE LOOP, it's sorted.

This article has been dead for over six months. Start a new discussion instead.