Results 1 to 6 of 6

Thread: console program

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2001
    Posts
    163

    console program

    Why this program works fine with bc++ but it exit just ater getting the value of x from console in vc++?



    #include <iostream.h>
    void main()
    {
    int x;

    cin >> x;
    // cout<<endl;

    x=x-10;
    cout << x;

    cin.get();
    }
    Purushottam

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Works fine for me. Try starting it from a command prompt. You're not seeing the printout because the program closes.

    Also, you're horribly non-standard there:
    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main() {
        int x;
    
        cin >> x;
    
        x = x - 10;
        cout << x << endl;
    
        system("pause");
    }
    The cin.get() at the end didn't work, so I used the method of choice for most people. Note that that will only work on DOS or DOS-compatible systems.

    When you run a console program from VC++, it automatically adds its own prompt anyway.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2001
    Posts
    163
    Thank U very much. It works (waits until we hit any key in bc++) but not in vc++.


    #include <iostream.h>
    #include <conio.h>
    void main()
    {
    int x;

    cin >> x;
    cout<<endl;

    x=x-10;
    cout << x;

    //cin.get();
    while(!kbhit());
    }
    Purushottam

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You missed my second point completely. Don't use <iostream.h>. Technically you shouldn't use <conio.h> either, but...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main() {
        int x;
    
        cin >> x;
    
        x = x - 10;
        cout << x << endl;
    
        system("pause");
    }

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Huh?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width