Hi!

I am trying to make a program, where it opens a text file. If it reads, for instance the "UP and the 2 #s after" in the file, it will print, on the same line (with the data) a result of another #.

for instance:

(command, code, and code2 given in text file)

COMMAND CODE #1 CODE#2 RESULT
UP 10010110 01110101 10110110

what commands can i use to display the result in a certain space (array?), but beforehand initiate by reading the command?

This is what I have so far.

#include <fstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;

/*===============================================================*/
void greeting();
void columntitle();
//void not(int i, const int [SIZE] = 7, int op1 [SIZE]);
/*===============================================================*/

int main()
{
const string fileName = "text.txt";
const int SIZE= 7;
char input[SIZE];
char runs;
char commands;
fstream inFile;
char name;

greeting();

columntitle();

//open the file for output
inFile.open(fileName);		

while(!inFile.eof())
{
		inFile.get(commands);
		cout << "" << commands;
}


inFile.close();



return 0;
}

/*==========================================================*/

void greeting()
{
cout << "*********************************************" << endl;
cout << "*                                           *" << endl;
cout << "*                    Title                  *" << endl;
cout << "*                                           *" << endl;
cout << "*********************************************" << endl << endl;
}

void columntitle()
{
cout << left << setw(11) << "COMMAND" << setw(14) << "Code #1" << setw(14) << "Code #2" << setw(9) << "Shift" <<"Result" << endl;
cout << setfill('-') << setw(58) << "-" << endl;
}

Please use code tags when posting code. Also, try to remove any code that is not related to the problem. For example here the greeting() function is unnecessary. You will find that the shorter you can make the problem, the more likely people are to look at it :)

You need to parse the input. There are two simple methods, starting with straight operator>>:

#include <fstream>
#include <iostream>
#include <string>

int main()
{
    std::ifstream in("test.txt");

    if (in) {
        std::string field;

        while (in>> field) {
            if (field == "UP")
                std::cout.put('\n');
            else
                std::cout<< field <<'\t';
        }
    }
}

The idea being that you can always determine the beginning of a new row since it has a consistent row header. Another way is to read the line as a whole and then parse it in memory:

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

int main()
{
    std::ifstream in("test.txt");

    if (in) {
        std::string line;

        while (std::getline(in, line)) {
            std::istringstream iss(line);
            std::string field;

            while (iss>> field) {
                if (field == "UP")
                    continue;

                std::cout<< field <<'\t';
            }

            std::cout.put('\n');
        }
    }
}

The benefit is that you don't have to do any format trickery to determine where a line begins and ends. Such a solution is well suited for when it's hard to figure out where the line breaks are, or if you simply want the added flexibility of doing your own parsing.

Thank you!

How would you take the 8 digit number and sotre it into memory as well?
like each number in each space?

It depends on what you want to do with it. If you want to store it exactly like you see it, store it in an std::string. If you are doing to need to do operations on it, you would need to choose an appropriate data type.

How would you take the 8 digit number and sotre it into memory as well?

It's already stored in memory, but that variable is overwritten when you read another value. To retain it, a vector would be a good option:

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

int main()
{
    typedef std::vector<std::string> line_t;
    typedef std::vector<line_t> file_t;

    std::ifstream in("test.txt");

    if (in) {
        std::string line;
        file_t lines;

        // Build the vector from file input
        while (std::getline(in, line)) {
            std::istringstream iss(line);
            std::string field;

            lines.push_back(line_t());

            while (iss>> field) {
                if (field == "UP")
                    continue;

                lines.back().push_back(field);
            }
        }

        // Display the results
        file_t::const_iterator file_it = lines.begin();
        file_t::const_iterator file_end = lines.end();

        while (file_it != file_end) {
            line_t::const_iterator line_it = file_it->begin();
            line_t::const_iterator line_end = file_it->end();

            while (line_it != line_end)
                std::cout<< *line_it++ <<'\t';

            std::cout.put('\n');
            ++file_it;
        }
    }
}

Though depending on your needs, it may be better to read and process only a single line at a time. Especially when files are large, it becomes impractical to hold the whole thing in memory at once.

Thanks!

So far I have

int main()
{
const string fileName = "binary.txt";
const int SIZE= 8;
int op1 [SIZE];
int op2 [SIZE];
fstream inFile;
int runs;
int result;
int i;
int j;

string command;

greeting();

columntitle();

//open the file for output
inFile.open(fileName);		

while(!inFile.eof())
{
	while (inFile >> command)
	{
		inFile >> op1[i];
	if (command == "CONVERT")
	{
		for (i=0 ; i<7 ; i++)
		{
			for (j=7 ; j>0 ; j--)
			{
			result = op1[i] * sqrt(j);
			}
		}
		i++;
	}

I am getting this error: error C2668: 'sqrt' : ambiguous call to overloaded function

sqrt is overloaded for all of the floating-point types and your argument isn't an exact match for any of them. int could potentially be converted to all of them, so the call is ambiguous. You can get around that error by casting to double:

sqrt((double)j)

how would you get the letters of a text file, like

HEY 01011011

to get the words "HEY" and display it.
Then if you wanted to make the loop to be

if (command==HEY)
{
to perform a task?
}

A READ followed by an IF using strcmp()

Well I got the "HEY" to work. as far as storing the 8 binaries into memory, would you go about doing

infile.get (ch)?

Did it work when you tried it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.