thermoplay
thermoplay

Reputation: 75

Program runs on one computer but doesn't work on another

I have tried running this code in my school and it works like a charm but when I got home, this piece of code suddenly gets error. Can somebody enlighten me about what happened to my code? Is there something that I should correct or add in my code? edited : There were build errors in the code.

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

ofstream fileObject;

using namespace std;

int main() {
    string username[5];
    cout << "Enter username: ";
    for (int i = 0; i < 5; i++) {
        getline(cin, username[i]);
    }
    fileObject.open("open.txt", ios::app);
    for (int x = 0; x < 5; x++) {
        fileObject << username[x] << endl;
    }
    fileObject.close();
    return 0;
}

Upvotes: 0

Views: 732

Answers (1)

anastaciu
anastaciu

Reputation: 23792

ofstream fileObject; is above using namespace std, so it's not recongnized as a type.

Either move it below using namespace std, or use std scope, std::ofstream fileObject.

The second option is a better one, read Why is “using namespace std;” considered bad practice?

I don't see how this can run in any C++ compiler, unless you include headers that already recognize std namespace.

Upvotes: 3

Related Questions