Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Stuck on cin/cout (http://www.programmingforums.org/showthread.php?t=13673)

Z3nathur Jul 31st, 2007 4:29 PM

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

Infinite Recursion Jul 31st, 2007 4:42 PM

-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 ~]$

Z3nathur Jul 31st, 2007 4:53 PM

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?

DaWei Jul 31st, 2007 5:09 PM

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.

lectricpharaoh Jul 31st, 2007 5:09 PM

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.

sixstringartist Aug 2nd, 2007 10:02 AM

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.

Infinite Recursion Aug 2nd, 2007 10:27 AM

For that matter, the actual line number of the errors are not always exact. You will need to look a few lines above also.


All times are GMT -5. The time now is 3:11 AM.

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