Confused and upset.

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";

while (loopControlVariable <= sentinelValue){
cout << loopControlVariable << " ";
counter = counter + 1;
accumulator = accumulator + loopControlVariable;
loopControlVariable++;
}

cout << "\nThe while loop printed "
<< counter << " numbers "
<< "that sum to " << accumulator << endl << endl;

//demonstrate for loop
//reset the variables
cout << endl;
counter = 0;
accumulator = 0;
cout << "For loop...\n";

for (int i = startNumber; i <= lastNumber ; i++ ){
cout << i << " ";
counter = counter + 1;
accumulator = accumulator + i;
}

cout << "\nThe For loop printed "
<< counter << " numbers "
<< "that sum to " << accumulator << endl << endl;

//demostrate do until loop
//reset variables
counter = 0;
accumulator = 0;
sentinelValue = lastNumber;
loopControlVariable = startNumber; //prime the loop
cout << "do While...\n";

do {
cout << loopControlVariable << " ";
counter = counter + 1;
accumulator = accumulator + loopControlVariable;
loopControlVariable++;
}while (loopControlVariable <= sentinelValue);

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
Compiled perfectly in mine. You could take away the namespace and for every - cout, cin and endl. Change them to std::cout, std::cin and std::endl
Or more simply try this - it's a little neater for you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream> 

int main()
{
	int startNumber = 0;
	int lastNumber = 0;

	std::cout << "Print a Range of Numbers\n" << "Enter the starting number: ";

	std::cin >> startNumber;
	std::cout << std::endl;
	std::cout << "Enter the last number: ";
	std::cin >> lastNumber;
	std::cout << std::endl;

	//demonstrate while loop
	int counter = 0;
	int accumulator = 0;
	int sentinelValue = lastNumber;
	int loopControlVariable = startNumber; //prime the loop
	std::cout << "While loop...\n";

	while(loopControlVariable <= sentinelValue)
	{
		std::cout << loopControlVariable << " ";
		counter = counter + 1;
		accumulator = accumulator + loopControlVariable;
		loopControlVariable++;
	}

	std::cout << "\nThe while loop printed " 
	<< counter << " numbers "
	<< "that sum to " << accumulator << std::endl << std::endl;

	//demonstrate for loop
	//reset the variables
	std::cout << std::endl;
	counter = 0;
	accumulator = 0;
	std::cout << "For loop...\n";

	for (int i = startNumber; i <= lastNumber ; i++ )
	{
		std::cout << i << " ";
		counter = counter + 1;
		accumulator = accumulator + i;
	}

	std::cout << "\nThe For loop printed " 
	<< counter << " numbers "
	<< "that sum to " << accumulator << std::endl << std::endl;

	//demostrate do until loop
	//reset variables
	counter = 0;
	accumulator = 0;
	sentinelValue = lastNumber;
	loopControlVariable = startNumber; //prime the loop
	std::cout << "do While...\n";

	do{
		std::cout << loopControlVariable << " ";
		counter = counter + 1;
		accumulator = accumulator + loopControlVariable;
		loopControlVariable++;
	}while (loopControlVariable <= sentinelValue);

	std::cout << "\n The do while loop printed " 
	<< counter << " numbers "
	<< "that sum to " << accumulator << std::endl << std::endl;

	return 0;
}
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.
Last edited on
@Mythios
Wy are your recommending people to use std:: instead of using namespace std;? What's the differce between those to?
No problem
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 using namespace 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.
Topic archived. No new replies allowed.