Thread: stupid program question

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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,612
    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,612
    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
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    EOF means end of file

    putting a semi-colon after the main function is just asking for trouble.
    Look at this...

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    // function prototypes
    void func ( void );
    void funct2 ( void );  // it is ok to end function prototypes with a semi=colon
    
    int main ( void );  // this is an error
    {
    func(); // function calls end with a semi-colon
    
    return 0;
    
    cin.get();
    }
    
    void func ( void ); // this is an error
    {
    func2();
    }
    
    void func2 ( void ); // so is this
    {
    }
    in general. NEVER place a semicolon after function title implementation.
    ONLY include a semi-colon after a function prototype and when you call a function

    Hope this helps -pete

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You'll need to understand the difference between a function DECLARATION and a function DEFINITION. A declaration simply asserts that a function exists and is ready for use.

    Code:
    int main(int, char**);
    So you can use main() although you haven't defined it's contents (although calling main() is a VERY BAD IDEA™). But to really compile your code, you'll need to define what main() does. This is the definition, which does not include the semicolon to differentiate it from a declaration:

    Code:
    int main(int n, char **v)
    {
        std::cout << "Hello World!\n";
        std::cin.get();
    }
    Last edited by jafet; 05-26-2006 at 09:23 PM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

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