Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My DO/WHILE loop doesn't work. Help me too find the mistake, please!!


C++
#include<iostream>
#include<conio.h>
#include<process.h>
using namespace std;

int main()
{  
	char exit;
  float a,b,result;
  int s1,s2,s3,s4,s5;
	do {
  cout<<"========== WELCOME TO MY CALCULATOR ==========\n1.Arithmetic operations\n2.Trigonometric Functions\n3.Logarithmic Functions\n4.Power Functions\n5.Exit\n";
  cin>>s1;

  switch (s1) {
  case 1:
	cout<<"1.Addition"<<endl;
	cout<<"2.Substraction"<<endl;
	cout<<"3.Multiplication"<<endl;
	cout<<"4.Division"<<endl;
	cout<<"Choose operator by number:";
	cin>>s2;
	switch (s2) {

	  case 1: 
	     cout<<"Enter first value: ";
	     cin>>a;
	     cout<<"Enter second value: ";
	     cin>>b;
	     result=a+b;
	     cout<<"Your result is: "<<result<<endl;
	     break;

	  case 2: 
	     cout<<"Enter first value: ";
	     cin>>a;
	     cout<<"Enter second value: ";
	     cin>>b;
	     result=a-b;
	     cout<<"Your result is: "<<result;
	     break;

	  case 3:
		cout<<"Enter first value: ";
	    cin>>a;
	    cout<<"Enter second value: ";
	    cin>>b;
	    result=a*b;
	    cout<<"Your result is: "<<result;
	    break;
	
	  case 4:cout<<"Enter first value: ";
	    cin>>a;
	    cout<<"Enter second value: ";
	    cin>>b;
	    result=a/b;
	    cout<<"Your result is: "<<result;
	    break;
}
break;

	case 2:
    cout<<"1.Sin function"<<endl;
	cout<<"2.Cos function"<<endl;
	cout<<"3.Tan function"<<endl;
	cout<<"Choose operator by number:"<<endl;
	cin>>s3;
	switch (s3) {

	  case 1: 
	     cout<<"Enter a number: ";
	     cin>>a;
	     result=(sin(a));
	     cout<<"Your result is: "<<result<<endl;
	     break;

	  case 2: 
	     cout<<"Enter a number: ";
	     cin>>a;
	     result=(cos(a));
	     cout<<"Your result is: "<<result<<endl;
	     break;

	  case 3:
		 cout<<"Enter a number: ";
	     cin>>a;
	     result=(tan(a));
	     cout<<"Your result is: "<<result<<endl;
	     break;
}
break;

	case 3:
    cout<<"1.Natural log"<<endl;
	cout<<"2.Log with base 10 "<<endl;
	cout<<"Choose operator by number:"<<endl;
	cin>>s4;
	switch (s4) {

		 case 1: 
	     cout<<"Enter a number: ";
	     cin>>a;
	     result=(log(a));
	     cout<<"Your result is: "<<result<<endl;
	     break;

	     case 2: 
	     cout<<"Enter a number: ";
	     cin>>a;
	     result=(log10(a));
	     cout<<"Your result is: "<<result<<endl;
	     break;
}
break;

	case 4:
    cout<<"1.Power"<<endl;
	cout<<"2.Square root"<<endl;
	cout<<"Choose operator by number:"<<endl;
	cin>>s5;
	switch (s5) {
		
		 case 1: 
	     cout<<"Enter first value: ";
	     cin>>a;
	     cout<<"Enter second value: ";
	     cin>>b;
	     result=pow(a,b);
	     cout<<"Your result is: "<<result<<endl;
	     break;

	     case 2: 
	     cout<<"Enter a number: ";
	     cin>>a;
	     result=sqrt(a);
         cout<<"Your result is: "<<result<<endl;
	     break;
}
break;

	case 5:{
		cout<< "Are you sure you want to leave this program? Press Y to stay or else press any key to leave " << endl;
		break;
		   }
		   break;
}
cout<< "Press Y to continue or else press any key to leave program:";
cin>>a;
}while (exit == 'y');
}


What I have tried:

I couldn't find the solution for this. The error gives The variable "exit" is being used without initialized.
Posted
Updated 13-Nov-17 1:07am
v2
Comments
Richard MacCutchan 13-Nov-17 6:49am    
Get rid of all that duplicated code. Ask for the numbers and operators first, and check that they are valid. Only then do you need to use the switch block to do the calculations. And only at the end of the switch block do you need to print the result.

The answer to your question is, that the "exit" must be initialized like this:
C++
char exit = 'n';//no exit

More intuitive is to code the loop in that way:
C++
while (exit != 'y');

For a better programming experience I would refactor your "spaghetti code" into some functions.
 
Share this answer
 
