Cin and Cout not working - c++

#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.

Related

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();
}

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

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

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

Console UI with User input

So i'm trying to make a simple console application that asks the user for certain characteristics. The first question, asks the user for their age. For example, it should look like "I am >enter age< years old" I've been having a lot of console application problems, so I'll probably be moving to GUI interfaces in the future.. Until then, I think this is good practice. Heres my code. (Sorry for not using the code format, it doesnt seem to work properly on my mobile)
#include <iostream>
#include <string>
using namespace std;
//Variables
int Age;
//Functions
int AgeEnt(){
cin >> Age;
return Age;
}
//Main
int main (){
cout << "Welcome! Please enter your age to continue\n";
cout << "I am " << AgeEnt << " years old";
return 0;
}
This automatically puts a 1 where the age should be. How would I make it to where I can input a number between the text? Im still a beginner so excuse me if this isnt possible in console, or extremely depreciated.
int main (){
cout << "Welcome! Please enter your age to continue\n";
int age;
cin >> age;
return 0;
}
Also, why do you declare functions and variables above your main? Except for global functions, this is not needed in C++
#include <iostream>
using namespace std;
//Functions
int AgeEnt(){
int Age;
cin >> Age;
return Age;
}
//Main
int main (){
cout << "Welcome! Please enter your age to continue\n";
AgeEnt();
cout << "I am " << AgeEnt() << " years old";
return 0;
}
A few things
You don't need the <string> header file for this. Its not being used anywhere.
You don't need to declare Age as a global variable. Its only being used in one function so declare it in that.
Also, although i haven't done it here, i would usually do something like int a = AgeEnt(); cout<<a;

Cout of a string is giving an error and a hard time some insight help pls?

I cant find the error in this piece of code could anyone please insight me? I ran the debugging but the errors are un-understandable..
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
string name;
cout << "Input your name please?" << endl;
cin >> name;
if
{
(name == "Bart Simpson")
cout << "You have been very naughty" << endl;
}
return 0;
}
Problems:
You have some missing #includes, which probably caused your initial compiler errors.
You have a simple syntax error with your if statement.
Using the stream extraction operator will never yield a string with whitespace inside of it.
The following should work as you expect:
#include "stdafx.h"
#include <iostream>
#include <ostream>
#include <string>
using namespace std;
int main()
{
cout << "Input your name please?" << endl;
string name;
getline(cin, name);
if (name == "Bart Simpson")
{
cout << "You have been very naughty" << endl;
}
return 0;
}
(You need to include string for std::string and std::getline, and ostream for std::endl.)
I assume the bracket in the wrong place is just a problem when pasting the code
if(name == "Bart Simpson")
name will never equal "Bart Simpson", since extracting a string stops when it encounters whitespace; so it would only be "Bart". Perhaps you want to use getline() instead?
Should be
if (name == "Bart Simpson")
{
cout << "You have been very naughty" << endl;
}
And you need to include <string>