C++ reading command from .txt file

717 views Asked by At

I want to create a little "calculator" but I dont know how can I create.

My problem is, I have an input file (.txt) with codes:

acc +40

acc -14

nop +386

jmp +262

acc -4

nop +25

... the "acc" adds the number to my variable

the "jmp" is jump to the line (jmp +500 jump foward 500 line)

the "nop" dont do anything

and here is my code but not working (the acc is okay, but the jmp is not)

ifstream file("my.txt");
    string cmd;
    int num;

    int var= 0;
    int i = 0;

    if(file.is_open())
    {
        while (file >> cmd >> num)
        {

            cout << "Var" << var<< endl;

            cout << "Command: " << cmd << "   Number: " << num<< " ----" << i <<" //  Var: " << var<< endl;
            ++i;
            if(cmd == "acc")
            {
                var= var+ num;
            }

            if(cmd == "jmp")
            {
                ;
            }

        }

        file.close();

    }else {
        cout << "error"<<  endl;
        cin.get();
}
1

There are 1 answers

0
Flower On

This is a sample code. I hope everything here is in order. I did what Some programmer dude told you. Using vectors, you read all the lines into them and then just iterate.

#include <fstream>
#include <iostream>

#include <string> //addition
#include <vector> //addition

using namespace std;

int main() {
    ifstream file("my.txt");
        string cmd;
        int num;

        int var= 0;
        int i = 0;
        
      string my_string;//addition
      vector<int> numbers;//addition
      vector<string> commands;//addition

        if(file.is_open())
        {
        /*this while function reads every line of the file and writes command to the vector of strings named "commands" and the number to the vector of integers named "numbers".*/
            while (getline(file, my_string))//while you can read line from "file", read it and put in into string called "my_string".
            {
            cmd = my_string;
            cmd.resize(3);//leaves only first three characters of the string.
            commands.push_back(cmd);//adds this "cmd" string to vector "commands"  
            
            my_string.erase(0,4);//erases characters from 0 to 4, inclusive, from the string "my_string". So erases first 4 characters, so our command and the space after.
            numbers.push_back(stoi(my_string));//adds my_string, converted to int, to vector "numbers". Stoi() converts string to int.
            ++i;
            }
          
          for(i = 0; i < commands.size(); i++)
          {                          
            cout << "Var " << var << endl;
              if(commands[i] == "acc")
              {
                //remember about "+=", it's quicker this way :)
                var += numbers[i];
              }
            cout << "Command: " << commands[i] << "   Number: " << numbers[i] << " ----" << i <<" //  Var: " << var << endl;
            if(commands[i] == "jmp")
              {
                i+= numbers[i];
              }
               
          }
            file.close();

        }else {
            cout << "error"<<  endl;
            cin.get();
    }
}

Sorry in advance for any formatting issues. My first answer on stackoverflow...