Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Running a simple c++ program (http://www.velocityreviews.com/forums/t748757-running-a-simple-c-program.html)

lopez 05-21-2011 09:51 PM

Running a simple c++ program
 
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

Kai-Uwe Bux 05-21-2011 10:16 PM

Re: Running a simple c++ program
 
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


Ian Collins 05-21-2011 10:19 PM

Re: Running a simple c++ program
 
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


All times are GMT. The time now is 10:48 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.