Name of charactor

So I'm making a text based adventure in C++ and I'm doing this all fabulously when suddenly I realize I don't know how to output infomation that the user gave.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "stdafx.h"

int name;

int _tmain(int argc, _TCHAR* argv[])
{
	printf("I hope you're prepared to enter the world lying beneath the kettle lid\n");
	printf("This is a text based adventure game created based on the imagination of young \n");
	printf("Sam Downey. As you walk around in this world you will have to choose your own\n");
	printf("path and if you're lucky you might even get to met Ms Downey yourself, though\n");
	printf("she is a busy programmer and might just shake your hand in the end (but the \n");
	printf("more you impress her the better chance you'll have of meeting Samantha ;D)\n\n");
	printf("So if you are ready to begin the adventure, please enter your name here.");
	scanf("%d", name); //The name of the users charactor
	if ( name )
	{
		printf();
	}
	scanf("pause");
	return 0;
}


If I did it correct then the user's answer should have stored their name into the int name;
So my question is how do I make it so the given name is put in the printf(); Dialogue. Sorry if I don't make since or sound stupid. And tell me if I did something wrong.
First and foremost, unless your user is a computer that only has a number for a name, you're going to get a problem with type casting. Others may point out other problems, but that is going to be a big one. String, or a char[], but not int.
Okay I understood the first point since %d is decimal but you lost me after that
C++ ...

use the string class: #include<string>

Then you can do this:

1
2
3
4
string name;
cin >> name;
cout << "My name is " << name << endl;


I don't know if I got you correctly...
:l I'm wanting to make is so the name they enter is the name of the character. And I don't know what cout and cin and all that is.
You're using C++, they are like scanf() and printf()...

1
2
3
        char name[64];
        scanf("%s", &name);
        printf("My name is %s", &name);


There, how about now?
Ooooh. So if I do that then if at that scanf they enter "Sammy" then that printf will say "My name is Sammy"?
But that that char name[64]; ?
Yes.

Btw, do you know C++ basics? If not, please go for a quick tutorial.

char name[64] is a cstring (character array) of 64 characters. Since you are not familiar with strings and cannot use iostream, you're left with cstrings. And, when declaring a cstring, you need to include the number of characters it can hold.
Last edited on
Okay. So I connect things to that? Where do I put that then and what does it connect to?
I think you're missing quite a lot. Type is the single most fundamental thing in C++, because its raison d'etre is to create user defined types.

It would really help if you worked through the tutorials before starting a project.
I have worked through tutorials.
Do you understand what an int and a string are?
int name;

Well, no offense but the fact that you declared your characters name as an integer means you might want to go through some basic tutorials again. I think you're missing out on a very fundamental concept here. Come up to speed on C++ I/O too. printf and scanf are plain old C commands. C++ has better options. The tutorial on this site is quick and should bring you up to speed: http://cplusplus.com/doc/tutorial/
Topic archived. No new replies allowed.