-1

I'm working on an assignment where I have to split up a program into modules of .cpp and .h files, and I'm getting a weird error. In one of my .cpp files, I have the code

#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>


bool getYesNoResponse()
{
    string response;
    getline (cin, response);
    while (response.size() == 0 ||
    (response[0] != 'y' && response[0] != 'Y'
     && response[0] != 'n' && response[0] != 'N'))
    {
        if (response.size() > 0)
        cout << "Please respond 'yes' or 'no'. " << flush;
        getline (cin, response);
    }
    return response[0] == 'y' || response[0] == 'Y';
}

I get the error error: 'string' was not declared in this scope. I'm not supposed to edit the actual code given to me (we're only supposed to write the includes and defining the functions in the .h files), but I wanted to see if the string problem was a one time thing, so I replaced string with std::string in the "string response;" line, and the problem ceased; except I then got error: 'cin' was not declared in this scope with the next line. I have my #include for <string>, <iostream>, and <fstream>, so I'm rather confused as to why it's not working. Any advice on how to fix this without altering my source code would be appreciated!

Jarod42
  • 203,559
  • 14
  • 181
  • 302
JC2112
  • 77
  • 1
  • 1
  • 12
  • can u try `using namespace std` just after the includes – Sakthi Kumar Feb 13 '14 at 14:47
  • @SakthiKumar "without altering my source code" –  Feb 13 '14 at 14:48
  • You already found a correct way to solve the problem, but you reject that solution. In fact, you reject *any* solution in your question. What do you then ask for? –  Feb 13 '14 at 14:48
  • ***without altering my source code???*** – herohuyongtao Feb 13 '14 at 14:49
  • You can not fix this code without editing it. Except... well... modifying the runtime headers. – typ1232 Feb 13 '14 at 14:49
  • 2
    it is `std::cin` and `std::string` and `std::getline` you get the picture. – nurettin Feb 13 '14 at 14:49
  • "Any advice on how to fix this without altering my source code would be appreciated!" - As a general rule, if the compiler complains about something, you will have to alter your source code. The only exception is if you made a mistake on the compiler command line. – Sebastian Redl Feb 13 '14 at 14:56
  • @herohuyongtao: a [solution](http://coliru.stacked-crooked.com/a/18a4f255254dc2e6) without modifying the source code : Use compiler flags ^_^. Sure it is not what OP wants. – Jarod42 Feb 13 '14 at 15:27

3 Answers3

3

You need to add

using namespace std;

as cin and string are defined under standard name space std, which is not the same scope with the main() body.

The keyword using technically means, use this whenever you can. This refers, in this case, to the std namespace. So whenever the computer comes across string, cout, cin, endl or anything of that matter, it will read it as std::string, std::cout, std::cin or std::endl.

When you don't use the std namespace, the computer will try to call string or cin as if it weren't defined in a namespace (as most functions in your codes). Since it doesn't exist there, the computer tries to call something that doesn't exist! Hence, an error occurs.

You can refer to here for more info and examples.


Note: by doing this, you should also know its disadvantages. Check out Why is “using namespace std;” considered bad practice? for more info.

Better way is that you can put std:: front like std::cin, std::string etc to explicitly give their namespaces.

Community
  • 1
  • 1
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
  • This worked! I tried looking this up somewhere else and they said there that the problem was that they had both "using namespace std;" and the header includes and that had supposedly caused a problem. Thanks! – JC2112 Feb 13 '14 at 14:51
  • 1
    `using namespace std;`: Generally bad advice, unless you spell out the pitfalls clearly. – juanchopanza Feb 13 '14 at 14:52
  • @nurettin Added more info and links for more examples. Thanks. – herohuyongtao Feb 13 '14 at 15:00
1
using namespace std; 

Just add the above line after including the header files in the start.

The error cin not declared in this scope or 'string'/'cin' was not declared in this scope comes up because C++ uses namespace to keep function names from conflicting with each other. The namespace provides a separate scope to the names declared inside of it, known as the namespace scope, a global scope. Thus any name declared inside the namespace scope will not be mistaken for similar names that are in other scopes.

vanika
  • 11
  • 4
0

All these names that were pointed out in error messages are declared in the standard name space std. So either you should prefix such names with std:: that is to use qualified names as for example

std::string response;

or you should say the compiler that these unqualified names will correspond to names from the standard namespace as for example

using std::string;

or you should place all standard names into global namespace as for example

using namespace std;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335