error: ‘cout’ was not declared in this scope

I wrote the following program:
1
2
3
4
5
6
7
#include <fstream>
using namespace std;
int main( int argc, char**argv) // returns 1 on error
{
	for (int ctr = argc; ctr; ctr--)
	cout << argv[ctr] << " ";
}


But get this error
ch17_reverse.cpp: In function ‘int main(int, char**)’:
ch17_reverse.cpp:6: error: ‘cout’ was not declared in this scope

I don't understand why :(
You might want to try to add #include <iostream> on the top of your code, to see the family tree of input and output go here: http://www.cplusplus.com/reference/iostream/
Thanks, that fixed it, but I have another question. I'm trying to write a program that displays its command-line arguments in reverse and does not display the program name. I have wrote this:
1
2
3
4
5
6
7
8
#include <iostream>
#include <fstream>
using namespace std;
int main( int argc, char**argv) // returns 1 on error
{
	for (int ctr = argc; ctr; ctr--)
	cout << argv[ctr] << " ";
}

and included <iostream> just as you suggested.
I get no output and after I run ./test it just goes to the next line.
I'm using Ubuntu Linux and do:
g++ <filename> -o test
followed by: ./test
to run the program, but unfortinately I get no output.
I tried to do: ./test <filename> abc xyz
but that does nothing. I have no idea what command-line arguments I'm supposed to type and what the syntax of them is. This is just a program that I got from the book and I'm learning C++, can you please help me with this
It looks fine to me.
You could use

for (int ctr = argc-1; ctr; ctr--)

since the arguments are zero-indexed.
Last edited on
Oops, yeah that might be why.
Thanks, that made it work ;)
Topic archived. No new replies allowed.