COUT NOT DECLARED IN THIS SCOPE

Aug 25, 2016 at 4:47pm
Hello Fellow Forum Members,
I am using Dev-C++. I have written this code, but during compilation it says cout is not declared in this scope, and also cin not declared. Please can anybody look into the code and rewrite the right one with mu mistake? Thanks!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  bool accept2()
{
	cout << "Do You want to proceed (y or n)?\n";
	// write question
	char answer = 0;
	cin >> answer;
	switch (answer){
		case 'y':
			   return true;
		case 'n':
		       return false;
			   default:cout<<"I'll take that for a no>/n";
			   return false;   
	}
Aug 25, 2016 at 4:52pm
closed account (E0p9LyTq)
Since you only posted a small extract of your full source it is hard to say what the problem might be.

Did you #include <iostream> ?
Aug 25, 2016 at 4:53pm
This is the entire source code and not a part of it. No I didn't use #include <iostream>
Aug 25, 2016 at 5:19pm
closed account (E0p9LyTq)
That is not a complete program, you have no include statements nor a main function.

Just an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

bool accept2()
{
   std::cout << "Do You want to proceed (y or n)? ";
   // write question
   char answer = '\0';
   std::cin >> answer;
   switch (answer)
   {
   case 'y':
      return true;
   case 'n':
      return false;
   default:
      std::cout << "I'll take that for a no...\n";
      return false;
   }
}

int main()
{
   accept2();
}


Do You want to proceed (y or n)? t
I'll take that for a no...
Aug 25, 2016 at 6:52pm
Pratham372002

Your bit of code worked for me. As FurryGuy said "Did you #include <iostream> ?"?

I also suggest for your switch:
1
2
3
4
5
6
7
8
	case 'y':
	case 'Y':
		return true;
	case 'n':
	case 'N':
		return false;
	default:cout << "\nI'll take that for a no>\n";
		return false;

Do not forget about capital letters. And in line 12 of your code for default the text that is output ends with /n and should be \n. The \n at the beginning of the line I added for testing.

Hope that helps,

Andy
Aug 26, 2016 at 7:36am
No I didn't use #include <iostream>

One cannot use undeclared identifiers in C++, because the compiler does not know what they are. That is why one has to declare (function, variable, type identifiers) first, before use.

It would be a pain to write every declaration yourself. That is why the library writers have written some for us and stored into files that the preprocessor presents for the compiler in our behalf -- but only if you do use the #include directive.
Topic archived. No new replies allowed.