By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,889 Members | 1,358 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,889 IT Pros & Developers. It's quick & easy.

Using a variable

P: n/a
Ok I am making a very simple program (I am new to this so I dont know
much, its kinda a trial and error thing)

This is what I am trying to do:

-Get a name from the user (designated as variable 'name')
-Take that name and say this to them
"Well, Helllo 'name' welcome.

Then from there I dont know what I wanna do, but this is what I have
for code so far:

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
//Welcome them.
cout << "Welcome to my first program.\n";
cout << "I hope I can do some preety neet things.\n";

//Get there name and intoduce myself.
int name;
cout << "So we can be more formal please tell me your name:";
cin >name;
system ("PAUSE");
}

Now how would I make a reply.
Let's say that they enter the name Sam, How would I have it say:

Welcome Sam. I am Joe, nice to meet you!

Thanks alot for anyone that responds.

Jul 11 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
I am new to this too but i think this is how it goes:

//Get there name and intoduce myself.
int name;
cout << "So we can be more formal please tell me your name:";
cin >name;
cout <<"Welcome" << name << "I am Joe..." ;
system ("PAUSE");
}
I think thats how it goes, try it, if it doesnt work, sorry and
hopefully someone else can help.

An*********@verizon.net wrote:
Ok I am making a very simple program (I am new to this so I dont know
much, its kinda a trial and error thing)

This is what I am trying to do:

-Get a name from the user (designated as variable 'name')
-Take that name and say this to them
"Well, Helllo 'name' welcome.

Then from there I dont know what I wanna do, but this is what I have
for code so far:

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
//Welcome them.
cout << "Welcome to my first program.\n";
cout << "I hope I can do some preety neet things.\n";

//Get there name and intoduce myself.
int name;
cout << "So we can be more formal please tell me your name:";
cin >name;
system ("PAUSE");
}

Now how would I make a reply.
Let's say that they enter the name Sam, How would I have it say:

Welcome Sam. I am Joe, nice to meet you!

Thanks alot for anyone that responds.
Jul 11 '06 #2

P: n/a
An*********@verizon.net wrote:
Ok I am making a very simple program (I am new to this so I dont know
much, its kinda a trial and error thing)

This is what I am trying to do:

-Get a name from the user (designated as variable 'name')
-Take that name and say this to them
"Well, Helllo 'name' welcome.

Then from there I dont know what I wanna do, but this is what I have
for code so far:

#include <cstdio>
#include <cstdlib>
#include <iostream>
Add:

#include <string// for reasons to become clear later
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
//Welcome them.
cout << "Welcome to my first program.\n";
cout << "I hope I can do some preety neet things.\n";

//Get there name and intoduce myself.
int name;
Are you sure, the users name is just a number? What about:

string name;

Since the data type string is defined in <string>, you have to include that
header above.
cout << "So we can be more formal please tell me your name:";
cin >name;
Ok. Here, you read the name. Now, to greet the user:

cout << "Welcome " << name << ". I am Joe, nice to meet you!\n"
system ("PAUSE");
}
Best

Kai-Uwe Bux
Jul 11 '06 #3

P: n/a
<An*********@verizon.netwrote:
Ok I am making a very simple program (I am new to this so I dont know
much, its kinda a trial and error thing)

This is what I am trying to do:

-Get a name from the user (designated as variable 'name')
-Take that name and say this to them
"Well, Helllo 'name' welcome.

Then from there I dont know what I wanna do, but this is what I have
for code so far:

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
//Welcome them.
cout << "Welcome to my first program.\n";
cout << "I hope I can do some preety neet things.\n";

//Get there name and intoduce myself.
int name;
cout << "So we can be more formal please tell me your name:";
cin >name;
system ("PAUSE");
}
Ok, firstly, if you're not actually going to use anything from the
old C-style IO library, get rid of the "#include<cstdio>". And if
you DO want to use both the new-style and old-style IO systems
together in the same program, always put this line of code as the
first line of your main():

std::ios_base::sync_with_stdio();

That synchronizes the two systems so they work well together.

Secondly, never write "using namespace std;". It pollutes the global
namespace with thousands of names you'll never use, and serves only
to cause name conflicts. Instead, tack "std::" on the front of any
name from the standard library, or use limited "using declarations"
for things you use a lot, like so:

using std::cin;
using std::cout;
using std::cerr;
using std::endl;

