Thread: Help- Program won't run (Using MSVC++)

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    43

    Help- Program won't run (Using MSVC++)

    I feel like an idiot asking you guys about what is probably a very simple thing, but...

    I am using Microsoft Visual C++ 6.0 Introductory Edition, and my program won't run. I'm doing the programming on my other computer, and I don't have all of the code on hand, so it will be easier for other programmers with the same compiler to help me.

    I made a simple win32 application, and added 2 lines of code to the source code file. I put #include <isostream.h> under the line with #include "stdasx.h" (or something like that... Can't remember if the last 3 characters were asx or not, but I think they were), and I added cout<<"Hello world"; (complex, isn't it?) between the braces and before the line with return 0;. I compiled the code, and it came up with 0 errors and 0 warnings. But, when I tried to run the program, it didn't run anything. Thanks in advance

  2. #2
    NewbieUser
    Guest
    its <iostream.h>

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    43
    Yeah that's just a typo in my message... I have iostream in code.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >> But, when I tried to run the program, it didn't run anything.<<
    Does this sound familiar ?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    NewbieUser
    Guest
    Okay since I am guessing it is a simple outputting problem...ill show you it here

    Code:
    #include<iostream.h>
    
    int main()
    {
        cout << "HelloWorld";
    
    	return(0);
    }
    thats outputting it, I have no clue about the other .h file though

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main (void)
    {
        cout << "Hello World" << endl;
    
        system ("pause");
    
        return 0;
    }

  7. #7
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Yeah, check the link in Hammer's thread. That's your problem. Your program finishes so Windows gets rid of the window.
    Away.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I recommend <iostream> instead of <iostream.h>. Don't start learning C++ with outdated standards.

    So the program would look like
    Code:
    // This is a VC++-specific header. Avoid it, remove the header from the project
    // and all include directives from your source. Don't use this header
    // unless you're using MFC, which you shouldn't for quite some time.
    // You can prevent VC++ from adding the header by unchecking the
    // "precompiled header" checkbox in the project wizard.
    //#include "stdafx.h"
    // The new C++ standard says the .h headers are deprecated.
    #include <iostream>
    #include <cstdlib>
    // The new headers declare all elements in the namespace std.
    // Follow the link to find out what this line means
    using namespace std;
    
    int main (void)
    {
        cout << "Hello World" << endl;
    
        system ("pause");
    
        return 0;
    }
    The system("pause"); is actually not necessary. If you run a console app as it is intended (from the console window) then the console won't close when the app ends. And if you start it from within VC++ by pressing Ctrl+F5 VC++ keeps the console window open.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    43
    Well... I'll try that one later, but I don't have time to now (stupid school!).

    Anyway, I got something to run finally, but it doesn't do the "Hello World!" text it was supposed to. Instead, it just says "Press any key to continue." I've got my code here this time, so here it is...

    Code:
     
    #include "stdafx.h"
    #include <iostream.h>
    #include <stdlib.h>
    
    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    
    {
     	
    	cout<<"Hello World!!!";
    	system("pause");
    	return 0;
    
    }
    Whenever I remove #include "stdafx.h" or try to change the long main thing to just int main(), I get error messages and it won't let me run it at all.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Woah.

    Trash the project and start a new one. You chose a win32 project with precompiled header. This is NOT what you want. Make sure you start a CONSOLE application and an EMPTY project. Then add a source file to it (by using Project->Add->Add New Item... or something like that). Give the source file some name (like main.cpp) and write your code with a normal main and without stdafx.h.

    And use the headers without the .h as I showed you.

    And do the << endl thing too or else the pause message will be on the same line as the "Hello World", and that doesn't look nice.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    May 2003
    Posts
    43
    Ok! Thanks for the help and tips Hopefully I can get it to work now.

  12. #12
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Originally posted by CornedBee
    I recommend <iostream> instead of <iostream.h>. Don't start learning C++ with outdated standards.

    So the program would look like
    Code:
    // This is a VC++-specific header. Avoid it, remove the header from the project
    // and all include directives from your source. Don't use this header
    // unless you're using MFC, which you shouldn't for quite some time.
    // You can prevent VC++ from adding the header by unchecking the
    // "precompiled header" checkbox in the project wizard.
    //#include "stdafx.h"
    // The new C++ standard says the .h headers are deprecated.
    #include <iostream>
    #include <cstdlib>
    // The new headers declare all elements in the namespace std.
    // Follow the link to find out what this line means
    using namespace std;
    
    int main (void)
    {
        cout << "Hello World" << endl;
    
        system ("pause");
    
        return 0;
    }
    The system("pause"); is actually not necessary. If you run a console app as it is intended (from the console window) then the console won't close when the app ends. And if you start it from within VC++ by pressing Ctrl+F5 VC++ keeps the console window open.
    I should mention, not all compilers have iostream working properly... so some people have to use the .h

  13. #13
    Registered User
    Join Date
    May 2003
    Posts
    43
    Yeah... I got it to work But I did have to use <iostream.h>

    Anyway, now that I got that problem out of the way, I've been able to do a bunch of programs from the code in the tutorials... Plus, I wrote a simple calculator (+, -, *, / only, not scientific) using what I had learned... Though I have run into a few minor problems with that one...

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You should get yourself a better compiler.
    No, wait, you're using VC++6. So if you have to use <iostream.h> you're doing something wrong. VC++ DOES support the new style headers.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Originally posted by CornedBee
    You should get yourself a better compiler.
    No, wait, you're using VC++6. So if you have to use <iostream.h> you're doing something wrong. VC++ DOES support the new style headers.
    Yes, but I've noticed that they don't always work

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Replies: 3
    Last Post: 07-11-2005, 03:07 AM
  4. plz help me run this program!!!
    By galmca in forum C Programming
    Replies: 8
    Last Post: 02-01-2005, 01:00 PM
  5. Cannot run program in background?
    By registering in forum Linux Programming
    Replies: 3
    Last Post: 06-16-2003, 05:47 AM