How do i stop my loop from repeating?

When I perform a function, the case will keep repeating itself. How do i stop that so I can input a new function?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <cmath>

using namespace std;

int main()
{

	char function;
	double val;


	cout << "This calculator requires you to enter a funtion and a number" << endl;
	cout << "The functions are as follows: " << endl;
	cout << "S - sine " << endl;
	cout << "C - cosine " << endl;
	cout << "T - tangent " << endl;
	cout << "R - square root " << endl;
	cout << "N - natural log " << endl;
	cout << "X - exit the program " << endl;

	cout << endl;

	cout << "Please enter a function and a value " << endl;
	cin >> function;
	cin >> val;

	function = (toupper(function));

	while (toupper(function) != 'x')
	{

		double theSin = sin(val);
		double theCos = cos(val);
		double theTan = tan(val);
		double theSqrt = sqrt(val);
		double theLog = log(val);

		switch (function)
		{
		case 'S':
			cout << "The sine of your number is " << theSin << endl;
			break;
		case 'C':
			cout << "The cosine of your number is " << theCos << endl;
			break;
		case 'T':
			cout << "The tangent of your number is " << theTan << endl;
			break;
		case 'R':
			cout << "The square root of your number is " << theLog << endl;
		case 'X':
			exit(0);

		}

	}




	return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <cmath>

using namespace std;

int main()
{

	char function;
	double val;

    do
    {
	    cout << "This calculator requires you to enter a funtion and a number" << endl;
	    cout << "The functions are as follows: " << endl;
	    cout << "S - sine " << endl;
	    cout << "C - cosine " << endl;
	    cout << "T - tangent " << endl;
	    cout << "R - square root " << endl;
	    cout << "N - natural log " << endl;
	    cout << "X - exit the program " << endl;

	    cout << endl;

	    cout << "Please enter a function and a value " << endl;
	    cin >> function;
	    cin >> val;

	    function = (toupper(function));

		double theSin = sin(val);
		double theCos = cos(val);
		double theTan = tan(val);
		double theSqrt = sqrt(val);
		double theLog = log(val);

		switch (function)
		{
		case 'S':
			cout << "The sine of your number is " << theSin << endl;
			break;
		case 'C':
			cout << "The cosine of your number is " << theCos << endl;
			break;
		case 'T':
			cout << "The tangent of your number is " << theTan << endl;
			break;
		case 'R':
			cout << "The square root of your number is " << theLog << endl;
		case 'X':
			exit(0);

		}

	} while (function != 'x');




	return 0;
}

thank you for the help. But i am not allowed to use the do while loop for this project.
closed account (E0p9LyTq)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <cmath>

int main()
{

   char function = ' ';
   double val = 0;
   double theResult;

   std::cout << "This calculator requires you to enter a funtion and a number\n";
   std::cout << "The functions are as follows:\n";
   std::cout << "S - sine\n";
   std::cout << "C - cosine\n";
   std::cout << "T - tangent\n";
   std::cout << "R - square root\n";
   std::cout << "N - natural log\n";
   std::cout << "X - exit the program\n";

   while (function != 'X') // notice X is uppercase, not lowercase
   {
      std::cout << "\nPlease enter a function and a value: ";
      std::cin >> function;
      std::cin >> val;

      function = (toupper(function));

      switch (function)
      {
         case 'S':
            theResult = sin(val);
            std::cout << "The sine of your number is " << theResult << "\n";
            break;

         case 'C':
            theResult = cos(val);
            std::cout << "The cosine of your number is " << theResult << "\n";
            break;

         case 'T':
            theResult = tan(val);
            std::cout << "The tangent of your number is " << theResult << "\n";
            break;

         case 'R':
            theResult = sqrt(val);
            std::cout << "The square root of your number is " << theResult << "\n";;
            break;

         case 'N':
            theResult = log(val);
            std::cout << "The natural log of your number is " << theResult << "\n";
            break;

         case 'X':
            break;
      }
   }
   return 0;
}
Last edited on
Topic archived. No new replies allowed.