需要C ++与计算器的帮助 [英] Need help from C++ with calculator

查看:89
本文介绍了需要C ++与计算器的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的DO / WHILE循环不起作用。帮助我也找到了错误,请!!





My DO/WHILE loop doesn't work. Help me too find the mistake, please!!


#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');
}





我的尝试:



我找不到解决方案。错误给出变量exit正在使用而没有初始化。



What I have tried:

I couldn't find the solution for this. The error gives The variable "exit" is being used without initialized.

推荐答案

你的问题的答案是,exit必须像这样初始化:

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



更直观的是以这种方式对循环进行编码:


More intuitive is to code the loop in that way:

while (exit != 'y');



为了更好编程经验我会将你的spaghetti代码重构为一些函数。


For a better programming experience I would refactor your "spaghetti code" into some functions.


学会正确缩进你的代码,它显示它的结构,它有助于阅读和理解。

Learn to indent properly your code, it show its structure and it helps reading and understanding.
#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');
}



专业程序员的编辑器具有此功能,其他功能包括括号匹配和语法高亮。

Notepad++主页 [ ^ ]

ultraedit [ ^ ]


如前所述,您必须初始化退出带有值。



但是你从未在代码中的其他地方设置变量。相反,您正在 wile 循环的末尾读取用户输入到 a 变量。所以你必须改变它:

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');



最后你应该重命名该变量,因为有一个C / C ++标准库函数具有相同的名称。使用与现有函数同名的变量不是错误。但是这种风格很糟糕,并且在调用函数时忘记括号时可能会导致未确认的行为。


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.


这篇关于需要C ++与计算器的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