_getch() and doing a carriage return

This is a discussion on _getch() and doing a carriage return within the C++ Programming forums, part of the General Programming Boards category; ok im using msvc++ 6.0. All i want to do is display the numbers 1 to 5 and THATS IT! ...

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    _getch() and doing a carriage return

    ok im using msvc++ 6.0. All i want to do is display the numbers 1 to 5 and THATS IT! Except when the program is done i want it to do a pause until i hit a key so it wont close right away. Also, i want each number to be displayed on the next line, not the same one. first of all, when i put a _getch() by the end of my program, and then execute it, it stays up but i don't see anything. if i take out the getch, i see "12345" on the same line. if i make it an executable file and launch it that way, i dont want it to close right away, but when i put the _getch in then i dont see anything and the window stays up. hmm, what do i do?

  2. #2
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    Ok, I almost got it

    ok look at this now, its a different program, same concept:

    #include "iostream.h"
    #include "conio.h"
    int main()
    {
    int a;
    int b;
    int c;
    cout<<"Enter two numbers to be multiplied: ";
    cin>>a>>b;
    c=a*b;
    cout<<c;
    _getch();
    return 0;
    }

    Ok, how come the product of the two numbers is displayed AFTER i hit a key, not before? See, i want the answer to a*b to be displayed, then if you press a key the program exits. arg, whats wrong!?

  3. #3
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Try using this in place of cout <<c; -

    cout<<c<<flush;
    zen

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    55
    Heh..exact same thing as i have dude. Cept im doing Deviding and everything in there aswell. What you want is this:

    #include <iostream.h>
    #include <stdlib.h>
    #include <windows.h>
    int main
    {

    int a // first number
    int b // second number
    int x // multiplied numbers


    cout<<"Hey,Enter a number!"endl; //gets first number
    cin>>a; //first number recorded
    cout<<"Enter another!"endl; //gets the other
    cin>>b; //other one recorded
    int x = a*b
    cout<<"Your 2 numbers muliplied are: "<<x <<endl;
    system("PAUSE") //handy lil thing so the program dont close
    return 0;
    }

    that should work
    D4050
    Glorified C++ Programmer
    Console, DOS, HTML, Javascript, Visual Basics Compatible

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    55
    aack....sorry...all the endl; SHOULD be <<endl; not endl;
    D4050
    Glorified C++ Programmer
    Console, DOS, HTML, Javascript, Visual Basics Compatible

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    55
    CORRECTED CODE:

    #include <iostream.h>
    #include <stdlib.h>
    #include <windows.h>
    int main
    {

    int a // first number
    int b // second number
    int x // multiplied numbers


    cout<<"Hey,Enter a number!"<<endl; //gets first number
    cin>>a; //first number recorded
    cout<<"Enter another!"<<endl; //gets the other
    cin>>b; //other one recorded
    int x = a*b;
    cout<<"Your 2 numbers muliplied are: "<<x <<endl;
    system("PAUSE"); //handy lil thing so the program dont close
    return 0;
    }
    D4050
    Glorified C++ Programmer
    Console, DOS, HTML, Javascript, Visual Basics Compatible

  7. #7
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    thanks

    thanks, that worked. but....what exactly is it that "flush" does?

  8. #8
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Output using cout is buffered. _getch()'s input isn't. As buffered output and input is linked your buffered output won't be flushed until after _getch() has had it's input. Both endl and flush will imediately send the output to the device (endl will also output a newline).
    zen

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21