tutorial error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
int main()
{
using namespace std;
char response[256];
cout << "What is your name? ";
cin.getline(response,256);
if (strlen(response) == 0)
cout << "You must tell me your name...";
else
cout << "It's nice to meet you, " << response;
return 0;
}


A book says to type this in as a tutorial, but when you run it , it gives an error code strlen not declared in this scope
They proceed to go on explaining as if it worked, but it didn't for me?
#include<cstring>
However it may be better to learn to use std::string
Don't use that book. You're missing an include (<cstring>), however you should use none of the functions from that header in C++.

In correct C++, the program looks like this:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
int main()
{
  using namespace std;
  string response;
  cout << "What is your name? ";
  getline(cin,response);
  if (response.empty())cout << "You must tell me your name...";
  else cout << "It's nice to meet you, " << response;
}
What book are you using? This doesn't seem like a good tutorial...
Also what are you typing it into? My IDE (Code::Blocks) runs it fine.
Yeah I would get a different book. Try this one: http://www.amazon.com/Beginning-Through-Game-Programming-Second/dp/1598633600 . It's what I used.
Athar wasn't as bored as I am.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <cstdlib>
#include <iostream>
#include <string>

int main(int argc, char **argv)
{
    std::string response;

    std::cout << "What is your name? ";
    std::getline(std::cin, response);

    if (response.empty())
    {
        std::cout << "You must tell me your name...";
        return EXIT_FAILURE;
    }
    else
        std::cout << "It's nice to meet you, " << response;

    return EXIT_SUCCESS;
}

Game programming books aren't exactly known for their quality (to put it mildly).
Viable choices are The C++ Primer, Accelerated C++ and Thinking in C++.
I have a few books
I abandoned C++ for Dummies, it was not explained well at all. It gave examples and explained only some of what was going on but not all and the writer explanation was confusing

The book I was using for this forum tutorial was Learn to Program with C++ (1993), I liked it cuz it explained everything as if you had no knowledge of C++ or math. It explained line for line of code and what this does and that does before proceeding.

Topic archived. No new replies allowed.