Program runs on one computer but doesn't work on another - c++

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;
}

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.

Related

I have a problem with reading from a file

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
fstream file, save_file;
vector<string> words;
for (int i = 0; i < 1000; i++)
file >> words[i];
cout << words[0];
return 0;
}
I want to save these words in a vector, but I can't. I have a message: zad1.exe is already runing! Please close it first to compile successfully! I don;t know why.
Here is my solution:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
int main()
{
std::fstream file("test.txt"), save_file;
std::vector<std::string> words;
std::string line;
for (int i = 0; i < 1000; i++){
file >> line;
words.push_back(line);
}
std::cout << words[0];
return 0;
}
Instead of test.txt write your file path
On some operating systems you cannot write to a file if that file is a running program. That is what the error message is telling you. You are creating a program called zad1.exe and it is currently running so you cannot create a new version of zad1.exe until you stop the version that is already running.
Are you working on Windows? If so then use the task manager to kill any versions of zad1.exe that you can see.
Plus you have many problems with the code as pointed out in the comments above. But the first task is to kill those running programs.

VS2017 c++ ifstream error

I've just installed Visual Studio to start writing C++ there, I spent the last year learning in CodeBlocks and everything was going fine. But when I got to trying out the ifstream function, it shoots out an error and I've looked around and can't seem to find an answer anywhere. Could you guys please help?
Here's the very simple code that I wrote:
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int n, m[100];
ifstream f1("in.txt");
f1 >> n;
for (int i; i < n; i++)
{
f1 >> m[i];
cout << m[i] << endl;
}
f1.close();
system("pause");
return 0;
}
This is the in.txt path
C:\Users\Administratar\source\repos\ConsApp1\in.txt
And this is what is inside in.txt
4
1
2
3
4
And the error says:
"Error: Unable to open file
C:\Users\Administratar\source\repos\ConsApp1\Debug\ConsApp1.obj
Error code = 0x80070002"
I had a similar issue just now, actually, but the problem wasn't in ifstream or the include at all. It was in the file included previous of it.
Just check stdafx.h file and make sure there's no issues or errors in it and it shall work perfectly fine.

No match for 'operator>>'

I can't figure out why it says this. I am new as you can probably tell... here is the code:
#include <iostream>
using namespace std
int main() {
if (cin >> "hi"
cout << "hello"
return 0;
}
"The thing you were using" (read: your compiler) wanted you to end your using namespace std statement with a semicolon, not to dump one at the start of a function definition.
Your code has a number of extreme and baffling syntax errors, to the extent that it's not even clear what you're trying to accomplish.
Below is a hint to get you started but, from now on, I strongly recommend that you read a good, peer-reviewed C++ book and learn the language before asking any further questions about nonsense code!
#include <iostream>
#include <string>
using namespace std;
int main()
{
string input;
getline(cin, input);
if (input == "hi") {
cout << "hello" << endl;
}
return 0;
}

Array of Ofstream in c++

I want 41 output files to use in my project to write text on them. first create a string array list to name those output files then I tried to define an array of ofstream objects and use list to name them, but I get this error that 'outfile' cannot be used as a function. Below is my code:
#include <sstream>
#include <string>
#include <iostream>
#include <fstream>
using namespace std ;
int main ()
{
string list [41];
int i=1;
ofstream *outFile = new ofstream [41];
for (i=1;i<=41 ;i++)
{
stringstream sstm;
sstm << "subnode" << i;
list[i] = sstm.str();
}
for (i=0;i<=41;i++)
outFile[i] (list[i].c_str());
i=1;
for (i=1;i<=41;i++)
cout << list[i] << endl;
return 0;
}
See below for the following fixes:
don't use new unless you have to (you were leaking all files and not properly destructing them will lead to lost data; ofstreams might not be flushed if you don't close them properly, and the pending output buffer will be lost)
Use proper array indexing (starting from 0!)
Call .open(...) on a default-constructed ofstream to open a file
Recommendations:
I'd recommend against using namespace std; (not changed below)
I recommend reusing the stringstream. This is is good practice
Prefer to use C++-style loop index variables (for (int i = ....). This prevents surprises from i having excess scope.
In fact, get with the times and use ranged for
#include <sstream>
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
ofstream outFile[41];
stringstream sstm;
for (int i=0;i<41 ;i++)
{
sstm.str("");
sstm << "subnode" << i;
outFile[i].open(sstm.str());
}
for (auto& o:outFile)
cout << std::boolalpha << o.good() << endl;
}
You can not call the constructor as you do. Try calling outFile[i].open(list[i].c_str()). Note the 'open'.

Cannot Output Data Into a Text File

I am experimenting with some code for outputting information to a file. The file address appears to be correct and the code compiles but the file never populates. Can you see a problem?
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
int size = 10;
for(int i=0; i<size; ++i)
{
ofstream outputfile;
outputfile.open("C:MyFolder\outputfile.txt", ios::app);
outputfile << "SYMBOL, STOCK_PRICE" << endl;
outputfile << i << endl;
outputfile.close();
}
}
When fixing the path to be an actual Windows path, it runs ok for me;
outputfile.open("C:\\MyFolder\\outputfile.txt", ios::app);
maybe the path is not right, you did not escape the backslashes. otherwise the code is fine, and worked for me.
Others have given answer to your problem. I also suggest you to open and close file only once (outside loop), and do only file-writing within loop.