Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

idol4u

macrumors newbie
Original poster
Jun 7, 2006
1
0
plzz see my codes and tell me what's wrong?
I want to make c++ for Cgi

#include <fstream>
#include <string>
#include <cstdio>

struct Applicant
{
string name, address, city, phone, mail;
};
class WebProcess
{
string input, dest[5];
Applicant app;
public:

get()
{
input.assign(getenv("QUERY_STRING"));
cout << "Content-type :html/text" << endl << endl;
}

void replaceAll(const string &findstr, const string &replstr)
{
int found = input.find(findstr);
while(found != string::npos)
{
input.replace(found, findstr.length(),replstr);
found = input.find(findstr);
}
}

void urldecode()
{
replaceAll("%2C", ",");
replaceAll("=", " ");
split();
}

void split()
{
int count=0;
char *p;
p=strtok(input.begin(),"&");
while(p != NULL)
{
dest[count] = p;
count++;
p = strtok(0, "&";
}

for(int counter = 0;counter != count;counter++)
dest[counter].erase(0,dest[counter].find("=") + 1);
app.name = dest[0];
app.address = dest[1];
app.city = dest[2];
app.phone = dest[3];
app.mail = dest[4];
}

save()
{
ofstream file;
file.open("applicant.dat",ios::eek:ut|ios::app);
file.write((char *)&app,sizeof(app));
file.close();
}

display()
{
cout << "<html>";
cout << "Name:"<< app.name << "<br>";
cout << "Address:"<< app.address << "<br>";
cout << "City:"<< app.city << "<br>";
cout << "Phone:"<< app.phone << "<br>";
cout << "Mail:"<< app.mail << "<br><br><br><br><br><br>";
cout << "have been recorded.";
cout << "thk";
cout << "</html>";
}
};
int main()
{
WebProcess web;
web.get();
web.urldecode();
web.split();
web.save();
web.display();
return 0;
}

when I compile :::

nui-ibook-g4:~ surasakthepa-armonkit$ g++ ./c++/webadd.cc./c++/webadd.cc:7: error: 'string' does not name a type
./c++/webadd.cc:11: error: 'string' does not name a type
./c++/webadd.cc:15: error: ISO C++ forbids declaration of 'get' with no type
./c++/webadd.cc:21: error: expected %<,%> or %<...%> before '&' token
./c++/webadd.cc:21: error: ISO C++ forbids declaration of 'string' with no type
./c++/webadd.cc:59: error: ISO C++ forbids declaration of 'save' with no type
./c++/webadd.cc:67: error: ISO C++ forbids declaration of 'display' with no type
./c++/webadd.cc: In member function `int WebProcess::get()':
./c++/webadd.cc:17: error: 'input' was not declared in this scope
./c++/webadd.cc:18: error: 'cout' was not declared in this scope
./c++/webadd.cc:18: error: 'endl' was not declared in this scope
./c++/webadd.cc: In member function `void WebProcess::replaceAll(int)':
./c++/webadd.cc:23: error: 'input' was not declared in this scope
./c++/webadd.cc:23: error: 'findstr' was not declared in this scope
./c++/webadd.cc:24: error: 'string' is not a class or namespace
./c++/webadd.cc:24: error: 'npos' was not declared in this scope
./c++/webadd.cc:26: error: 'replstr' was not declared in this scope
./c++/webadd.cc: In member function `void WebProcess::urldecode()':
./c++/webadd.cc:33: error: no matching function for call to 'WebProcess::replaceAll(const char [4], const char [2])'
./c++/webadd.cc:21: note: candidates are: void WebProcess::replaceAll(int)
./c++/webadd.cc:34: error: no matching function for call to 'WebProcess::replaceAll(const char [2], const char [2])'
./c++/webadd.cc:21: note: candidates are: void WebProcess::replaceAll(int)
./c++/webadd.cc: In member function `void WebProcess::split()':
./c++/webadd.cc:42: error: 'input' was not declared in this scope
./c++/webadd.cc:45: error: 'dest' was not declared in this scope
./c++/webadd.cc:47: error: expected `)' before ';' token
./c++/webadd.cc:51: error: 'dest' was not declared in this scope
./c++/webadd.cc:52: error: 'struct Applicant' has no member named 'name'
./c++/webadd.cc:52: error: 'dest' was not declared in this scope
./c++/webadd.cc:53: error: 'struct Applicant' has no member named 'address'
./c++/webadd.cc:54: error: 'struct Applicant' has no member named 'city'
./c++/webadd.cc:55: error: 'struct Applicant' has no member named 'phone'
./c++/webadd.cc:56: error: 'struct Applicant' has no member named 'mail'
./c++/webadd.cc: In member function `int WebProcess::save()':
./c++/webadd.cc:61: error: 'ofstream' was not declared in this scope
./c++/webadd.cc:61: error: expected `;' before "file"
./c++/webadd.cc:62: error: 'file' was not declared in this scope
./c++/webadd.cc:62: error: `ios' has not been declared
./c++/webadd.cc:62: error: 'out' was not declared in this scope
./c++/webadd.cc:62: error: `ios' has not been declared
./c++/webadd.cc: In member function `int WebProcess::display()':
./c++/webadd.cc:69: error: 'cout' was not declared in this scope
./c++/webadd.cc:70: error: 'struct Applicant' has no member named 'name'
./c++/webadd.cc:71: error: 'struct Applicant' has no member named 'address'
./c++/webadd.cc:72: error: 'struct Applicant' has no member named 'city'
./c++/webadd.cc:73: error: 'struct Applicant' has no member named 'phone'
./c++/webadd.cc:74: error: 'struct Applicant' has no member named 'mail'
nui-ibook-g4:~ surasakthepa-armonkit$

thk before
 

x86

macrumors regular
May 25, 2006
166
0
Dearborn, MI
Sorry to say, but you need to go back to the chapter about creating classes before even attempting to fix this... You essentially have a C program here, trying to access the WebProcess class that has never been defined.

[edit:] I would like to add that your functions need to be defined like this: WebProcess::display(). Also, you need the class definition in there as well.
 

plinden

macrumors 601
Apr 8, 2004
4,029
142
It would be easier to read if you put it inside a code block.

Anyway, all your problems are mentioned in the compiler output. I would start by replacing all your "string" references with "std::string" and recompiling. That's not the only problem, of course, but it should clear up your compiler errors a lot. Then go through them one at a time, finding a solution for each one.
 

x86

macrumors regular
May 25, 2006
166
0
Dearborn, MI
Oops! I missed the top part of your code, disreguard my first post. But yea, posting it in code blocks help out a lot.
 

ipastuszak

macrumors newbie
Jun 7, 2006
1
0
#include <fstream>
#include <string>
#include <cstdio>
#include <iostream>
#include <cstring>

// prevents reoccurrence of "std::" throughout
using namespace std;

struct Applicant
{
string name, address, city, phone, mail;
};
class WebProcess
{
string input, dest[5];
Applicant app;
public:

void get()
{
// need a guard here otherwise bus error
input.assign(getenv("QUERY_STRING"));
cout << "Content-type :html/text" << endl << endl;
}

void replaceAll(const string &findstr, const string &replstr)
{
int found = input.find(findstr);
while(found != string::npos)
{
input.replace(found, findstr.length(),replstr);
found = input.find(findstr);
}
}

void urldecode()
{
replaceAll("%2C", ",");
replaceAll("=", " ");
split();
}

void split()
{
int count=0;
char *p;
p=strtok(const_cast<char*>(input.c_str()),"&");
while(p != NULL)
{
dest[count] = p;
count++;
p = strtok(0, "&");
}

for(int counter = 0;counter != count;counter++)
dest[counter].erase(0,dest[counter].find("=") + 1);
app.name = dest[0];
app.address = dest[1];
app.city = dest[2];
app.phone = dest[3];
app.mail = dest[4];
}

void save()
{
ofstream file;
// you had "ios::ut" - think you meant "out"
file.open("applicant.dat",ios::eek:ut|ios::app);
file.write((char *)&app,sizeof(app));
file.close();
}

void display()
{
cout << "<html>";
cout << "Name:"<< app.name << "<br>";
cout << "Address:"<< app.address << "<br>";
cout << "City:"<< app.city << "<br>";
cout << "Phone:"<< app.phone << "<br>";
cout << "Mail:"<< app.mail << "<br><br><br><br><br><br>";
cout << "have been recorded.";
cout << "thk";
cout << "</html>";
}
};
int main()
{
WebProcess web;
web.get();
web.urldecode();
web.split();
web.save();
web.display();
return 0;
}
 

Soulstorm

macrumors 68000
Feb 1, 2005
1,887
1
Does anyone here write in code blocks?
Code:
#include <fstream>
#include <string>
#include <cstdio>
#include <iostream>
#include <cstring>

// prevents reoccurrence of "std::" throughout
using namespace std;

struct Applicant
{
	string name, address, city, phone, mail;
};
class WebProcess
{
	string input, dest[5];
	Applicant app;
public:
		
		void get()
	{
			// need a guard here otherwise bus error
			input.assign(getenv("QUERY_STRING"));
			cout << "Content-type :html/text" << endl << endl;
	}
	
	void replaceAll(const string &findstr, const string &replstr)
	{
		int found = input.find(findstr);
		while(found != string::npos)
		{
			input.replace(found, findstr.length(),replstr);
			found = input.find(findstr);
		}
	}
	
	void urldecode()
	{
		replaceAll("%2C", ",");
		replaceAll("=", " ");
		split();
	}
	
	void split()
	{
		int count=0;
		char *p;
		p=strtok(const_cast<char*>(input.c_str()),"&");
		while(p != NULL)
		{
			dest[count] = p;
			count++;
			p = strtok(0, "&");
		}
		
		for(int counter = 0;counter != count;counter++)
			dest[counter].erase(0,dest[counter].find("=") + 1);
		app.name = dest[0];
		app.address = dest[1];
		app.city = dest[2];
		app.phone = dest[3];
		app.mail = dest[4];
	}
	
	void save()
	{
		ofstream file;
		// you had "ios::ut" - think you meant "out"
		file.open("applicant.dat",ios::out | ios::app);
		file.write((char *)&app,sizeof(app));
		file.close();
	}
	
	void display()
	{
		cout << "<html>";
		cout << "Name:"<< app.name << "<br>";
		cout << "Address:"<< app.address << "<br>";
		cout << "City:"<< app.city << "<br>";
		cout << "Phone:"<< app.phone << "<br>";
		cout << "Mail:"<< app.mail << "<br><br><br><br><br><br>";
		cout << "have been recorded.";
		cout << "thk";
		cout << "</html>";
	}
};
int main()
{
	WebProcess web;
	web.get();
	web.urldecode();
	web.split();
	web.save();
	web.display();
	return 0;
}
 

demallien

macrumors regular
Oct 13, 2005
137
0
Umm, unless my C++ has completely gone, you're going to need to add a '.h' to the end of each file name in your includes... so for example

#include <fstream>

becomes

#include <fstream.h>

If that still doesn't work, you're going to have to figure out what your compiler is doing with the include paths... You should be OK though, these are all stdlib headers... Should clear up the vast majority of those errors
 

Josh

macrumors 68000
Mar 4, 2004
1,640
1
State College, PA
demallien said:
Umm, unless my C++ has completely gone, you're going to need to add a '.h' to the end of each file name in your includes... so for example

#include <fstream>

becomes

#include <fstream.h>

If that still doesn't work, you're going to have to figure out what your compiler is doing with the include paths... You should be OK though, these are all stdlib headers... Should clear up the vast majority of those errors

Nah, things like <fstream> <iostream> <string> <cmath> etc are included libraries that don't take (and won't work with) a ".h."

