编译器和exe文件中的输出不同

时间:2015-07-25 15:08:45

标签: c++

我正在使用turbo c ++。当我从编译器运行以下代码时,我会在函数结果(int)中的标记点获得不同的答案,然后从运行的.exe文件中获得。

#include<fstream.h>
#include<conio.h>
#include<process.h>
#include<iomanip.h>
#include<string.h>
#include<stdio.h>
#include<dos.h>

ifstream  fil;
int pos[50];
char date[11];

void exitt(int times = 0)
{
    cout << endl << endl << "     Enter 0 to exit." << endl;
    if (times == 0)
        cout << "     Enter L to return to last screen." << endl;
}
void options();
void companychoose();
void companyscreen(int);
void write(int ch, int pos = 0)                      //add a check for duplicacy
{
    ofstream fout;
    clrscr();
    if (ch == 1)
    {
        fout.open("database.dat", ios::binary | ios::app | ios::ate);
        char companyname[20], temp;
        exitt();
        cout << "     Enter Company name: ";
        gets(companyname);
        if (strcmp(companyname, "0") == 0)
            exit(0);
        else if (strcmp(companyname, "l") == 0)
            options();
        for (int i = 19; i>0; i--)
            companyname[i] = companyname[i - 1];
        companyname[0] = '%';
        fout << endl;
        fout << companyname;
        fout.close();
        cout << "     Add data now?(y/n)" << endl;
    askagain:
        cin >> temp;
        switch (temp)
        {
        case 'y':
            fil.close();
            write(2);
            break;
        case 'n':
            options();
            break;
        default:
            cout << "    Invalid input" << endl;
            goto askagain;
            break;
        }
    }
}

void result(int ch)
{
    int high[4], low[4], end, i = 0, enough = 0, temp = 0;
    char check[20];
    fil.open("database.dat", ios::binary);
    fil.seekg(pos[ch], ios::beg);
    fil >> check;
    cout << endl;
    if (check[0] == '%')
    {
        cout << "    Not Enough Data!!!" << endl;
        fil.close();
        return;
    }
    while (!fil.eof())
    {
        if (i == 3)
        {
            i = 0;
            enough = 1;
        }
        fil >> high[i] >> low[i] >> end >> check;
        if (check[0] == '%')
            break;
        i++;
    }
    low[i] = 0;
    temp = low[0];
    if (enough == 0)
        cout << "    Not Enough Data!!!" << endl;
    else
    {
        for (i = 0; i<3; i++)
        {
            if (low[i]<low[i + 1])
                temp = low[i + 1];
        }
        if (temp>end)
            cout << "     Stock Running Low!!";
        else if (temp = end)
            cout << "     Stock Is Stable";
        else
            cout << "     Stock is HIGH!!";
        cout << "  " << end - temp << endl << endl << endl;
    }
    fil.close();
}

int read(int ch, int find = 0)
{
    clrscr();
    result(ch);
    fil.open("database.dat", ios::binary);
    fil.seekg(pos[ch], ios::beg);
    char entry[20];
    fil >> entry;
    cout << setw(20) << "Date" << setw(10) << "High" << setw(10) << "Low" << setw(10) << "Close" << endl;
    while (entry[0] != '%')
    {
        if (find == 1)
        {
            if (strcmp(entry, date))
                return(fil.tellg() - 11);
            else
                continue;
        }
        cout << setw(20) << entry;
        fil >> entry;
        cout << setw(10) << entry;
        fil >> entry;
        cout << setw(10) << entry;
        fil >> entry;
        cout << setw(10) << entry << endl;
        fil >> entry;
        delay(500);
    }
    fil.close();
    getch();
    clrscr();
    companyscreen(ch);
}

void edit(int ch)
{
    cout << "Enter date of data to be edited";
    gets(date);
    write(2, read(ch, 1));
}

void companyscreen(int ch)
{
    int ch1;
askagain:
    result(ch);
    cout << "    1. Add Data" << endl;
    cout << "    2. Show history" << endl;
    cout << "    3. Edit Data" << endl;
    exitt();
    ch1 = getch() - 48;
    if (ch1 == 1)
        write(2);
    else if (ch1 == 2)
        read(ch);
    else if (ch1 == 3)
    {
        read(ch);
        edit(ch);
    }
    else if (ch1 == 0)
    {
        cout << "   exiting!!" << endl;
        exit(500);
    }
    else if (ch1 == 60)
        companychoose();
    else
    {
        cout << "     Invalid option chosen" << endl;
        getch();
        clrscr();
        goto askagain;
    }
}

void companychoose()
{
    char name[20];
    int i, ch;
    clrscr();
    fil.open("database.dat", ios::binary);
askagain:
    fil.seekg(0, ios::beg);
    cout << "     Choose Company:";
    cout << endl;
    i = 1;
    while (!fil.eof())
    {
        fil >> name;
        if (name[0] == '%')
        {
            name[0] = ' ';
            pos[i] = fil.tellg();
            cout << setw(10) << i << "." << name << endl;
            i++;
        }
    }
    fil.close();
    exitt();
    ch = getch() - 48;
    if (ch == 0)
        exit(0);
    else if (ch == 60)
        options();
    else if (ch>i)
    {
        cout << "Invalid choice" << endl;
        getch();
        clrscr();
        goto askagain;
    }
    clrscr();
    companyscreen(ch);
}

void options()
{
    int ch;
    clrscr();
askagain:
    cout << endl << endl;
    cout << "       1. Add company" << endl;
    cout << "       2. Choose company" << endl;
    exitt(1);
    ch = getch() - 48;
    if (ch == 1)
        write(1);
    else if (ch == 2)
        companychoose();
    else if (ch == 0)
    {
        cout << setw(10) << "      Exiting!!";
        exit(500);
    }
    else
    {
        cout << setw(10) << "      Invalid choice chosen" << endl;
        getch();
        clrscr();
        goto askagain;
    }
}

void main()
{
    clrscr();
    textbackground(MAGENTA);
    textcolor(WHITE);
    clrscr();
    options();
    getch();
}

请注意,程序尚未完全完成,因此某些功能无效。

我不知道如何在此处包含dat文件数据和截图。

我没有使用visual c ++导致我的电脑很慢。 我没有使用代码块,因为我不知道如何使用它。即使在使用命名空间std;&#34;

添加&#34;上面的代码也会产生数百个错误

请帮我解决一下。如果你还需要别的话请问我。谢谢

1 个答案:

答案 0 :(得分:1)

  

我在函数result(int)中获得了不同的答案(...)然后从运行的.exe文件中获得了什么。

当您从IDE执行程序时,使用了不同的working directory,因此看似存在/丢失的文件不同。通常工作目录是可配置的。

顺便说一句,不需要goto。真的,it is not