Learn to indent properly your code, it show its structure and it helps reading and understanding.
C++
#include<iostream>
#include<conio.h>
#include<process.h>
using namespace std;

int main()
{
  char exit;
  float a,b,result;
  int s1,s2,s3,s4,s5;
  do {
    cout<<"========== WELCOME TO MY CALCULATOR ==========\n1.Arithmetic operations\n2.Trigonometric Functions\n3.Logarithmic Functions\n4.Power Functions\n5.Exit\n";
    cin>>s1;

    switch (s1) {
    case 1:
      cout<<"1.Addition"<<endl;
      cout<<"2.Substraction"<<endl;
      cout<<"3.Multiplication"<<endl;
      cout<<"4.Division"<<endl;
      cout<<"Choose operator by number:";
      cin>>s2;
      switch (s2) {

      case 1:
        cout<<"Enter first value: ";
        cin>>a;
        cout<<"Enter second value: ";
        cin>>b;
        result=a+b;
        cout<<"Your result is: "<<result<<endl;
        break;

      case 2:
        cout<<"Enter first value: ";
        cin>>a;
        cout<<"Enter second value: ";
        cin>>b;
        result=a-b;
        cout<<"Your result is: "<<result;
        break;

      case 3:
        cout<<"Enter first value: ";
        cin>>a;
        cout<<"Enter second value: ";
        cin>>b;
        result=a*b;
        cout<<"Your result is: "<<result;
        break;

      case 4:cout<<"Enter first value: ";
        cin>>a;
        cout<<"Enter second value: ";
        cin>>b;
        result=a/b;
        cout<<"Your result is: "<<result;
        break;
      }
      break;

    case 2:
      cout<<"1.Sin function"<<endl;
      cout<<"2.Cos function"<<endl;
      cout<<"3.Tan function"<<endl;
      cout<<"Choose operator by number:"<<endl;
      cin>>s3;
      switch (s3) {

      case 1:
        cout<<"Enter a number: ";
        cin>>a;
        result=(sin(a));
        cout<<"Your result is: "<<result<<endl;
        break;

      case 2:
        cout<<"Enter a number: ";
        cin>>a;
        result=(cos(a));
        cout<<"Your result is: "<<result<<endl;
        break;

      case 3:
        cout<<"Enter a number: ";
        cin>>a;
        result=(tan(a));
        cout<<"Your result is: "<<result<<endl;
        break;
      }
      break;

    case 3:
      cout<<"1.Natural log"<<endl;
      cout<<"2.Log with base 10 "<<endl;
      cout<<"Choose operator by number:"<<endl;
      cin>>s4;
      switch (s4) {

      case 1:
        cout<<"Enter a number: ";
        cin>>a;
        result=(log(a));
        cout<<"Your result is: "<<result<<endl;
        break;

      case 2:
        cout<<"Enter a number: ";
        cin>>a;
        result=(log10(a));
        cout<<"Your result is: "<<result<<endl;
        break;
      }
      break;

    case 4:
      cout<<"1.Power"<<endl;
      cout<<"2.Square root"<<endl;
      cout<<"Choose operator by number:"<<endl;
      cin>>s5;
      switch (s5) {

      case 1:
        cout<<"Enter first value: ";
        cin>>a;
        cout<<"Enter second value: ";
        cin>>b;
        result=pow(a,b);
        cout<<"Your result is: "<<result<<endl;
        break;

      case 2:
        cout<<"Enter a number: ";
        cin>>a;
        result=sqrt(a);
        cout<<"Your result is: "<<result<<endl;
        break;
      }
      break;

    case 5:{
      cout<< "Are you sure you want to leave this program? Press Y to stay or else press any key to leave " << endl;
      break;
      }
      break;
    }
    cout<< "Press Y to continue or else press any key to leave program:";
    cin>>a;
  }while (exit == 'y');
}

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
 
Share this answer
 
As already mentioned you have to initialise exit with a value.

But you never set the variable elsewhere in your code. Instead, you are reading user input at the end of the wile loop into the a variable. So you have to change that too:
char exit = 'y';

// ...

    case 5: //{
        // This is useless here because you will print a similar message below.
        //cout<< "Are you sure you want to leave this program? Press Y to stay or else press any key to leave " << endl;
        //break;
        //}
        break;
    }
    cout<< "Press Y to continue or else press any key to leave program:";
    // You have to assign the input to exit here instead:
    //cin>>a;
    cin >> exit;
}
// The user might also enter an uppercase 'Y'!
while (exit == 'y' || exit == 'Y');

Finally you should rename that variable because there is a C/C++ standard library function with the same name. It is not an error to use a variable with the same name as an existing function. But it is bad style and may lead to unreckoned behaviour when forgetting the parentheses when the function should be called.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900