Using a variable

A

Animatorboy

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.
 
P

poolshark1691

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.
 
K

Kai-Uwe Bux

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 said:
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;

you have to include that said:
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
 
R

Robbie Hatley

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/
 
R

Robbie Hatley

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/
 
H

Howard

Robbie Hatley said:
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
 
R

Robbie Hatley

Howard said:
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/
 
O

Old Wolf

Robbie said:
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).
 
A

Andrew Koenig

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.
 
M

mlimber

Andrew said:
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
 

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
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top