Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 31st, 2007, 3:29 PM   #1
Z3nathur
Newbie
 
Z3nathur's Avatar
 
Join Date: Jul 2007
Posts: 17
Rep Power: 0 Z3nathur is on a distinguished road
Question Stuck on cin/cout

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
Z3nathur is offline   Reply With Quote
Old Jul 31st, 2007, 3:42 PM   #2
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
-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:
[jpowers@pandora ~]$ ./a.out
How old is your dog?
5
Your dog is 5 years old!
Press any key to continue...

[jpowers@pandora ~]$
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Jul 31st, 2007, 3:53 PM   #3
Z3nathur
Newbie
 
Z3nathur's Avatar
 
Join Date: Jul 2007
Posts: 17
Rep Power: 0 Z3nathur is on a distinguished road
Hmm, so by adding the
 
char nothing; 
cin >> nothing;
do you pause the program at the cin point in order to keep the console open without using a system pause?
Z3nathur is offline   Reply With Quote
Old Jul 31st, 2007, 4:09 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jul 31st, 2007, 4:09 PM   #5
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,033
Rep Power: 5 lectricpharaoh will become famous soon enough
Quote:
Originally Posted by Z3nathur
do you pause the program at the cin point in order to keep the console open without using a system pause?
Exactly.
__________________
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
lectricpharaoh is offline   Reply With Quote
Old Aug 2nd, 2007, 9:02 AM   #6
sixstringartist
Programmer
 
Join Date: Jun 2005
Posts: 68
Rep Power: 4 sixstringartist is on a distinguished road
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.
sixstringartist is offline   Reply With Quote
Old Aug 2nd, 2007, 9:27 AM   #7
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:53 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC