C++ std::getline error - c++

I'm new to C++, could someone please explain to me why I received the below errors when I do use "std::getline"? Here's the code:
#include <iostream>
#include <string>
int main() {
string name; //receive an error here
std::cout << "Enter your entire name (first and last)." << endl;
std::getline(std::cin, name);
std::cout << "Your full name is " << name << endl;
return 0;
}
ERRORS:
te.cc: In function `int main()':
te.cc:7: error: `string' was not declared in this scope
te.cc:7: error: expected `;' before "name"
te.cc:11: error: `endl' was not declared in this scope
te.cc:12: error: `name' was not declared in this scope
However, the program would run and compile when I used "getline" with "using namespace std;" instead of std::getline.
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
cout << "Enter your entire name (first and last)." << endl;
getline(cin, name);
cout << "Your full name is " << name << endl;
return 0;
}
Thank you!

The errors are not from std::getline. The error is you need to use std::string unless you use the using namespace std. Also would need std::endl.

You need to use std:: on all the identifiers from that namespace. In this case, std::string and std::endl. You can get away without it on getline(), since Koenig lookup takes care of that for you.

#include <iostream>
#include <string>
int main()
{
std::string name; // note the std::
std::cout << "Enter your entire name (first and last)." << std::endl; // same here
std::getline(std::cin, name);
std::cout << "Your full name is " << name << std::endl; // and again
return 0;
}
You just needed to state the namespace for various elements that are in the std namespace (alternatively, you can remove all the std::s and place a using namespace std; line after your includes.)

Try this:
#include <iostream>
#include <string>
int main()
{
std::string name;
std::cout << "Enter your entire name (first and last)." <<
std::endl;
while(getline(std::cin, name))
{
std::cout <<"Your name is:"<< name << '\n';
}
return 0;
}

Related

Calling a string getter function from a header file

I'm learning C++, and I'm just messing around with putting classes in separate files for practice. I have a getter function, which returns a string (because the variable is saved as a string). However, from my main() function, I am not sure how to call it. I know the problem is probably that I need to include string somewhere when I call the object, but I have no idea how to format it.
I know this is a pretty newbie questions, but I couldn't find the answer anywhere. Could someone help me out?
(p.s. I'm not trying to get this specific code to work, since it's useless. I'm just trying to learn how to apply it for future reference).
I've tried throwing in string in a couple of places when calling or creating the object, but I always get an error. I know I could get around it by not encapsulating the variable or not having a separate class file, but that's not what I want.
main.cpp
#include <iostream>
#include "usernameclass.h"
#include <string>
using namespace std;
int main()
{
usernameclass usernameobject;
usernameobject.getUsername();
return 0;
}
usernameclass.h
#ifndef USERNAMECLASS_H
#define USERNAMECLASS_H
#include <string>
class usernameclass
{
public:
usernameclass();
std::string getUsername();
void setUsername(std::string name);
askUsername();
private:
std::string usernameVar = "test";
};
#endif
usernameclass.cpp
#include "usernameclass.h"
#include <iostream>
#include "username.h"
#include <string>
using namespace std;
string usernameclass::getUsername(){
return usernameVar;
cout << "test cout" << endl;
}
usernameclass::askUsername(){
string name;
cout << "What is your name?" << endl;
cin >> name;
setUsername(name);
cout << "Ah, so your name is "+usernameVar+", great name I guess!" << endl;
cin.get();
cin.get();
cout << "You're about to do some stuff, so get ready!" << endl;
}
usernameclass::usernameclass(){}
void usernameclass::setUsername(string name){
string* nameptr = &usernameVar;
*nameptr = name;
}
Expected result: runs getUsername() function and returns usernameVar
Actual result: doesn't run the getUsername() function
The current code would not compile, because you have not specified return type of 'askUsername()' routine, which is 'void', I believe.
Other things are good, apart from an output in 'getUsername()', which happens after returning from the function and about which you should have received a warning, I guess.
To the question: you can call that 'get' method in 'main()' as:
cout << usernameobject.getUsername();
Your code should be structured more like this instead:
main.cpp
#include <iostream>
#include "usernameclass.h"
int main()
{
usernameclass usernameobject;
// optional:
// usernameobject.askUsername();
// do something with usernameobject.getUsername() as needed...
return 0;
}
usernameclass.h
#ifndef USERNAMECLASS_H
#define USERNAMECLASS_H
#include <string>
class usernameclass
{
public:
std::string getUsername() const;
void setUsername(std::string name);
void askUsername();
private:
std::string usernameVar = "test";
};
#endif
usernameclass.cpp
#include <iostream>
#include "usernameclass.h"
std::string usernameclass::getUsername() const {
return usernameVar;
}
void usernameclass::setUsername(std::string name) {
usernameVar = name;
}
void usernameclass::askUsername() {
std::string name;
std::cout << "What is your name?" << std::endl;
std::getline(std::cin, std::name);
setUsername(name);
std::cout << "Ah, so your name is " << getUsername() << ", great name I guess!" << std::endl;
std::cout << "You're about to do some stuff, so get ready!" << std::endl;
}

how to concatenate a string and an int c++

#include <iostream>
using namespace std;
int main ()
{
int Age =25;
string name = "John";
cout << Age name ;
return 0;
}
compilation
In function 'int main()':
10:13: error: expected ';' before 'name'
For C++, you need all the other important parts, such as the include files, and the namespace qualifiers.
Unlike most scripting languages, the programming statements and expressions have to be in the context of a function.
For example:
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
int main()
{
int Age = 25;
string name = "John";
cout << Age << " " << name << endl;
return 0;
}
The error says that there's a missing semicolon somewhere, your compiler should tell you in which line is the error and you might find it.
Also you should do all the previous steps in the c++ code as following :
#include <iostream>
using namespace std;
int main()
{
int age = 25;
string name= "john";
cout<<"The age is: "<<age<<endl;
cout<<"The name is: "<<name<<endl;
return 0;
}
if there's still a problem please clarify it.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int Age ;
Age = 32;
string name ;
name = "ahmed";
cout << "age is"<< Age << " " <<endl;
cout<< "name is " << name << endl;
return 0;
try this
#include <iostream>
using namespace std;
int main(){
int Age =25; string name = "John";
cout<< name << " age ="<<Age;}
you mean that

error: reference to non-static member function must be called

I'm working on a program that collects family last names and then asks for names of first name of family members. I'm using an istringstream object to separate the names collected from a getline. For the step where I try to fill the elements in the map, I get the following error. I know that you can create keys by supplying them in brackets following the name of the map. If that method is valid, why do I get the following error.
ex11_14.cpp:19:6: error: reference to non-static member function must be called
family[lname].push_back[fname];
Code:
#include <iostream>
#include <map>
#include <vector>
#include <sstream>
int main()
{
std::string lname, fname;
std::string last, children;
std::map<std::string, std::vector<std::string>> family;
std::cout << "Enter family names" << std::endl;
getline(std::cin, last);
std::istringstream i{last};
std::cout << "Enter children's names" << std::endl;
while(i >> lname) {
std::cout << lname << std::endl;
getline(std::cin, children);
std::istringstream j{children};
while(j >> fname)
family[lname].push_back[fname];
}
for(auto &c:family) {
std::cout << c.first << std::endl;
for(auto &w:c.second)
std::cout << w << " ";
std::cout << std::endl;
}
std::cout << std::endl;
return 0;
}
Sorry. push_back should be called with () not []. Fixes the issue.
It seems, this line:
family[lname].push_back[fname];
wants to read
family[lname].push_back(fname);
BTW, don't use std::endl. Use '\n' to get a newline and std::flush to flush the stream: use of std::endl is most often a performance problem without being useful in the first place.

Cannot use ifstream in Xcode

I am new to C++ and have researched this everywhere and cannot seem to figure out how to compile this and have not idea why. It works in visual C++ but not Xcode. The error seems to be on the input stream. Any suggestions?
Error reads - "Implicit instantiation of undefined template 'std::_basic_ifstream >'
#include <iostream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "The file is providing the data.";
ifstream myFile("/Users/me/Desktop/somewords.txt"); // * error
int i;
string s;
double d;
myFile >> i >> s >> d;
cout << "here is your data " << endl;
cout << i << endl << s << endl << d << endl;
return 0;
}
You forgot to #include <fstream>, the header file that actually defines all your ifstream goodness. You included <iostream> twice (or at least tried to), perhaps one of those was meant to be <fstream>?

Why is writing a std::string to cout causing an unknown operator << error?

I am getting an error when I try to output the return value from one of my methods:
Error: No operator "<<" matches these operands. Operand types are: std::ostream << std::string
Main.cpp
#include <iostream>
using namespace std;
#include "Book.h"
int main()
{
book.setTitle("Advanced C++ Programming");
book.setAuthorName("Linda", "Smith");
book.setPublisher("Microsoft Press", "One Microsoft Way", "Redmond");
book.setPrice(49.99);
cout << book.getBookInfo(); // <-= this won't compile because of the error above.
int i;
cin >> i;
return 0;
};
Method which should return string:
string Book::getBookInfo()
{
stringstream ss;
ss << title << endl << convertDoubleToString(price) << endl;
return ss.str();
}
#include <string> is missing.
How did the code get the definition of string? The header <string> also declares the stream inserter.