Hi, I am new to C++ and I was trying to follow an example of a code in a youtube video but it doesn't seem to work when I run it. The person of the video seems to use an older version of C++ since he doesn't even use the #include "stdafx.h"
I looked online and I used std::string name = ""; for the string since I use stdafx.h and he doesn't but still it doesn't work. the program its supposed to ask for your name and age
int main()
{
std::string name = ""; //empty string
int age = 0; //variable age initialized to 0
cout << "Enter your name:\n";
cin >> name;
cout << "Enter your age:\n";
cin >> age;
cout << "your name is:" << name << ".\nYour age is:" << age << ".";
return 0;
}
When I run it, it shows errors such as:
'string': is not a member of std
'string': undefined identifier
syntax error:missing ';' before identifier 'name'
'name': undeclared identifier
'cout': undeclared identifier
The person of the video seems to use an older version of C++ since he doesn't even use the #include "stdafx.h"
This header doesn't have anything to do with new or older versions of C++, it is a Microsoft C++ specific header relating to pre-compiled headers. By the way it should be the first thing #included if you want to use this feature.
stdafx.h is only used in Visual Studio when using pre-compiled headers. Feel free to remove it.
The rest of your program looks fine and works on VS 2015. What compiler / IDE do you use?
As the others have said "stdafx.h" is a Visual C++ feature, best not used even if you are using VC++ to learn.
Using code tags (and a bit of editing to your source):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
#include <string>
int main()
{
std::string name = ""; // empty string
unsignedint age = 0; // variable age initialized to 0
std::cout << "Enter your name: ";
std::cin >> name;
std::cout << "Enter your age: ";
std::cin >> age;
std::cout << "\nYour name is: " << name << ".\nYour age is: " << age << ".\n";
}
@jlb I am not very sure about the compilar, I tried looking it up online but it didn't help..
stdafx.h is only used in Visual Studio when using pre-compiled headers. Feel free to remove it.
The rest of your program looks fine and works on VS 2015. What compiler / IDE do you use?
@Thomas1965 I am using the C++ that comes in Visual Studio 2017, I tried my code again deleting the #include "stdafx.h" and the std before string but that didn't work either, I also tried deleting only the #include "stdafx.h" but still didn't work... I don't know what compiler I am using ...
@FurryGuy I tried the code you mentioned but it didn't work, I am still getting errors, not sure why but thank you all for your help
When you remove stdafx.h you need to change the project settings. Under Properties->C/C++ ->Precompiled heades set Precompiled headers to Not Using Precompiled Headers and remove stdafx.h from Precompiled Header File