Ok I copied and pasted a code from my school book straight into the citrix lab's visual studio. Here is the code;
/* week 3's walkthrough: Demonstrates the three primary loop types*/
#include <iostream>
using namespace std;
int main(void) {
int startNumber = 0;
int lastNumber = 0;
cout << "Print a Range of Numbers\n"
<< "Enter the starting number: ";
cin >> startNumber;
cout << endl;
cout << "Enter the last number: ";
cin >> lastNumber;
cout << endl;
//demonstrate while loop
int counter = 0;
int accumulator = 0;
int sentinelValue = lastNumber;
int loopControlVariable = startNumber; //prime the loop
cout << "While loop...\n";
cout << "\n The do while loop printed "
<< counter << " numbers "
<< "that sum to " << accumulator << endl << endl;
return 0;
}
this code is for loops. Now I ran the code in visual and it tells me I need # include"stdafx.h" once I add this then it gives me these errors can anyone tell me why?
1>f:\loops\loops\loops.cpp(5) : error C2871: 'std' : a namespace with this name does not exist
1>f:\loops\loops\loops.cpp(11) : error C2065: 'cout' : undeclared identifier
1>f:\loops\loops\loops.cpp(14) : error C2065: 'cin' : undeclared identifier
1>f:\loops\loops\loops.cpp(15) : error C2065: 'endl' : undeclared identifier
Thanks folks for the help, I found out what I was doing wrong. I was putting #include "stdafx.h" after the #include <iostream> and before using namepsace std; so it was not finding the cout, cin, and endl correctly.
Mythios, you can use namespace std; the std:: However the reason my code was not working and recognizing was because I was putting
#include "stdafx.h" in between iosream and the using namespace std; Which was causing my code to not recognize the cout, cin, and endl.
Main reason i say not to use "using namespace std" is as i come from a background of game programming. In all my classes and games i've worked on It's always been pointed out to our teams its a bad practice.
@Mythios
I dont know or you're right or wrong, i'm only asking wy you prefer std::. What is the difference between std:: and using namespace std;? I understand its told you to use std::, did they also explain wy?
if you use usingnamespace std; you are including the entire std namespace in the global scope, sometimes you could have problems with that (eg having the same name for different things)
but I think the best way to solve this isn't to always add std:: before all standard stuff, just say using std::cout;//etc for what you are using most frequently
The original purpose of namespaces was to solve the problem where two independent library writers would write libraries that exported identical symbols. A third party software package that tried to link to both libraries would get duplicate symbol errors or worse.
The solution was to add namespaces - then library A and library B could define the same symbols as long as they were in different namespaces and a third party software package could link to both without problem.
By saying "using namespace std;" in your .cpp, you are essentially saying "thanks but no thanks" to the protection that the namespace feature gives you. In many cases it isn't a problem to use an entire namespace in a .cpp file, though some people will recommend against it anyway. The tradeoff is more typing -- having to prefix everything with std:: (or whatever other namespaces you have). The alternative is to use just the pieces you need as Bazzy points out. This is less typing, with the main drawback that it can lead to a lot of "using" declarations at the top of all of your .cpp files which is still a bit cumbersome to type and can be somewhat of a maintenance problem if symbols are moved from one namespace to another.
By saying "using namespace std;" in a header file, you are gratuitously turning off the namespace protection for any source file that directly or indirectly includes that header file. NEVER use namespaces in header files.