![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jul 2007
Posts: 17
Rep Power: 0
![]() |
can someone tell me what i did wrong with this text? it seems simple enough to me, but for some reason, Dev-C++ refuses to compile it
#include <iostream> using namespace std; void main() { int age = 0; cout << "How old is your dog?" endl; cin age; cout << "Your dog is "; cout << age; cout << " years old!" endl; cin.get return 0; } Here are the errors i get : 'main' must return 'int', expected ';' before "endl" age endl and return statement cannot resolve address of overloaded function |
|
|
|
|
|
#2 | |
|
Programming Guru
![]() ![]() ![]() |
-You are missing << prior to both of your endl; statements.
-You are missing >> between cin and age. -You can put your output on one line, there is no need to split it out like you are doing. -Your return type of main is void, yet you return 0 at the end. The return type should be of type int. -Your errors within the compiler are pointing you in the right direction. You need to read up more on cin, cout, and the << and >> operators. -Next time, put your code in [ CODE ] yourcode [ /CODE ] tags. I'll let someone else elaborate on that, in the interim you can read the forum rules. This will work for you, if you have ANY questions. Ask... don't assume anything. #include <iostream>
using namespace std;
int main()
{
int age = 0;
char nothing;
cout << "How old is your dog?" << endl;
cin >> age;
cout << "Your dog is " << age << " years old!" << endl;
cout << "Press any key to continue...";
cin >> nothing;
return 0;
}Executed: Quote:
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jul 2007
Posts: 17
Rep Power: 0
![]() |
Hmm, so by adding the
char nothing; cin >> nothing; |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
You don't need to assign a variable. Just use cin.get ();
Note that if previous operations leave material in the buffer, you will need to empty the buffer before the cin (regardless of the form you use). Note also that input buffers are not available until an end of line or end of file condition. Therefore, it is better to use "Press ENTER to continue" than "Press any key...". The latter requires non-portable code which is operating system and implementation dependent. Suggested reading: The FAQ/rules, the "How to Post a Question" thread, and the documentation for the functions you use. Bear in mind that it is important to test your input statements for success. I can break any of these examples with 2 keystrokes.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 | |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,010
Rep Power: 5
![]() |
Quote:
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
|
#6 |
|
Programmer
Join Date: Jun 2005
Posts: 68
Rep Power: 4
![]() |
Some words of advice for debugging:
Start with the first error first. Often times, an error at the beginning of the program can trip false errors later on (especially when the initial mistake was improper placement of scope brackets. |
|
|
|
|
|
#7 |
|
Programming Guru
![]() ![]() ![]() |
For that matter, the actual line number of the errors are not always exact. You will need to look a few lines above also.
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| pig latin exercise - I'm stuck | boatn19 | Visual Basic | 4 | Jun 17th, 2007 10:34 PM |
| Stuck with a C problem | Polaris | C++ | 8 | Aug 19th, 2006 3:30 PM |
| stuck.... | NightShade01 | Visual Basic .NET | 4 | Oct 30th, 2005 1:37 AM |
| Multiple http-requests are stuck | MereMortal | C++ | 0 | May 4th, 2005 3:08 AM |
| stuck at this program | strider496 | C++ | 17 | Mar 22nd, 2005 2:46 PM |