473,833 Members | 2,144 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using a variable

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
9 1637
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*********@ver izon.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
An*********@ver izon.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
<An*********@ve rizon.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<cstdi o>". 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
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

"Robbie Hatley" <bo***********@ no.spamwrote in message
news:mc******** ************@ne wssvr21.news.pr odigy.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
"Howard" <al*****@hotmai l.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
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<cstdi o>". 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
<An*********@ve rizon.netwrote in message
news:11******** **************@ m79g2000cwm.goo glegroups.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
Andrew Koenig wrote:
<An*********@ve rizon.netwrote in message
news:11******** **************@ m79g2000cwm.goo glegroups.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
4315
by: Sergio Otoya | last post by:
Hi all, I need to add an input hidden field to an existing form (post). I have tried a couple things like adding the '<INPUT type=hidden name=idSelectedURL value=http://server/documents>' to the innerHTML of the form but it fails. ie
1
1437
by: eric | last post by:
I am using an xml file as a relational data store like the following (original data is much larger): .... <variable> <id>A</id> <name>VarA</name> <description></decription> </variable> <variable>
17
4237
by: Danny J. Lesandrini | last post by:
The following code works with a standard MDB to navigate to a particluar record (with a DAO recordset, of course) but it's giving me problems in an ADP I'm working on. Dim rs As ADODB.Recordset Set rs = Me.RecordsetClone rs.Find "=" & lngContractID If Not rs.EOF Then Me.Bookmark = rs.Bookmark I must site the Heisenberb Uncertainty Principal here, as it
8
2141
by: Michael | last post by:
Hi, I think my problem deals with class casting and inheritance. I want to deal with various Audio Formats, reading into memory for modification, working with it (done by different classes), and writing the result to disk afterwards. Therefore I have created some classes, e.g. WaveFileIO and AiffFileIO and MP3FileIO and AuFileIO for the In/Out operations.
2
3064
by: Jake Barnes | last post by:
Using javascript closures to create singletons to ensure the survival of a reference to an HTML block when removeChild() may remove the last reference to the block and thus destory the block is what I'm hoping to achieve. I've never before had to use Javascript closures, but now I do, so I'm making an effort to understand them. I've been giving this essay a re-read: http://jibbering.com/faq/faq_notes/closures.html
13
2141
by: Andy Baxter | last post by:
Can anyone recommend a good online guide to using objects in javascript? The book I bought (DHTML Utopia) suggests using objects to keep the code clean and stop namespace clashes between different parts of the code, but as far as I can see, the way objects work in javascript is quite awkward. I had it working the way they suggest in the book, and it was going OK until I wanted to call one object method from another - I couldn't find a...
7
4719
by: garyusenet | last post by:
This is the first time i've worked with openfile dialog. I'm getting a couple of errors with my very basic code. Can someone point out the errors in what i've done please. ========================================== using System; using System.Collections.Generic; using System.ComponentModel; using System.Data;
19
2403
by: Mitesh | last post by:
Hi all, I have the following code: $req = "SELECT * FROM table1"; $res = mysql_query($req); if(!$res) return;
11
1945
by: dgk | last post by:
If I have a class with a public variable, isn't this the same as a private variable with a property? ie, isn't this: Public Class MyTest Public MyVar as String End Class the same as this: Public Class MyTest
13
12807
by: jkimbler | last post by:
As part of our QA of hardware and firmware for the company I work for, we need to automate some testing of devices and firmware. Since not everybody here knows C#, I'm looking to create a new scripting language that makes writing automated tests simpler. Really, I'm looking to kind of abstract the power of the C# language into a simpler language that's easier to learn. The script files would be interpreted by a script interpreter...
0
9796
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9642
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10782
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10500
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10543
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9323
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7753
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
3078
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.