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
i am facing this error while reading the text file and the file contain a "name" string.
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
char ch;
ifstream read;
read.open("sam.txt");
while (read.eof())
{
read.get(ch);
cout << ch << endl;
}
return 0;
}
The issue you mention in the comments happens because when the end of the file is reached, get fails and does not overwrite the previous value of ch. A quick way to fix that is:
while (!read.eof())
{
if(read.get(ch))
{
cout << ch << endl;
}
}
Now ch is printed only when read.get(ch) returns successfully.
Related
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 1 year ago.
Improve this question
Hi I need help with this code. It keeps on printing "ur input:32765" and the number keeps changing. I read a question on stack overflow and it said it wasn't initialized, whatever that means. Can someone help with whats wrong?
#include <iostream>
using namespace std;
int main() {
int x;
cout << "ur input:";
cin >> x;
cout << "" << x;
return 0;
}
Write
if (cin >> x){
cout << "" << x;
} else {
cout << "bad input";
}
otherwise a read of an unintialised x could arise if the cin fails in C++03 or earlier. That can happen if there are not data on the stream that can be read into an int type.
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
int main()
inFile.get(file);
while(inFile) {
inFile.get(file);
cout << file;
if(inFile.fail()) {
break;
}
if(inFile) {
++charNum;
}
if(inFile && c =='<') {
++comNum;
}
The values keep outputting 1, and its not actually counting the amount of < in the file. If I put inFile >> c, it makes my file a bunch of gibberish. What is the best way to count a certain amount of characters within a file, that is being opened by the user? Thank you.
You can rely on algorithms provided by the Standard Library.
#include <algorithm>
#include <fstream>
#include <iostream>
int main(int argc, char* argv[]) {
std::ifstream fp(argv[1]);
const auto count = std::count(std::istreambuf_iterator<char>{fp},
std::istreambuf_iterator<char>{}, '<');
std::cout << "count: " << count << "\n";
}
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 3 years ago.
Improve this question
My program is saying that the function length() is not in the standard library and is producing an error that does not let me run my code
I have tried to place two different libraries into the #include statements. I then tried to place the length function on different variables but the same error occurs.
int main()
{
string line;
ifstream out_file_DOI("declaration_of_independence.txt");
if (out_file_DOI.is_open())
{
int i = 0;
while (getline(cout, out_file_DOI, i))
{
cout << line << endl;
cout << line.length(); //error is on this line...
i++;
}
out_file_DOI.close();
}
}
else
{
cout << "Unable to open file...";
}
//creates the declaration file
out_file_DOI.open("declaration_of_independence.txt");
}
I expect the code to run and show the proper results (proper results being the program encrypting the file (Declaration of Independence).
if u wanna read a line by line from the file to “line” string,
getline(out_file_DOI, line);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 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
My output file is supposed to show to answer to the function it calls on. The program runs fine, however it is not displaying the text in the "prime" function. the output file, when checked, only displays 1's. I believe this is due to the fact that its declared as a bool function, and set to return true. However, how would I get this code to return the solution in Prime to the output file?
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
bool prime(int);
int main()
{
int reader;
ifstream Infile;
Infile.open("numlist.txt");
ofstream outputFile;
outputFile.open("theoutput.txt");
while (Infile >> reader)
{
outputFile << prime(reader) <<endl;
}
Infile.close();
outputFile.close();
}
bool prime(int p)
{
if (p % 2 == 0)
cout << "\n" << p << "\n Is not a prime number";
else if (p % 2 != 0)
cout << "\n" << p << "\n is a prime number";
return true;
}
No errors, however the output file is only showing 1's.
This is happening because in your prime() function, all the output is going to cout and not into outputFile. The prime() function returns a bool which is what is sent to outputFile.
If you'd like to have output of the function go to outputFile, you can either pass outputFile as a parameter and use that instead of cout or make it global.
A few more comments on your code: you don't need the full else if (p % 2 != 0) in the else statement. You can just use else, because p % 2 is either 0 or it's not, there's no other option.
Also, strongly recommend using braces around if statements, even if they are just a single line.
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 9 years ago.
Improve this question
i started learning c++ last week and ive finally learned enough to try and stand on my own feets. well guess what i have a problem. the program im trying to make will ask for a file already existing or creates a new one if the name isnt found, and places information in the file lines. when you type -1 you close the program.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string x;
string Input;
int line=0;
cout << "Please enter the name of the file with the file type" << endl;
cin >> x;
ofstream SelectedFile;
SelectedFile.open(x);
while(Input != "-1"){
cout << "Enter the content of the " << line <<" line, or type -1 to quit." << endl;
cin >> Input;
line++;
}
SelectedFile.close();
}
I guess you're having a compile error because std::ofstream::open does not take an std::string as an argument in C++98 standard. Try this one:
SelectedFile.open(x.c_str());
Or compile with C++11 support.
Update: Where you write things into file? I think you forgot to write that part
SelectedFile << line << std::endl ;