simple addition program, no output

Z

Zack Wahab

Hi,

I use Dev C++ compiler. Tried this program :

// Fig. 1.6: fig01_06.cpp
// Addition program.
#include <iostream>

// function main begins program execution
int main()
{
int integer1; // first number to be input by user
int integer2; // second number to be input by user
int sum; // variable in which sum will be stored

std::cout << "Enter first integer\n"; // prompt
std::cin >> integer1; // read an integer

std::cout << "Enter second integer\n"; // prompt
std::cin >> integer2; // read an integer

sum = integer1 + integer2; // assign result to sum

std::cout << "Sum is " << sum << std::endl; // print sum
// indicate that program ended successfully
} // end function

The window prompt to enter the integers.However, the result was not
displayed. Instead, immediately it comes back to the compiler program
(The command Prompt window immediately turn off). Is it something to
do with the compiler? I tried to find the button for enabling output
result but couldn't find one..
 
V

Victor Bazarov

Zack Wahab said:
I use Dev C++ compiler. Tried this program :

// Fig. 1.6: fig01_06.cpp
// Addition program.
[...]
The window prompt to enter the integers.However, the result was not
displayed. Instead, immediately it comes back to the compiler program
(The command Prompt window immediately turn off). Is it something to
do with the compiler? I tried to find the button for enabling output
result but couldn't find one..

Get out of the IDE and run your program from the command line.
 
A

Arijit

Hi,

I use Dev C++ compiler. Tried this program :

// Fig. 1.6: fig01_06.cpp
// Addition program.
#include <iostream>

// function main begins program execution
int main()
{
int integer1; // first number to be input by user
int integer2; // second number to be input by user
int sum; // variable in which sum will be stored

std::cout << "Enter first integer\n"; // prompt
std::cin >> integer1; // read an integer

std::cout << "Enter second integer\n"; // prompt
std::cin >> integer2; // read an integer

sum = integer1 + integer2; // assign result to sum

std::cout << "Sum is " << sum << std::endl; // print sum
// indicate that program ended successfully
} // end function

The window prompt to enter the integers.However, the result was not
displayed. Instead, immediately it comes back to the compiler program
(The command Prompt window immediately turn off). Is it something to
do with the compiler? I tried to find the button for enabling output
result but couldn't find one..


At the very end of the program write


int main()
{
........
........ //your program

int dummy;
cin >> dummy;

return 0; // optional
}

This will keep the command prompt open until you enter a dummy
value at the end.

-Arijit
 
H

Howard

Arijit said:
(e-mail address removed) (Zack Wahab) wrote in message


At the very end of the program write


int main()
{
.......
....... //your program

int dummy;
cin >> dummy;

return 0; // optional
}

This will keep the command prompt open until you enter a dummy
value at the end.

It might be better to stream into a string than an int. Using an integer,
if the user fails to enter a valid integer, the program will not be happy.

-Howard
 
R

Richard Herring

In message said:
[...]
At the very end of the program write


int main()
{
.......
....... //your program

int dummy;
cin >> dummy;

return 0; // optional
}

This will keep the command prompt open until you enter a dummy
value at the end.

It might be better to stream into a string than an int. Using an integer,
if the user fails to enter a valid integer, the program will not be happy.

.... indeed, it will be so unhappy that it will set the failbit on
std::cin. That may be a price the OP is prepared to pay.

Whereas, reading a string, it has the potential to run out of memory.
 
A

Arijit

Richard Herring said:
In message said:
[...]
At the very end of the program write


int main()
{
.......
....... //your program

int dummy;
cin >> dummy;

return 0; // optional
}

This will keep the command prompt open until you enter a dummy
value at the end.

It might be better to stream into a string than an int. Using an integer,
if the user fails to enter a valid integer, the program will not be happy.

But if OP is not using a string in his program, he must #include
... indeed, it will be so unhappy that it will set the failbit on
std::cin. That may be a price the OP is prepared to pay.
Its the last line of the program. Setting failbit shouldn't matter,
unless in some destructor cin is used (very unlikely taking input in
destructor).
Whereas, reading a string, it has the potential to run out of memory.

_Extremely_ unlikely. Whats he going to type at the prompt, an essay
on how to terminate a program, that too without spaces ?

-Arijit
 
R

Richard Herring

Arijit said:
Richard Herring said:
In message said:
[...]


At the very end of the program write


int main()
{
.......
....... //your program

int dummy;
cin >> dummy;

return 0; // optional
}

This will keep the command prompt open until you enter a dummy
value at the end.


It might be better to stream into a string than an int. Using an integer,
if the user fails to enter a valid integer, the program will not be happy.

But if OP is not using a string in his program, he must #include
... indeed, it will be so unhappy that it will set the failbit on
std::cin. That may be a price the OP is prepared to pay.
Its the last line of the program. Setting failbit shouldn't matter,
unless in some destructor cin is used (very unlikely taking input in
destructor).
Exactly.
Whereas, reading a string, it has the potential to run out of memory.

_Extremely_ unlikely. Whats he going to type at the prompt, an essay
on how to terminate a program, that too without spaces ?
ISTR some versions of Unix included a program called 'yes' which output
an infinite string of 'Y's. You could pipe its output to stupid programs
which insisted on user confirmation of every action.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,477
Latest member
ColumbusMa

Latest Threads

Top