Thread: stupid program question

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    132

    stupid program question

    I'm reading a book on C++ and this is the first simple quiz question...

    it says run this and guess what it does, now i know what it does but it wont compile.

    Code:
    #include <iostream>
    int main();
    {
        	int x = 5;
    	int y = 7;
    	cout "\n";
    	cout << x + y << " " << x * y;
    	cout "\n";
    	return 0;
    }
    on DEV C++ it says - 3 C:\Documents and Settings\Reece.LAPTOP\Desktop\test.cpp expected unqualified-id before '{' token

    3 C:\Documents and Settings\Reece.LAPTOP\Desktop\test.cpp expected `,' or `;' before '{' token

    and on Visual C++ Express Edition it says

    Compiling...
    tester.cpp
    .\tester.cpp(3) : error C2447: '{' : missing function header (old-style formal list?)
    Build log was saved at "file://c:\Documents and Settings\Reece.LAPTOP\My Documents\Visual Studio 2005\Projects\tester\tester\Debug\BuildLog.htm"
    tester - 1 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


    can someone please just tell me whats wrong cuz its really winding me up because its soo simple.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    #include <iostream>
    int main();  // Remove this semicolon
    {
        int x = 5;
        int y = 7;
        std::cout << "\n";
        std::cout << x + y << " " << x * y;
        std::cout << "\n";
        return 0;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    For now, add this.
    Code:
    #include <iostream>
    using namespace std;
    int main()//;
    {
        	int x = 5;
    	int y = 7;
    	cout << "\n";
    	cout << x + y << " " << x * y;
    	cout << "\n";
    	return 0;
    }
    And copy better.
    Last edited by Dave_Sinkula; 05-25-2006 at 11:34 AM. Reason: And I should work faster.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    Hmm, ok thanks. but why does the STD:: and deleting the ; work?

    and using namespace std; doesnt work with it. get same error.


    EDIT! using namespace std; worked with the << sorry, u didnt put the << till after

    also, why the ; deleting?

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Because int main(); is a function prototype, so all your code is ignored. Not that typing it is a problem: I see no reason why this wouldn't compile:
    Code:
    #include <iostream> 
    int main(void); // but it is a waste of kestrokes
    
    int main(void) {
        int x = 5, y = 7;
        std::cout << "\n";
        std::cout << x + y << " " << x * y;
        std::cout << "\n";
        return 0;
    }
    std is a namespace that all standard templates, objects, and functions are in. This needs to be explicitly declared, or you must say you are using the namespace std.
    Last edited by whiteflags; 05-25-2006 at 12:22 PM.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    yeh it works as STD::

    i am used to using int main(); as i normaly add namespace std; but i didnt think to this time.

    sorry for posting a stupid thing like this.

    Hugo.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    another one. giving me the same error about missing function header for thr '{'

    Code:
    #include <iostream>
    using namespace std;
    // Function demonstration function
    // prints out a usefull message
    
    void DemonstrationFunction()
    {
    	cout<<"In Demonstration Function\n";
    
    }
    // Function main - prints out a message, then
    // calls DemonstrationFunction, then prints out
    // a second message
    int main();
    {
    	cout<<"In Main\n";
    	DemonstrationFunction();
    	cout<<"Back in main\n";
    	system("PAUSE");
    	return 0;
    }
    really bugging me. sorry. thanks.

  8. #8
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Code:
    int main();
    {
    	cout<<"In Main\n";
    	DemonstrationFunction();
    	cout<<"Back in main\n";
    	system("PAUSE");
    	return 0;
    }
    You were just told about this!
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    sorry just fixed it.

    yeh but the last one worked WITH ; in it still. and they said it would work with ; as long as i added namespace

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    There is the excepton that you called system without including stdlib.h. That being said, calling system is the worst way to do a lot of things.

    The program pauses for input only, so just use a standard input function to pause the program, like cin.get() or something.

  11. #11
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    it shouldn't - just stop doing that and you won't run into that
    problem again - best solution

    [update1]
    they weren't saying that it would work with namespace std - that
    was for the cout and stuff.
    [/update1]

    [update 2]
    system is defined in iostream - since iostream was designed to
    replace stdio.h and stuff.
    [/update 2]
    Last edited by Richie T; 05-25-2006 at 01:26 PM.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    ok, its just ive always put ; after main im just so used to it.

    btw, is there anyway to stop the program from exiting straight the way other than putting system("PAUSE"); at the end?

  13. #13
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    getchar(); ?

    I don't know where that's defined, or whether it's in C or C++ or whether it matters. Or somewhere here.

  14. #14
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    yes - as already mentioned a cin.get() does the job, but
    sometimes you'll need to clear the input buffer, otherwise it'll just
    eat any junk in the buffer. Just do something like this:

    Code:
    int main (void)
    {
        int ch;
        //body of program
    
        while ((ch = cin.get()) != '\n' && ch != EOF);
    
        cin.get ();
        return 0;
    }
    or call cin.ignore() before cin.get()

    [update]
    getchar() works too - in C its in stdio.h but it happens to be in
    iostream too - like a lot of things from stdio.h and stdlib.h
    [/update]
    Last edited by Richie T; 05-25-2006 at 02:02 PM.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    k thanks alot mate.

    hmm wots EOF?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. question about the loop in case conversion program
    By Elhaz in forum C++ Programming
    Replies: 8
    Last Post: 09-20-2004, 04:06 PM
  3. program design question
    By theroguechemist in forum C++ Programming
    Replies: 4
    Last Post: 03-02-2004, 08:45 PM
  4. Stupid Question.........
    By incognito in forum Windows Programming
    Replies: 6
    Last Post: 04-06-2002, 09:59 PM
  5. Question about "Answer Key" Program
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 11-14-2001, 09:55 PM