Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm using a Mac and I'm learning C++. I'm trying to open and read a file and I wrote a few lines code to do it. But when I compile it, either with gcc or g++ or clang, the compiler fails because it can't find the istream file. Is there a way to install or download the default library again. Thank you
Edit:
This is the code:
#include <ifstream>
using namespace std;
int main()
{
ifstream ligand("ligand_dataset.txt");
if( ligand.is_open())
{
cout << "File ok";
}
else cout << "Unable to open the file";
return 0;
}
The ifstream header is replaced with fstream
#include <fstream>
int main()
{
std::ifstream f(....);
return 0;
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I don't know how to explain it, but I will try.
So I write C++ code and I run it with code runner. But the program closes itself. I don't want to use system("PAUSE"); because I need to remove it if I'm in a competition.
So is there another way?
Ok, I tried something that came to my mind and it works.
"code-runner.executorMap": {
"cpp": "g++ $fullFileName -o $fileNameWithoutExt.exe && start cmd /k $fileNameWithoutExt.exe"
}
I used this in the settings.json
You can use Dev C++ as an alternative IDE. But if you wish to do it in Visual Studio without a system("PAUSE"); you can use cin.get(); or cin.ignore(); as an alternative.
Code:
#include<iostream>
using namespace std;
int main()
{
cout << "Hello World";
cin.ignore();
}
or
#include<iostream>
using namespace std;
int main()
{
cout << "Hello World";
cin.get();
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
The following code is not working in DEV C++:
#include <iostream>
using namespace std;
void main()
{
cout << "Hello world";
}
How can I do this?
Also, tell me if I can get almost all the functionality of Turbo C++ in Dev C++. Also, tell me if I can easily switch to DEV C++ if I know a moderate amount in Turbo C++. I just want to use C++ with Python 3.x for console applications including GUI.
From what I see in your initial question (printf works while ostream doesn't) the problem may be in buffering. Try that:
std::cout << "Hello world" << std::endl;
or, if you wish to avoid a newline:
std::cout << "Hello world";
std::cout.flush();
There is a difference between streams and printf: the streams are not outputing the data immediately but buffer that to optimize the performance. This however means that in some environments for the small outputs like yours it may stuck forever waiting for more output.
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello world";
return 0;
}
I used int main instead of void main.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I don't know what's wrong here ?
It's just running errors !!!
#include <iostream>
using namespace std;
int main()
{
cout << string("hello world");
return 0;
}
Read more about C++. So read first Programming -- Principles and Practice Using C++.
Then read C++ reference documentation, notably the one about std::string-s.
You need to #include <string>
You should enable all warnings when compiling. If using GCC, compile with g++ -Wall -g
You don't need that string before the actual string:
#include <iostream>
using namespace std;
int main()
{
cout << "hello world";
return 0;
}
Or, alternatively, if you're trying to store a string:
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str = "hello world";
cout << str;
return 0;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
So I am a beginner and for some reason this still give me the undeclared identifier error and the system cannot find specified file errro I would be grateful if someone would answer me this
#include <iostream>
#include "stdafx.h"
using namespace std;
int main()
{
cout << "hello world" << endl;
return 0;
}
When using visual studio, this line #include "stdafx.h" needs to be at the top of your code when using pre-compiled headers.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
cout << "hello world" << endl;
return 0;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I compiled both in code blocks and cygwin but it crashes when i run it.
The source.txt file is formated like this:
>sample1
ACTG
GCA
GTC
>sample2
TAACG
GGCC
And dtb should look something like this:
dtb=(sample1,ACTGGCAGTC,sample2,TAACGGGCC)
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
int main()
{
ifstream myfile;
int i=0;
string seq="",holder="";
myfile.open("source.txt");
vector<string> dtb;
while (myfile>> seq)
{
if (seq.substr(0,1)==">")
{
dtb[i]=seq.substr(1,seq.length()-1);
i++;
if (i!=0)
dtb[i]=holder;
holder="";
}
else
{
holder+=seq;
}
}
cout<<dtb[0]<<"\n"<<dtb[1]<<"\n"<<dtb[2]<<"\n"<<dtb[3];
return 0;
}
A std::vector object starts out empty. That means any indexing in it will be out of bounds and lead to undefined behavior.
You need to add elements to the vector, using e.g. push_back.