-5

i cant figure out where to put all the loop parts for a simple y/n (repeat/exit) loop.i tried to find answers, but none are clear enough for my particular case. P.S. iam a beginner at coding, so please dont make it too complicated unless necessary this is my code so far

#include <stdio.h>
#include <iostream>
using namespace std;

// input function
void Input (float &x, float &y);

float a=1.0, b=1.0, result;
char operation;
char yesNO; 

int main ()

{
do {
    cout << "Programma wat optelt, aftrekt, vermedigvuldigd en deelt. \n\n";

    cout << "Geef een opdracht (eg. 1 + 2): \n";
    cin >> a >> operation >> b;

    Input (a,b);

    cout << "Het antwoord is: " << result << endl;
    system ("pause");
    return 0;
}
 while (yesNO == 'y');
void Input (float &x, float &y)
{
    a = x;
    b = y;

    switch (operation)
    {
        case '+':
            result = x + y;
            break;

        case '-':
            result = x - y;
            break;

        case '*':
            result = x * y;
            break;

        case '/':
            result = x / y;
            break;

        default:
            cout << "foutieve invoer: \n";
            cin >> a >> operation >> b;
            Input (a, b);
    }
  }
 }
Max
  • 3
  • 2
  • 1
    [using std namespace is bad see here](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice) – Ed Heal Jun 06 '16 at 20:22
  • The `while` loop is in about the right place, but you do not want the `return` inside the loop and you do want something that sets `yesNO` inside the loop. – user4581301 Jun 06 '16 at 20:25
  • Looks like you messed up your scopes. You can't declare `Input()` inside of `main()`. – πάντα ῥεῖ Jun 06 '16 at 20:28
  • If you sort out the indentation then things become apparent what is wrong. Also using global variables is not a good idea – Ed Heal Jun 06 '16 at 20:30

2 Answers2

-1

I'll ignore some of the things wrong with your program and answer the question directly.

Two things:

  1. You are never asking the user if they want to continue
  2. You are aborting your loop by returing out of main()

So replace these 2 lines:

system ("pause");
return 0;

with a query that asks the user if they want to continue, and populate the variable yesNO with their answer.

zdan
  • 28,667
  • 7
  • 60
  • 71
-2

It stops because of the return statement in "int main". I would suggest using "void main ()" instead of "int main ()". But if you want to use "int main ()", shift the "return 0" below the while statement. You also need to ask the user if he or she wants to continue. Try this: (Ignore the bad spacing)

int main () {
    do {
        cout << "Programma wat optelt, aftrekt, vermedigvuldigd en deelt. \n\n";

        cout << "Geef een opdracht (eg. 1 + 2): \n";
        cin >> a >> operation >> b;

        Input (a,b);

        cout << "Het antwoord is: " << result << endl;
        cout << "Press y to continue: ";
        cin >> yesNo;
    } while (yesNO == 'y');
    return 0;
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190