简体   繁体   中英

Why the result doesn't write on the text file?

I have created csis.txt file but when I look up, it is empty. Could anyone teach me why the file is created without results? Here are my codes:

//ZipCode.cpp

#include <iostream>
#include "ZipCode.h"
#include <fstream>
#include <string>
#include <math.h> 
#include <sstream>

using namespace std;

extern ofstream csis;

    ZipCode::ZipCode(int _zipcode) {
    zipcode = _zipcode;
    }

    ZipCode::ZipCode(const char *barcode) {
        int value;
        zipcode = 0;
        for (int i = 0; i<5; i++) //repeat x5 positions
        {
            value = 0;
            for (int j = 0;j<5;j++) //repeat x5 letters
            {
                if (barcode[i * 5 + j] == '1') //if '1' is letter
                {
                    value += base[j]; //add position(5) 
                }
            }
            if (value >10)
            {
                value = 0; 
            }

            zipcode = zipcode * 10 + value;
        }
    }

    string ZipCode::getBarCode()
    {
        char barcode[26] = "";
        int zip = zipcode;
        int value;
        for (int i = 0;i<5;i++) //repeat x5 positions
        {
            for (int j = 0;j<4;j++) //repeat x4(last barcode position 0) 
            {
                //10^position * barcode position goes to value 
                value = (int)(pow(10.0, 4 - i) * base[j]);
                if (zip / value)
                {
                    barcode[i * 5 + j] = '1'; //set as '1' 
                    zip = zip % value;//zip is now remainder
                }
                else
                {
                    barcode[i * 5 + j] = '0'; //set as '0'
                }
            }

            barcode[i * 5 + 4] = '0';
        }
        return barcode;
    }

    int ZipCode::getZipCode()
    {
        return zipcode;
    }




  //ZipCode.h
#ifndef _ZIPCODE_H
#define _ZIPCODE_H

using namespace std;

class ZipCode {
    private: 
        int zipcode;
        int base[5] = { 7,4,2,1,0 }; //barcode positions
    public: 
        ZipCode(int _zipcode); //convert zipcode to barcode
        ZipCode(const char *barcode); //convert barcode to zipcode
        string getBarCode(); //get barcode
        int getZipCode(); //get zipcode
};

#endif



//main.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include "ZipCode.h"
#include <sstream>

using namespace std;

ofstream csis;

int main()
{
    csis.open("csis.txt");
        ZipCode zip1(99504);
        ZipCode zip2(12345);
        ZipCode zip3(67890);
        ZipCode zip4("100101010011100001100110001");
        ZipCode zip5("110100001011100001100010011");
        ZipCode zip6("100011000110101000011100101");

        cout << "Digits" << "       " << "Bar Code" << endl;
        cout << zip1.getZipCode() << setw(35) << zip1.getBarCode() << endl;
        cout << zip2.getZipCode() << setw(35) << zip2.getBarCode() << endl;
        cout << zip3.getZipCode() << setw(35) << zip3.getBarCode() << endl;
        cout << endl;
        cout << zip4.getZipCode() << setw(35) << zip4.getBarCode() << endl;
        cout << zip5.getZipCode() << setw(35) << zip5.getBarCode() << endl;
        cout << zip6.getZipCode() << setw(35) << zip6.getBarCode() << endl;
        return 0;
    }

You probably want to write the file rather than std::cout.

Just replace :

cout << "Digits" << "       " << "Bar Code" << endl;

by

csis << "Digits" << "       " << "Bar Code" << endl;`

And repeat that for every line...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM