PDA

View Full Version : help Me for C++




idol4u
Jun 7, 2006, 06:42 PM
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::out|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
Jun 7, 2006, 07:28 PM
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
Jun 7, 2006, 07:44 PM
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
Jun 7, 2006, 07:48 PM
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
Jun 7, 2006, 10:15 PM
#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;
}

Soulstorm
Jun 8, 2006, 02:04 AM
Does anyone here write in code blocks?
#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;
}

almightyshoe
Jun 8, 2006, 02:47 AM
I concur, use code blocks. The compiler errors you get are usually self-explanatory. I'll run through it tomorrow and see if I can lend a hand.

demallien
Jun 8, 2006, 06:29 AM
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
Jun 8, 2006, 11:07 AM
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
Jun 8, 2006, 11:17 AM
// 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
Jun 8, 2006, 11:43 AM
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
Jun 8, 2006, 02:17 PM
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
Jun 8, 2006, 06:08 PM
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
Jun 21, 2006, 10:12 PM
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 :


{
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
Jun 22, 2006, 01:48 AM
Well, to avoid name conflicts, you can declare this before your main code:

using std::cout;
using std::cin;
using std::endl;
And no conflicts are made;