Those are the only 4 I ever use "using" declarations with, myself.
The reason being, I want to see at a glance what namespace everything
is in. If it doesn't have a namespace identifier on it, I assume its
either global or local to the namespace I'm working in. And I use
very few globals. When I do use globals, I usually preface them with
"::".

Thirdly, you can't store textual data in an integer variable; use
std::string instead.

Fourthly, "cin >>" loads text into string variables by word (delimited
by space), so if you really want to allow any name, such as
"Robert Sampson Kelezvenue-Pickering III", then you'll have to grab
a whole line of text. So use getline().

Something like this should work for you:

#include <iostream>

using std::cin;
using std::cout;
using std::endl;
int main(void)
{
//Welcome them.
cout << "Welcome to my first program." << endl;
cout << "I hope I can do some preety neet things." << endl;

//Get there name and intoduce myself.
std::string name;
cout << "So we can be more formal please tell me your name:" << endl;
getline(cin, name);

//Greet them by name:
cout << "Why, hello there, " << name << "! How you doing???" << endl;
system ("PAUSE");
return 0;
}

Yes, I compiled and ran that, and it works.

(Also, see my post from a few minutes ago on how to get rid of that
ugly, unnecessary "system("PAUSE")" command.)

--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
Jul 11 '06 #4

P: n/a
Oops, an addendum to my post from a few minutes ago:
The sample program I gave should also #include <string>
like so:

#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
int main(void)
{
//Welcome them.
cout << "Welcome to my first program." << endl;
cout << "I hope I can do some preety neet things." << endl;

//Get there name and intoduce myself.
std::string name;
cout << "So we can be more formal please tell me your name:" << endl;
getline(cin, name);

//Greet them by name:
cout << "Why, hello there, " << name << "! How you doing???" << endl;
system ("PAUSE");
return 0;
}

--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
Jul 11 '06 #5

P: n/a

"Robbie Hatley" <bo***********@no.spamwrote in message
news:mc********************@newssvr21.news.prodigy .com...
Oops, an addendum to my post from a few minutes ago:
The sample program I gave should also #include <string>
like so:

#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
int main(void)
{
//Welcome them.
cout << "Welcome to my first program." << endl;
cout << "I hope I can do some preety neet things." << endl;

//Get there name and intoduce myself.
std::string name;
cout << "So we can be more formal please tell me your name:" << endl;
getline(cin, name);

//Greet them by name:
cout << "Why, hello there, " << name << "! How you doing???" << endl;
system ("PAUSE");
return 0;
}
Nice of you to do his entire homework for him. :-)

-Howard
Jul 11 '06 #6

P: n/a
"Howard" <al*****@hotmail.comwrote:
Nice of you to do his entire homework for him. :-)
Well, he did most of the work himself. I just corrected
some errors.

If I see:
Hi, i'm having trouble with this program I'm writing:
(a dozen lines of code, showing hard work)
I'm a lot more likely to help than with these dorks who come
here saying,
I need an operating system written in C++. Can you write
one for me (for free)?
My natural response to THAT is to want to write:
[insert a whole bunch of expletives here]
But I don't actually like to cuss on Usenet, because these posts
are archived for decades on Google. So I just make a wry, mildly
sarcastic comment, and let it go at that.
--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
Jul 11 '06 #7

P: n/a
Robbie Hatley wrote:
>
Ok, firstly, if you're not actually going to use anything from the
old C-style IO library, get rid of the "#include<cstdio>". And if
you DO want to use both the new-style and old-style IO systems
together in the same program, always put this line of code as the
first line of your main():

std::ios_base::sync_with_stdio();

That synchronizes the two systems so they work well together.
They are synchronized by default. You only call this function
if you want to de-synch them (probably for performance reasons).

Jul 12 '06 #8

P: n/a
<An*********@verizon.netwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
This is what I am trying to do:

-Get a name from the user (designated as variable 'name')
-Take that name and say this to them
"Well, Helllo 'name' welcome.
If I may make a slightly self-serving remark, Accelerated C++ solves pretty
much this same problem starting on page 9.
Jul 13 '06 #9

P: n/a
Andrew Koenig wrote:
<An*********@verizon.netwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
This is what I am trying to do:

-Get a name from the user (designated as variable 'name')
-Take that name and say this to them
"Well, Helllo 'name' welcome.

If I may make a slightly self-serving remark, Accelerated C++ solves pretty
much this same problem starting on page 9.
If I may make a less shameless plug, that's a great (perhaps the best)
book for C++ beginners. Buy it; read it; absorb the wisdom therein.

Cheers! --M

Jul 13 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.