C++ "Reference to overloaded function" error using namespace std - c++

I am new to C++, and I keep getting an error saying:
Reference to overloaded function could not be resolved; did you mean to call it?
Below is the code that is causing this:
#include <stdio.h>
#include <string>
using namespace std;
int main() {
string name;
string age;
cout << "Enter your name and age: ";
cin >> name >> age;
cout << "Hello, " << name << ", are you " << age << " years old?\n";
return 0;
}
I am using Xcode on Mac OS X Mojave.
I also noticed that if I have only that code, then it works just fine, but when I have multiple files, all of them fail to work.
Can anyone tell me why this is and a solution for it?

stdio.h does not define std::cin and std::cout. That header defines the C functions for input and output, like printf and scanf. So it is an I/O header, but it's not the one you need.
You need to include <iostream> to have std::cin and std::cout

Related

Why am I not getting the output from the program. Whenever I run my code it stop after taking the input? [duplicate]

This question already has answers here:
How do I pass a cin'd c style string to a function?
(2 answers)
Closed last year.
Here is my code I am expecting the output but I am not getting .It stop after taking the input
I am expecting the output if i give name Harsh
Your name is Harsh
#include <iostream>
#include <cstring>
using namespace std;
int main() {
cout << "Enter your name" << endl;
char *s;
cin >> s;
cout << "Your name is " << s;
return 0;
}
I have also tried with cin.getline(s,100);but still it is not working.
So I request to you to solve the problem and give me solution.
Your code has undefined behavior because you are not allocating any memory for s to point at. s is an uninitialized pointer.
Try this instead:
#include <iostream>
using namespace std;
int main(){
cout << "Enter your name" << endl;
char s[100];
cin >> s; // or: cin.getline(s,100);
cout << "Your name is " << s;
return 0;
}
Alternatively, you should use std::string instead, eg:
#include <iostream>
#include <string>
using namespace std;
int main(){
cout << "Enter your name" << endl;
string s;
cin >> s; // or: getline(cin,s);
cout << "Your name is " << s;
return 0;
}
s in your code is unallocated.
Since it is C++ we're talking about, you probably don't want to use pointers and memory allocation, and use std::string instead.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
cout << "Enter your name" << endl;
string s; // Instead of dealing with char* allocation and memory issues.
cin >> s;
cout << "Your name is " << s;
return 0;
}
you have done it correctly but the problem with output is because of the memory allocation.
You have to allocate memory and try to avoid the concept of a pointer in that. Instead
Use string s;
or
char s[50];

How to use the getline() function if I don't use "using namespace std;"?

I just started learning C++ recently, so please excuse me if I have some mistakes in my question.
In most of the tutorials I have watched, I have been taught to use the "using namespace std;" statement. But later I found that it is a bad practice, so now I don't use it. The problem I have is that if I use the getline() function without "using namespace std;", I get an error. However, if i add "using namespace std;", it works. I'll show you some examples.
Without "using namespace std;"
#include <iostream>
int main() {
string my_str;
std::cout << "Enter some text here -> ";
std::getline(cin, my_str);
std::cout << "You entered -> " << my_str;
}
If I run this code, I get this error:
error: 'my_str' was not declared in this scope
Now here is the code with "using namespace std;":
#include <iostream>
using namespace std;
int main() {
string my_str;
cout << "Enter some text here -> ";
getline(cin, my_str);
cout << "You entered -> " << my_str;
}
This code runs without any errors.
Am I supposed to add any syntax? Could anyone please help me?
Thanks.
EDIT: Thanks everyone for your help!
The issue is that a ot of things are in the std namespace.
Because you are not using using namespace std; everything that is in this namespace must be explicitly prefixed with std::.
In this situation my_str has a type string. But there is no type string there is a type std::string. You need to be explicit.
#include <iostream>
int main() {
std::string my_str;
// ^^^^^ Add this.
std::cout << "Enter some text here -> ";
std::getline(std::cin, my_str);
// ^^^^^ Add this.
std::cout << "You entered -> " << my_str << "\n";
// ^^^^^^^ probably add this
}

Taking in input with spaces using std::getline() in c++

I am pretty new to coding in c++, and I am using Visual Studio Code on MAC. I have tried to find the right code so that I can enter a string with spaces, but everything that I tried hasn't worked. I tried std::getline(std::cin, name), but when I ran the code, and it came time to input the name, it just showed nothing where the name should have been. I don't know if there is something wrong with Visual Studio Code, or if there is just a little bit of code that I am missing. Here is the code that I am having a problem with.
#include <iostream>
#include <string>
#include <cmath>
#include <sstream>
#include <string.h>
int main (){
std::string name;
std::cout << "Please, enter the name of your nation: ";
std::getline(std::cin, name);
std::cout << "General Williams: " << name << " is a great name!\n";
}
I executed your code and it worked.
Maybe you can not see your result because you run with .exe!
please Add "std::getchar()" to end of your code and try it again.
int main()
{
std::string name;
std::cout << "Please, enter the name of your nation: ";
std::getline(std::cin, name);
std::cout << "General Williams: " << name << " is a great name!\n";
std::getchar();
}

Choose among more .txt

I'm working on a project and I need to select a different .txt every time based on the input.
This is what I have:
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <fstream>
#include <string>
#include <stdio.h>
using namespace std;
int main()
{
string hp, att, def, vel, spec;
string answer, monster;
do
{
cout << "Which Monster?: ";
cin >> monster;
cout << endl;
ifstream selection;
selection.open(monster+".txt");
selection.close();
cout << endl << "Again? ";
cin >> answer;
}
while (answer == "y");
cout << "Hello world!" << endl;
return 0;
}
I have to get the monster string and search the .txt with the same name.
If I type "Troll" it will search for the Troll.txt
Is there a way?
This is the error I get:
F:\GdR\Campagna 1\CalcoloStats\main.cpp|22|error: no matching function for call to 'std::basic_ifstream::open(std::__cxx11::basic_string)'|
Given that monster is a std::string, this expression:
monster + ".txt"
is also a std::string.
Since C++11, you can use this as an argument to ifstream's open function just fine. However, until then, you are stuck with a limitation of ifstream which is that it can only take a C-style string.
Fortunately, you can get a C-style string from a std::string using the c_str() member function.
So, either:
selection.open((monster + "txt").c_str());
Or get a modern compiler / switch out of legacy mode.
Thanks Lightness Races in Orbit, solved with C++11 Compiler flag

Cin and Cout not working

#include <iostream>
#include "stdafx.h"
#include <string>
using namespace std;
int main()
{
//Variables in which student info is stored
string name = "";
//asks user for their name
cout << "/nHello, my name is Hal, what is your name? ";
cin >> name;
cout << "/nWelcome to C++ ";
cout << name;
cout << ", let the adventure begin!";
return 0;
}
Really basic code that I can't seem to get to work. Everywhere I have cin or cout the compiler says they're an undeclared identifier. I've looked at all of the answers on here and other forums and it none of the solutions seem to fix it. Thanks in advance.
Hope you have the "stdafx.h" file that you are trying to include in the right path. The code works fine without the file being included.