Jump to content
  • Advertisement
Sign in to follow this  
jlg

Problems with dev c++ [Solved]

This topic is 4767 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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>

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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. )

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!