Program will not exit a do-while loop

TheBlackSword

I'm trying to make a simple calculator in C++. Here is a portion of the code:

#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main()
{
    int math;
    int a1;
    int a2 = 0;
    int a3 = 1;
    int answer;
    int amount = 0;
    int achecker = 1;

    cout << "Welcome to my calculator! Type '1' to add, type '2' to subtract, "
            "type '3' to multiply, type '4' to divide, and type '5' to exit."
         << endl;
    cin >> math;

    while (math = 1)
    {
        cout << "Input how many numbers you wish to add:" << endl;
        cin >> amount;
        achecker = amount;
        do
        {
            cout << "Input the number you wish to add:" << endl;
            cin >> a1;
            answer = a1 + a2;
            a2 = a1;
            achecker = achecker - achecker + 1;
        } while (achecker < amount);
        cout << answer;
    }

The problem I am encountering is that when the program gets into the do-while loop, it never comes out, it just keeps on asking for the user to input a number. I have gone over this several times and I have no idea what the problem is. Can someone help?

iammrmehul

First of all , you should be writing while(math==1) sicnce math=1 is an assignment operator not a check operator.

Secondly, instead of while , use if since you want to do the calculation for addition only once , putting it in while loop will make it an infinite loop.

Thirdly , in the do - while loop , the condition should be while(achecker>=0), because your condition will always give a true value.So, actually , there is no need of achecker, simply keep decrementing amount by one for each loop run and keep the condition as while(amount>=0) .

One, more improvement i would like to suggest , though not required - declare answer as int answer = 0;. For each loop run , accept a new value in a1 and then for adding , write answer=answer+a1. This should serve your purpose.

So, edited code according to me should be -

#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main()
{
  int math;
  int a1;
  int a3 = 1;
  int answer = 0;
  int amount = 0;
  int achecker = 1;

  cout << "Welcome to my calculator! Type '1' to add, type '2' to subtract, type '3' to      multiply, type '4' to divide, and type '5' to exit." << endl;
  cin >> math;

  if(math == 1){
  cout << "Input how many numbers you wish to add:" << endl;
  cin >> amount;
  do{
  cout << "Input the number you wish to add:" << endl;
  cin >> a1;
  answer = answer + a1;
  amount = amount - 1;
  }while(amount>=0);
  cout << answer;
  }

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to exit a while loop

분류에서Dev

Switch statement within Do-While loop doesn't exit

분류에서Dev

How to fix this basic program? Java do-while loop

분류에서Dev

do while 및 exit 구현

분류에서Dev

How to end program while loop in python

분류에서Dev

Visual Basic "Do While" loop

분류에서Dev

do while loop with input scanner

분류에서Dev

Do-while loop issue

분류에서Dev

Java on JCreator (creating a LEAP YEAR program using while loop)

분류에서Dev

Use a do while loop to access menu

분류에서Dev

Do-while loop does not end?

분류에서Dev

do-while loop in android app

분류에서Dev

How do I get out of this do-while loop?

분류에서Dev

Need a fairly "rude" program exit

분류에서Dev

How do you break a while loop with a keypress in WPF?

분류에서Dev

do, while loop to class 구현 필요

분류에서Dev

How do i stop a while loop after a certain time in C

분류에서Dev

why isnt it staying inside the loop, do while in c

분류에서Dev

the nested while loop do not work, instead it displays unexpected '}'

분류에서Dev

VBA Excel Do While Loop 런타임 오류

분류에서Dev

Can't get a while, or do... while loop to work, looked up multiple answers. nothing works

분류에서Dev

Compile regex only once and cleanup on program exit

분류에서Dev

Program doesn't end after parsing "exit"

분류에서Dev

Why does this program loop?

분류에서Dev

How do I get my logic in this Java program to make my loop work?

분류에서Dev

Translate Python for loop into a while loop

분류에서Dev

oracle update with while loop

분류에서Dev

While loop if condition is not met

분류에서Dev

Optimizing a `while` loop