Only when you're using a library you created or got elsewhere that isn't included in the standard C++ libraries iswhen you need to do that.
 

plinden

macrumors 601
Apr 8, 2004
4,029
142
ipastuszak said:
// prevents reoccurrence of "std::" throughout
using namespace std;
I don't have to write C++ often, but I hate it when I'm working on code that others have written where they have used "using namespace ...;"

It's pure laziness and makes your code less robust, since you are more likely to get name conflicts. You probably wouldn't get conflicts with "std::" but as far as I'm concerned that's the start of a slippery slope.
 

Soulstorm

macrumors 68000
Feb 1, 2005
1,887
1
demallien said:
Umm, unless my C++ has completely gone, you're going to need to add a '.h' to the end of each file name in your includes... so for example

#include <fstream>

becomes

#include <fstream.h>

If that still doesn't work, you're going to have to figure out what your compiler is doing with the include paths... You should be OK though, these are all stdlib headers... Should clear up the vast majority of those errors
These libraries are included without the '.h'. The ones you are referring to are old as hell, and they shouldn't be used because they are deprecated.
 

therevolution

macrumors 6502
May 12, 2003
468
0
plinden said:
I don't have to write C++ often, but I hate it when I'm working on code that others have written where they have used "using namespace ...;"

It's pure laziness and makes your code less robust, since you are more likely to get name conflicts. You probably wouldn't get conflicts with "std::" but as far as I'm concerned that's the start of a slippery slope.
I disagree that it's about laziness. I think it makes the code more readable if you don't have "std::" littered all over the place.
 

superbovine

macrumors 68030
Nov 7, 2003
2,872
0
therevolution said:
I disagree that it's about laziness. I think it makes the code more readable if you don't have "std::" littered all over the place.

the .h files will be eventually deprecated, not for a long while though. I also believe having std:: all over the place makes the code harder to read. to resolve issues placing a few std: with some comments is much more readable. however, one should code in the style guidelines of whatever shop or project you are working in.
 

ozubahn

macrumors regular
Feb 15, 2003
100
0
Connecticut
plinden said:
I don't have to write C++ often, but I hate it when I'm working on code that others have written where they have used "using namespace ...;"

It's pure laziness and makes your code less robust, since you are more likely to get name conflicts. You probably wouldn't get conflicts with "std::" but as far as I'm concerned that's the start of a slippery slope.

I employ the "using namespace" statement regularly, but only within limited scopes, such as :

Code:
{
   using namespace std;
   cout << "Hi there." << endl;
}

The brackets restrict the using directive to a very limited scope, so it is safe. What you don't want to do is place a using directive in a header file that will be included by other files, since it will then leak into a large body of code and increase the chance of ambiguities.
 

Soulstorm

macrumors 68000
Feb 1, 2005
1,887
1
Well, to avoid name conflicts, you can declare this before your main code:

Code:
using std::cout;
using std::cin;
using std::endl;
And no conflicts are made;
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.