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";
}
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 12 months ago.
Improve this question
#include <bits/stdc++.h>
using namespace std;
int main()
{
char *str;
gets(str);
int size = strlen(*(&str));
//How to iterate through this str which is acting like a string here
return 0;
}
//I'm trying to print each char in a new line.
Ignoring all the other problems, such as using an uninitialized pointer, using gets (it's so bad it's been removed from C and C++), including bits/stdc++.h, not using std::string and std::getline...
Using your size variable, you can use loop like this:
for(int index = 0 ; index < size ; ++index) {
std::cout << "character at index " << index << " is '" << str[index] << "'\n";
}
But note that your code will crash at gets and never get to this loop. Please find better learning material to get started with C++!
PS. To get your code to not crash, change char *str; to char str[10000];... Then that program should run and you are unlikely to accidentally cause a buffer overflow. Still, I repeat, get better learning material!
The character pointer str doesn't point to any char object and has not been initialized.
Second, the function gets has been deprecated in C++11 and removed in C++14.
A better way would be to use std::string instead as shown below:
#include <string>
#include <iostream>
int main()
{
std::string str;
//take input from user
std::getline(std::cin, str);
//print out the size of the input string
std::cout << str.size() << std::endl;
//iterate through the input string
for(char& element: str)
{
std::cout<<element<<std::endl;
}
}
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.
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 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 8 years ago.
Improve this question
I've got something like this. The problem is strings must be letters only, how can I do this? I've been sitting over this for few hours now and can't find any working solution. I've tried to use answers from this topic Accept only letters but I guess I'm too dumb and still can't make it work :(
#include <string>
#include <vector>
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;
int main(void)
{
vector <string> strings;
string line;
do
{
cout << "enter string, 'stop' stops: ";
cin >> line;
strings.push_back(line);
}
while (line != "stop");
vector <string> :: iterator w;
cout << "Before sorting \n";
for (w=strings.begin(); w!=strings.end(); w++)
cout << *w << endl;
sort (strings.begin(),strings.end());
cout << "After sorting \n";
for (w=strings.begin(); w!=strings.end(); w++)
cout << *w << endl;
}
You need to add validation code. For simple cases, you can do
something like:
if ( std::find_if( line.begin(),
line.end(),
[]( unsigned char ch ) { return !isalpha( ch ); }
) != line.end() ) {
// not all letters
}
(This is really only appropriate for school projects, and won't
work for the usual network encoding, UTF-8.)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm relatively new to programming and taking a course in C++ currently. I haven't had any major problems so far. I am making a program where an X amount judges can score 0.0 - 10.0 (double) and then the highest and lowest one is removed then an average is calculated and printed out.
This part is done, now I want to read from a file in the shape of:
example.txt -
10.0
9.5
6.4
3.4
7.5
But I am stumbling onto problems with the dot (.) and how to get around it to get the number into a double. Any suggestions and (good) explanations so I can understand it?
TL;DR: Reading from file (E.G. '9.7') to a double variable to put into an array.
Since your textfile is whitespace delimited, you can use that to your advantage by utilizing std::istream objects who skip whitespace by default (in this case, std::fstream):
#include <fstream>
#include <vector>
#include <cstdlib>
#include <iostream>
int main() {
std::ifstream ifile("example.txt", std::ios::in);
std::vector<double> scores;
//check to see that the file was opened correctly:
if (!ifile.is_open()) {
std::cerr << "There was a problem opening the input file!\n";
exit(1);//exit or do additional error checking
}
double num = 0.0;
//keep storing values from the text file so long as data exists:
while (ifile >> num) {
scores.push_back(num);
}
//verify that the scores were stored correctly:
for (int i = 0; i < scores.size(); ++i) {
std::cout << scores[i] << std::endl;
}
return 0;
}
Note:
It is highly recommended to use vectors in lieu of dynamic arrays where possible for a myriad number of reasons as discussed here:
When to use vectors and when to use arrays in C++?
Try this:
#include <iostream>
#include <fstream>
int main()
{
std::ifstream fin("num.txt");
double d;
fin >> d;
std::cout << d << std::endl;
}
Does that do what you want?