Problems with dev c++ [Solved]

Started by
4 comments, last by Zahlman 18 years, 8 months ago
I'm using dev c++ to learn c++, but I'm having some problems with strings. My code reads: #include <iostream.h> #include <stdlib.h> #include <string.h> using std::string; int main() { string name; cout << "Enter name "; cin >> name; cout << "You entered: " << name << endl; system("PAUSE"); return 0; } but when I compile, I get the following errors: c:\docume~1\jorge\mydocu~1\entert~1\progra~1\c__~1\projects\untitl~1.cpp: In function `int main()': c:\docume~1\jorge\mydocu~1\entert~1\progra~1\c__~1\projects\untitl~1.cpp:9: `string' undeclared (first use this function) c:\docume~1\jorge\mydocu~1\entert~1\progra~1\c__~1\projects\untitl~1.cpp:9: (Each undeclared identifier is reported only once c:\docume~1\jorge\mydocu~1\entert~1\progra~1\c__~1\projects\untitl~1.cpp:9: for each function it appears in.) c:\docume~1\jorge\mydocu~1\entert~1\progra~1\c__~1\projects\untitl~1.cpp:9: parse error before `;' c:\docume~1\jorge\mydocu~1\entert~1\progra~1\c__~1\projects\untitl~1.cpp:11: `name' undeclared (first use this function) According to the book I'm using (beginning game programming with c++) my code is correct. What is the error then? UPDATE: I tried Fruny's suggestion, and it solved the problem. Thanks for all the responses though. [Edited by - jlg on July 28, 2005 9:01:19 PM]
Advertisement
The problem isn't with Dev-C++ but with your choice of header files.

<iostream.h> is not a standard C++ header, you want <iostream>. cout, cin and endl will be in the std namespace.

<stdlib.h> is a C header, prefer to use <cstdlib>, the C++ equivalent.

<string.h> is a C header for manipulation of char* strings, you want <string> to manipulate C++ strings. If you want to manipulate char* strings in C++, prefer to use <cstring> to <string.h>
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Make your headers undeprecated.
<iostream>
<string>
<stdlib.h>

EDIT: Beat me to it
--------------------------------A man of few words does not mean he does not have big ideas
try:
#include <iostream>
#include <string.h>
using std::string;
using namespace std;
int main()
{
string name;
cout << "enter name";
cin >> name;
cout << "you entered : " << name << "."<<endl;
system("PAUSE");
return 0;
}

Also, I use getch(); rather than pause, *i'm guessing they do the same thing?*, but I believe a <conio.h> is also needed for that.

I'm fairly new to Dev-C++ myself, but it seems that you are using way too much code for such a simple application/compile thingy.
conio.h is required for the use of getch() . Myself i prefer the use of getch() rather than pause.

Also use void main() instead of int main() if your program isn't going more far than what it is at the moment since you won't need to return a value, that will avoid that ugly return 0; ( which i find very ugly in fact ^^ ) just 1 less line yay. ( I prefer when everything is keep as short as possible. )
Quote:Original post by jlg
According to the book I'm using (beginning game programming with c++) my code is correct.


Your book is outdated; the headers have moved on - see Fruny's post for details.

Oh, once using <iostream>, note that 'cin', 'cout' and 'endl' will be in the std namespace as well.

Quote:Original post by whiz_kid
<stdlib.h>


No, it's <cstdlib>.

Quote:Original post by doyleman
try:
#include <iostream>
#include <string.h>


No; as Fruny observed, you want <string>.

Quote:
using std::string;
using namespace std;


The latter makes the former unnecessary.

Quote:
Also, I use getch(); rather than pause, *i'm guessing they do the same thing?*, but I believe a <conio.h> is also needed for that.


There are several ways to pause a program run artificially at the end. There are also several good reasons not to do it; learn to run your program from the command line instead.

Quote:I'm fairly new to Dev-C++ myself, but it seems that you are using way too much code for such a simple application/compile thingy.


He's using roughly the same amount you are. The rest of the post is the errors he's getting. :)

Quote:Original post by deathwearer
Also use void main() instead of int main() if your program isn't going more far than what it is at the moment since you won't need to return a value, that will avoid that ugly return 0; ( which i find very ugly in fact ^^ ) just 1 less line yay. ( I prefer when everything is keep as short as possible. )


No. As a special case, a return statement is not required for main() regardless, but void main() is not a legal prototype for the function in C++ - it must be int main(). It will implicitly return 0 if that part is left out.

This topic is closed to new replies.

Advertisement