Running a simple c++ program

Discussion in 'C++' started by lopez, May 21, 2011.

  1. lopez

    lopez Guest

    Hi,

    I tried running the following program using cygwin.
    /** @file list0201.cpp */
    /** Listing 2-1. Reading Test */
    /// Read the program and determine what the program does.

    #include <iostream>
    #include <istream>
    #include <limits>
    #include <ostream>

    int main()
    {
    int min(std::numeric_limits<int>::max());
    int max(std::numeric_limits<int>::min());
    bool any(false);
    int x;
    while (std::cin >> x)
    {
    any = true;
    if (x < min)
    min = x;
    if (x > max)
    max = x;
    }

    if (x)
    std::cout << "min = " << min << "\nmax = " << max << '\n';
    }

    the command I used is

    g++ list0201.cpp -o list0201 //to compile

    and

    ../list0201 //to run


    When I run I am getting a blank screen which does not seem to
    terminate .. Can someone help me
    lopez, May 21, 2011
    #1
    1. Advertising

  2. lopez

    Kai-Uwe Bux Guest

    lopez wrote:

    > Hi,
    >
    > I tried running the following program using cygwin.
    > /** @file list0201.cpp */
    > /** Listing 2-1. Reading Test */
    > /// Read the program and determine what the program does.
    >
    > #include <iostream>
    > #include <istream>
    > #include <limits>
    > #include <ostream>
    >
    > int main()
    > {
    > int min(std::numeric_limits<int>::max());
    > int max(std::numeric_limits<int>::min());
    > bool any(false);
    > int x;
    > while (std::cin >> x)
    > {
    > any = true;
    > if (x < min)
    > min = x;
    > if (x > max)
    > max = x;
    > }
    >
    > if (x)


    should this be

    if (any)

    > std::cout << "min = " << min << "\nmax = " << max << '\n';
    > }
    >
    > the command I used is
    >
    > g++ list0201.cpp -o list0201 //to compile
    >
    > and
    >
    > ./list0201 //to run
    >
    >
    > When I run I am getting a blank screen which does not seem to
    > terminate .. Can someone help me


    Did you provide some input? The program is reading from std::cin and will
    wait for input; e.g., using a pipe, you could provide input as follows:

    echo 14 17 23 12 ¦ ./list0201


    Best,

    Kai-Uwe Bux
    Kai-Uwe Bux, May 21, 2011
    #2
    1. Advertising

  3. lopez

    Ian Collins Guest

    On 05/22/11 09:51 AM, lopez wrote:
    > Hi,
    >
    > I tried running the following program using cygwin.
    > /** @file list0201.cpp */
    > /** Listing 2-1. Reading Test */
    > /// Read the program and determine what the program does.
    >
    > #include<iostream>
    > #include<istream>
    > #include<limits>
    > #include<ostream>
    >
    > int main()
    > {
    > int min(std::numeric_limits<int>::max());
    > int max(std::numeric_limits<int>::min());
    > bool any(false);
    > int x;
    > while (std::cin>> x)
    > {
    > any = true;
    > if (x< min)
    > min = x;
    > if (x> max)
    > max = x;
    > }
    >
    > if (x)
    > std::cout<< "min = "<< min<< "\nmax = "<< max<< '\n';
    > }
    >
    > the command I used is
    >
    > g++ list0201.cpp -o list0201 //to compile
    >
    > and
    >
    > ../list0201 //to run
    >
    >
    > When I run I am getting a blank screen which does not seem to
    > terminate .. Can someone help me


    How do you end your input? You are looping until end of file condition
    on cin, so you need to input an end of file character (^D on a Unix like
    shell).

    --
    Ian Collins
    Ian Collins, May 21, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    470
    Walter Roberson
    Aug 12, 2005
  2. Lorenzo Bettini
    Replies:
    3
    Views:
    619
    Lorenzo Bettini
    Sep 24, 2005
  3. Alexander Mahone

    Running a program (gperf) inside another C program

    Alexander Mahone, May 26, 2008, in forum: C Programming
    Replies:
    4
    Views:
    406
    Flash Gordon
    May 26, 2008
  4. Sagy Drucker

    running a simple ruby program

    Sagy Drucker, Jan 10, 2012, in forum: Ruby
    Replies:
    1
    Views:
    574
    Robert Klemme
    Jan 10, 2012
  5. Caroline Hou
    Replies:
    23
    Views:
    434
    Chris Angelico
    Nov 16, 2012
Loading...

Share This Page