Can't find the error, short program

May 25, 2017 at 2:37pm
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



#include <iostream>
#include <string>
#include "stdafx.h"

using namespace std;

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

May 25, 2017 at 3:21pm
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.

What compiler and compiler version are you using?

Please use code tags when posting code.

May 25, 2017 at 3:22pm
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?
May 25, 2017 at 3:45pm
closed account (E0p9LyTq)
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
   unsigned int 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";
}

This should compile with any modern C++ compiler, not just the Visual Studio IDE (Integrated Development Environment).

How to use code tags:
http://www.cplusplus.com/articles/jEywvCM9/
Last edited on May 25, 2017 at 3:46pm
May 25, 2017 at 7:36pm
@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
May 25, 2017 at 8:17pm
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

https://pasteboard.co/aEqopRid1.jpg
May 27, 2017 at 10:41pm
Thank you very much Thomas1965
Topic archived. No new replies allowed.