I have a switch statement with 2 functions. I put 2 test cout statements, both of which get displayed, but only one function is actually performed. A third test cout statement is made in the second function, but that does not get displayed. "switch" and "switch2" get displayed.

Yes, I am new to programming.

Thanks for your help, been scratching my head on this for a while.

int main ()
{
	char a;
	int min, max, num1;
	min = 0;
	max = 0;
	num1 = 0;
	cout << "Please select from the following:\n\n"
		 << setw(10) << " " << setw(15) << "(S) Square "
		 << setw(10) << " " << setw(15) << "(C) Cube " << endl << endl
		 << setw(10) << " " << setw(15) << "(F) Fourth Power"
		 << setw(10) << " " << setw(15) << "(Q) Quit " << endl;
	cin >> a;
	while (!cin.good())
	{
		cout << "Please try again." << endl;
		cin.clear();
		cin.ignore(80, '\n');
		cin >> a;
	}

	switch (a)
	{
	case 's':
	case 'S':
		minMax (&num1, &min, &max);
                cout << "switch" << endl;
		square (&num1, &min, &max);
                cout << "switch2" << endl;
		break;
	case 'c':
	case 'C':
		minMax (&num1, &min, &max);
		cube ( &num1,  &min,  &max);
		break;
	case 'f':
	case 'F':
		minMax (&num1, &min, &max);
		fourth ( &num1,  &min,  &max);
		break;
	default:
		cout << "switch3" << endl;
		return 0;
		break;
	}
}


int square (int *num1, int *min, int *max)
{
	int a,b,c,line;
	b = *min;
	c = *max;
	a = *num1;
	line = 0;
	while (a <= c)
	{
		cout << "square" << endl;
		b = a;
		a = a * a;
		output (&a);
		line++;	
		if (line = 10)
		{
			cout << endl;
			line = 0;
		}
	}
	return a, b, c;
}

Recommended Answers

All 2 Replies

I don't see anything that would cause the problem you report, but I do see problems with that square() function. In main() if you first select 'S', the value of the three variables are all 0. I don't know what minMax does, but assume it finds the minimum and maximum values. Since they are all 0 then min == 0 and max == 0. Now the square() function. Since a == b == c == 0 you have an infinite loop. The next thing that is wrong is the last return statement. You can not return three values from a function -- well I guess you can but only the last value will be returned.

Here is minMax, it requests the min and max #'s from the user

int minMax (int *num1, int *min, int *max)
{
	int a, b, c;
	
	
	b = *min;
	c = *max;
	cout << "Enter a minimum integer (0-100): " << endl;
	cin >> b;
	*num1 = b;
	a = b;
	
	while (!cin.good() || b > 100 || b < 0)
			{
				cout << "Please try again." << endl;
				cin.clear();
				cin.ignore(80, '\n');
				cin >> b;
	}

	cout << "Enter a maximum integer (0-100): " << endl;
	cin >> c;
	while (!cin.good() || c > 100 || c < b)
		{
			cout << "Please try again." << endl;
			cin.clear();
			cin.ignore(80, '\n');
			cin >> c;
	}
	
	return a, b, c;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.