What is wrong with my code - the program keeps closing and how do I give a integer the value of a string?

I just started to learn c++ and went through the first chapter of this book
I am trying to do this programming exercise "Write a C++ program that displays your name and address)
This is what I have got so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  #include <stdafx.h>
#include <iostream>
int main()
{
	using namespace std;
	cout << "What is your address?" << endl;
	int address;
	cin >> address;
	cout << "Your address is" << address << endl;
	cout << "What is your name" << endl;
	int name;
	cin >> name;
	cout << "Your name is" << name << endl;
	return 0;
}

I know that I could also attempt this activity in a different way which is
1
2
3
4
5
6
7
8
9
10
11
#include <stdafx.h>
#include <iostream>
int main()
{
	using namespace std;
	cout << "What is your address?" << endl;
	int address;
address = "Fake address";
cout << address;
return 0;
}

But I do not want to use pre-definied variables
How can I achieve this?
So basically I am asking how can I input a string into the command prompt and make the string the value of a variable?
http://www.cplusplus.com/doc/tutorial/variables/ you might want to take another look at this
Thank you, I have changed my code to
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdafx.h>
#include <iostream>
#include <string>
int main()
{
	using namespace std;
	cout << "What is your address?" << endl;
	string address;
	cin >> address;
	cout << "Your address is" << address << endl;
	cout << "What is your name" << endl;
	string name;
	cin >> name;
	cout << "Your name is" << name << endl;
	return 0;
}

How do I make the program pause?
firstly, before i tell you how to do that, some critiques

a) dont use stdafx. apparently it does have some uses but ive really only seen it needed when using sfml with visual studios.

b) dont put using namespace std in a function

c) dont use using namespace std. instead give it an explicit qualifier. ie
std::string adress;
std::cin >> adress;

to answer your question: system("pause") is the easiest since it appears you are on windows, however you probably shouldnt use it
Thanks I have fixed it up now - Is this okay? also if I do not use stdafx then an error comes up
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdafx.h>
#include <iostream>
#include <string>
int main()
{
	std::cout << "What is your address?" << std::endl;
	std::string address;
	std::cin >> address;
	std::cout << "Your address is" << address << std::endl;
	std::cout << "What is your name" << std::endl;
	std::string name;
	std::cin >> name;
	std::cout << "Your name is" << name << std::endl;
	system("pause");
	return 0;
}

Also why shouldnt I use system("pause") - what are the alternatives?
Thanks for all your help I really appreciate it!
Last edited on
are you using visual studios? there should be an option to turn it off in settings. that all looks ok. why you shouldnt use system: http://www.cplusplus.com/forum/articles/11153/
to be honest, there is no easy way of reliably clearing the screen. i had to borrow some low level c code for linux and a winapi function, and then just use #defines to make it one function (ie pause). did something very similar for clear(). the most common, correct, way ive seen is: http://www.cplusplus.com/forum/general/18/#msg1571
instead of system("pause") maybe try using getchar().
firstly, before i tell you how to do that, some critiques

Some critique on your critiques.


a) dont use stdafx. apparently it does have some uses but ive really only seen it needed when using sfml with visual studios.

If you don't understand what a thing is used for, don't give advice about its use. This is the mechanism by which VS implements pre-compiled headers. It certainly doesn't have anything to do with SFML. If you wish to remove that header, you also need to turn off pre-compiled headers in your project's properties.


b) dont put using namespace std in a function

It's much better to have it in a function where scope is limited than it is outside of a function.


to answer your question: system("pause") is the easiest since it appears you are on windows, however you probably shouldnt use it

I don't know what the operating system has to do with it. This is a toolchain issue and should (preferably) be resolved by configuring your toolchain correctly and not in the code. If one is using Visual Studio as an IDE, for instance, you would go to your project properties then, Linker -> System -> SubSystem which should be set to "Console". Most IDEs have some kind of setting that governs whether the window stays open when the program is run from the IDE. This is the (not very intuitive) setting for VS.

Note that you are expected to control the flow of the program via breakpoints and the like when you "Start Debugging" (F5 by default), so in that case the window will always close when the program is done executing if you do nothing to stop it. The setting above is reflected when you "Start Without Debugging" (ctrl+F5 by default).
system("pause") is a dependent function which uses commands of the operating system you are using. If you are not using Windows then this system("pause") may not work as there may be no "pause" command for console.

Try using:

cin.ignore()
Topic archived. No new replies allowed.