-1

Okay, so I've looked around and I still do not quite understand why I am getting this error. My code is included below. I had some older code that was working just fine. Then I decided to make it so you could perform more than one calculation per opening of the application. After fixing several other errors, this one popped up. This one had popped up after I realized I needed ' ' around the y.

#include "stdafx.h"
#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;
int main(int nNumberofArgs, char* pszArgs[])

{

    int x;
    int y;
    int result;
    char z;
    char a = 'y';

    char st();
    {
        cout << ("Do another calculation?: y/n");
        cin >> a;
        if (a = 'n')
        {
            system("PAUSE");
            return 0;
        }
    }


    while (a = 'y');
    {
        cout << "Symbol here: " <<endl;
        cin >> z;
        cout << "Number 1: " <<endl;
        cin >> y;
        cout << "Number 2: " <<endl;
        cin >> x;
        if (z == '*')
        {
            result = x * y;
            cout << "Answer:" << result <<endl;
            st();
        }
        else if (z == '/')
        {
            result = x * y;
            cout << "Answer:" << result <<endl;
            st();
        }
        else if (z == '-')
        {
            result = x / y;
            cout << "Answer:" << result <<endl;
            st();
        }
        else if (z == '+')
        {
            result = x + y;
            cout << "Answer:" << result <<endl;
            st();
        }
        else if (z == '%')
        {
            result = y % x;
            cout << "Answer:" << result <<endl;
            st();
        }
    }
}
Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74
Chris Altig
  • 680
  • 3
  • 8
  • 22

1 Answers1

1

you have a semi-colon at the end of char st(). This declares the function but does not define it. The code that comes after the declaration becomes part of main and gets executed the first time you start up. This is why you probably haven't noticed it.

You need to move st() out of main as local function definitions are illegal in C++.

using namespace std;

char st()
{
    cout << ("Do another calculation?: y/n");
    cin >> a;
    if (a = 'n')
    {
        system("PAUSE");
        return 0;
    }
}


int main(int nNumberofArgs, char* pszArgs[])
{
    int x;
    int y;

    // ... rest of your code

    return 0;
}
Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74
  • Thanks! that helped a ton! But, now when I launched the application (debugger saw no errors) it just shows a plain black screen. Do you know why this happened? – Chris Altig May 19 '13 at 05:25
  • Yes, when you send data to `std::cout` you need to pass `std::endl` at the end to flush the output buffers. `std::cout << "some data" << std::endl;` – Captain Obvlious May 19 '13 at 05:44