PC too dumb to read

Oct 22, 2014 at 6:22pm
So i get almost all my declared variables undeclared... any suggestions?

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
  #include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main ()
{
	int count;
	int length;
	int len;
	char k;
	string string;
	string str;	
	bool done;
	
	done=false;


	cout<<"Enter a string: ";
	cin>>string;

	cout<<"Enter substring to remove: ";
	cin>>str;

	length=string.length();
	len=str.length();

	while (!done)
	{
		if (string.find(str)<=length && string.find(str)>=0)
		{
			done=true;
		}
		else
		{
			cout<<"The substring "<<str<<" could not be found.";
			cout<<"\n";

			cout<<"Press any character to terminate...";
			cout<<"\n";
			cin>>k;

			return 0;
		}
	}
		int a;
		a=string.find(str);
		string.erase(a,len);

	cout<<"Press any character to continue...";
	cout<<"\n";
	cin>>k;

	return 0;
}
Oct 22, 2014 at 6:33pm
line 12 - you can't reuse the name 'string'.
Oct 22, 2014 at 6:51pm
Hmm, worked perfectly... I didn't know you couldn't do that before, that's one thing i won't be forgetting anytime soon. Thank you for the help.
Oct 22, 2014 at 7:05pm
Technically, you can do that. If you had switched the order of lines 12 and 13, it would've compiled fine. But once you've brought into scope an identifier or type that is the same as one in a broader scope, the one in the broader scope is hidden.

It's good practice to avoid doing so. (This may also serve as a compelling argument to use qualified identifiers such as std::string.)
Oct 22, 2014 at 7:12pm
It could. But honestly, the standard library string is so widely used, if I caught any of the coders I work with using the name 'string' to mean anything but the std::string, I'd tie 'em to a cactus in the desert and let the crows eat their eyes out.
Topic archived. No new replies allowed.