C++ Program Execution

A

Alexander.David.R

Hello,

I have been getting to know C and C++ alot lately and have come up with
the same problem with each program that I write. When I compile the
program, it comes out with no errors. I can solve those no problem
(usually just a misplaced semicolon) but when it comes to executing the
program to verify my program works correctly, after I input one or
sometimes two variables it closes the window that it is running in. I
have tried using both Visual Studio 2003 and Dev-C++ to compile and run
the programs and it does the same thing in each. Anyone have an ideas
as to why this is happening? I will include code for one that I am just
starting off writing in this post to help you see what is writen.
Thanks in advance for all help recieved.

David

// Code Start:
#include <iostream>

int main()
{
char name;
int age = 0;

std::cout << "Enter your name: ";
std::cin >> name;
std::cout << "Enter your age: ";
std::cin >> age;
std::cout << "\nName: ";
std::cout << name;
std::cout << "\nAge: ";
std::cout << age;
}
// End Code
 
O

osmium

I have been getting to know C and C++ alot lately and have come up with
the same problem with each program that I write. When I compile the
program, it comes out with no errors. I can solve those no problem
(usually just a misplaced semicolon) but when it comes to executing the
program to verify my program works correctly, after I input one or
sometimes two variables it closes the window that it is running in. I
have tried using both Visual Studio 2003 and Dev-C++ to compile and run
the programs and it does the same thing in each. Anyone have an ideas
as to why this is happening? I will include code for one that I am just
starting off writing in this post to help you see what is writen.
Thanks in advance for all help recieved.

David

// Code Start:
#include <iostream>

int main()
{
char name;
int age = 0;

std::cout << "Enter your name: ";
std::cin >> name;
std::cout << "Enter your age: ";
std::cin >> age;
std::cout << "\nName: ";
std::cout << name;
std::cout << "\nAge: ";
std::cout << age;
}
// End Code

Try adding this just before the closing brace in main.

cin.get();
 
J

Jay Nabonne

Hello,

I have been getting to know C and C++ alot lately and have come up with
the same problem with each program that I write. When I compile the
program, it comes out with no errors. I can solve those no problem
(usually just a misplaced semicolon) but when it comes to executing the
program to verify my program works correctly, after I input one or
sometimes two variables it closes the window that it is running in. I
have tried using both Visual Studio 2003 and Dev-C++ to compile and run
the programs and it does the same thing in each. Anyone have an ideas
as to why this is happening? I will include code for one that I am just
starting off writing in this post to help you see what is writen.
Thanks in advance for all help recieved.

If you're debugging it: set a breakpoint on the close brace of main.
If you're not debugging it: run from a separate command window.

- Jay
 
R

red floyd

Hello,

I have been getting to know C and C++ alot lately and have come up with
the same problem with each program that I write. When I compile the
program, it comes out with no errors. I can solve those no problem
(usually just a misplaced semicolon) but when it comes to executing the
program to verify my program works correctly, after I input one or
sometimes two variables it closes the window that it is running in. I
have tried using both Visual Studio 2003 and Dev-C++ to compile and run
the programs and it does the same thing in each. Anyone have an ideas
as to why this is happening? I will include code for one that I am just
starting off writing in this post to help you see what is writen.
Thanks in advance for all help recieved.

David

// Code Start:
#include <iostream>

int main()
{
char name;
int age = 0;

std::cout << "Enter your name: ";
std::cin >> name;
std::cout << "Enter your age: ";
std::cin >> age;
std::cout << "\nName: ";
std::cout << name;
std::cout << "\nAge: ";
std::cout << age;
std::cout << std::endl;
cin.get();
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top