I have looked at a few other questions regarding getline() not functioning, however most problems regarding the topic were due to the programmer not including the string header. I have the string header included however getline is still giving me error E0304 (which I have already looked into).
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
char input[100];
getline(cin, input);
cout << input << endl;
}
There are two forms of getline:
std::cin.getline(array, size); // reads into raw character array
getline(std::cin, string); // reads into std::string
You should use a std::string instead of a raw character array:
#include <iostream>
#include <string>
#include <sstream>
int main()
{
std::string input;
getline(std::cin, input);
std::cout << input << "\n";
}
The non-member getline only works with std::string. Use the std::istream member function getline for C-style strings:
std::cin.getline(input, sizeof(input));
Related
I'm a complete noob at C++, and the first problem I am encountering is the following:
no operator >> matches these operands
#include "pch.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "hello world!";
cin >> "hello world!";
}
std::cin needs to write to a variable, but you are passing it a const char[13] string literal instead.
You need to pass it something like a std::string instead:
std::string str;
std::cin >> str;
P.S. This is a good time to a) read compiler messages, b) avoid using namespace std; globally, c) get a good C++ book.
#include <vector>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <string>
#include <fstream>
#include <stdlib.h>
using namespace std;
•
• //main func declaration etc...
•
//Vectors for storing information from file
vector<string> include;
vector<string> exclude;
string temp; //for storing whatever the stream is on
int len = atoi(puzzle_file >> temp); //first pos
int width = atoi(puzzle_file >> temp); //second pos
The above code is supposed to read in a file and store the numbers in the corresponding ints. I'm getting an error that says "no matching function for call to 'atoi'", even though I have #include <\cstdlib> and #include <\stdlib.h> in my file header. Unsure of where to go from here. Did some research on stackoverflow and other forums, couldn't find anything that really helped me out. Any advice? Thanks
You should use stoi instead of atoi.
stoi takes a std::string as parameter while atoi takes const char* as parameter.
And don't forget stoi is new since c++11.
puzzle_file >> temp expression returns istream, but there is no atoi overload that would accept such a parameter.
You should call atoi(temp.c_str());
You tried to skip one instruction and lost. puzzle_file >> temp returns puzzle_file and not temp. So you apply atoi to an input stream which is converted to a bool. Use:
int len, width;
puzzle_file >> len >> width;
if (! puzzle_file)...
I'm experimenting with C++ file I/O, specifically fstream. I wrote the following bit of code and as of right now it is telling me that there is no getline member function. I have been told (and insisted still) that there is a member function getline. Anybody know how to use the getline member function for fstream? Or perhaps another way of getting one line at a time from a file? I'm taking in two file arguments on the command line with unique file extensions.
./fileIO foo.code foo.encode
#include <fstream>
#include <iostream>
#include <queue>
#include <iomanip>
#include <map>
#include <string>
#include <cassert>
using namespace std;
int main( int argc, char *argv[] )
{
// convert the C-style command line parameter to a C++-style string,
// so that we can do concatenation on it
assert( argc == 2 );
const string foo = argv[1];
string line;string codeFileName = foo + ".code";
ifstream codeFile( codeFileName.c_str(), ios::in );
if( codeFile.is_open())
{
getline(codeFileName, line);
cout << line << endl;
}
else cout << "Unable to open file" << endl;
return 0;
}
getline(codeFileName, line);
Should be
getline(codeFile, line);
You're passing in the file name, not the stream.
By the way, the getline you're using is a free function, not a member function. In fact, one should avoid the member function getline. It's much harder to use, and harkens back to a day when there was no string in the standard library.
Typo
getline(codeFileName, line);
should be
getline(codeFile, line);
I guess the lesson is you have to learn how to interpret compiler error messages. We all make certain kinds of mistakes and learn the compiler errors they tend to generate.
I'm trying to abstract out a method from a simple program. This method tests the length of an array against a predeclared CAPACITY constant, and spits out an error message if conditions aren't met. However, I'm having trouble creating a header file with a .cpp file to hold the method.
the header file:
//arrayHelper.h
#ifndef ARRAYHELPER_H
#define ARRAYHELPER_H
void arrayLengthCheck(int & length, const int capacity, string prompt);
#endif // ARRAYHELPER_H
the code file:
//arrayHelper.cpp
#include <iostream>
#include <string>
#include "arrayHelper.h"
using namespace std;
void arrayLengthCheck(int & length, const int capacity, string prompt)
{
// If given length for array is larger than specified capacity...
while (length > capacity)
{
// ...clear the input buffer of errors...
cin.clear();
// ...ignore all inputs in the buffer up to the next newline char...
cin.ignore(INT_MAX, '\n');
// ...display helpful error message and accept a new set of inputs
cout << "List length must be less than " << capacity << ".\n" << prompt;
cin >> length;
}
}
Running this main.cpp file that contains #include "arrayHelper.h" errors out that string is not declared in the header file. Including string in the header file has no effect, but #include "arrayHelper.cpp" results in a redefinition of the method. How should I approach this problem?
You should #include <string> in the header, and refer to string as std::string, since using namespace std would be a bad idea in a header file. In fact it is a bad idea in the .cpp too.
//arrayHelper.h
#ifndef ARRAYHELPER_H
#define ARRAYHELPER_H
#include <string>
void arrayLengthCheck(int & length, const int capacity, std::string prompt);
#endif // ARRAYHELPER_H
string is used in header file but can't find the symbol.
Move #include <string> to arrayHelper.h and replace all string with std::string
Side note:
call using namespace std; locally is idiomatic way, and pass prompt by reference can elide one copy. Below is slightly enhancement to your code:
arrayHelper.h
#ifndef ARRAYHELPER_H
#define ARRAYHELPER_H
#include <string>
void arrayLengthCheck(int & length, const int capacity, const std::string& prompt);
#endif // ARRAYHELPER_H
arrayHelper.cpp
#include <iostream>
#include "arrayHelper.h"
void arrayLengthCheck(int & length, const int capacity, const std::string& prompt)
{
using namespace std;
// If given length for array is larger than specified capacity...
while (length > capacity)
{
// ...clear the input buffer of errors...
cin.clear();
// ...ignore all inputs in the buffer up to the next newline char...
cin.ignore(INT_MAX, '\n');
// ...display helpful error message and accept a new set of inputs
cout << "List length must be less than " << capacity << ".\n" << prompt;
cin >> length;
}
}
You need to say std::string in your header file...
I'm having trouble understanding where I went wrong with my code:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[]) {
string str = "";
cin >> str;
remove(str.begin(), str.end(), ' ');
cout << str;
cin.ignore();
}
The error says "'remove': function does not take 3 arguments (C2660)"
Try adding
#include <algorithm>
"algorithm" is an STL header containing a lot of functions, including std::remove, which the OP is trying to call. The error he got was because there is another function that takes a single argument, called "remove", which deletes a file.