Author Topic: Runtime Error when compiling basic I/O code  (Read 3227 times)

HadMatter

  • Guest
Runtime Error when compiling basic I/O code
« on: April 08, 2012, 09:46:22 pm »
Hey guys, So I'm working on a project to create an assembler. I am kind of reintroducing myself to C++, and finally after getting my code to compile, I am getting a runtime error. I am using Code::Blocks with the GNU GCC compiler on a windows 7 machine

The Error: "This application has requested the Runtime to terminate it in an unusual way."

From what I can gather, this is due to the terminate() function being called possibly due to an unhandled exception

My Code:
Code
#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;

int main()
{
    string Oper;
    string Src;
    string Dst;
    int OpCode;
    char ThisLetter;
    char a = ' ';

    cout<<"here";

    ifstream file;
    file.open("Assy.txt");
    if (!file.good())
    {
        cout << "There was an error opening the file." << endl;
        return 0;
    }
    int i = 0;

    while(ThisLetter != a)
    {
        ThisLetter = file.get();
        cout<< ThisLetter;
        Oper.at(i) = ThisLetter;
        i++;
    }
    cout << Oper;

    file.close();

        return 1;
}

If I comment out the line:

Code
Oper.at(i) = ThisLetter;

It works, and the loop exits. The cout statements are purely for debug purposes. Any tips on where to look for a solution to this problem? I hope this is enough information. If you guys need anything else, let me know. Thanks!

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Runtime Error when compiling basic I/O code
« Reply #1 on: April 08, 2012, 10:11:17 pm »
Yes, use the debugger and read a book about the api you're using!

p.s. also read the rules, because you're violating them!
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline jarod42

  • Multiple posting newcomer
  • *
  • Posts: 87
Re: Runtime Error when compiling basic I/O code
« Reply #2 on: April 10, 2012, 10:06:35 am »
Oper += ThisLetter;

string::at is an accessor and can't append new char.