Variable/Char/ w/e help..

Started by
28 comments, last by sssummer 16 years, 1 month ago
!!Newbie Alert!! Hey, I've got an extremely simple code where the system prompts you for your name and you enter it and it says, "Hello, "name". But It only says the first letter of the name :\ Heres my code:
#include "stdafx.h"

#include <iostream>

using namespace std;

int main()

{
	char name;

	cout << "Please enter your name. \n";
	cin >> name;
	cout << "Hello.." << name << "\n";

system("PAUSE");

}
Advertisement
char stores a character (i.e. a single letter, punctuation mark, digit or one of a few other things). You want to store a sequence of characters – this sequence is called a string and is represented using the std::string type, rather than char.

Where you have char name, you want string name. To use std::string, you must put #include <string> at the top of your code (next to where you've got #include <iostream>).

(Incidentally, I'm calling it both string and std::string above – std::string is its fully qualified name, but you've got using namespace std; at the top of your code, which means that the compiler knows you mean std::string when you say string; this std:: bit can be used to differentiate between different types with the same name – the standard library has std::string to represent text, but you might have a string type of your own that refers to the piece of twine, and the compiler can't tell them apart unless you use their fully-qualified names)
[TheUnbeliever]
Thanks! Works great! :D

Question 2: I have changed the code so it is now a survey and I want it so if they put in one response, the system sends one message, but when the input the second, it feeds out a different message, is this possible?
	if(gender == "male")	{		cout << "Disappointing." << endl;	}	else if(gender == "female")	{		cout << "How you doin'? ;)" << endl;	}	else	{		cout << "Bit of both, eh?" << endl;	}


Something like that?

If that is what you're looking for, be aware that it's case sensitive.

Really, you should get into the habit of using buffers instead of the "string" type. e.g:

char name[32];


When working with those, though, you need to watch out for the potential for buffer overflows... but that may be too much to worry about if you're just starting.
Quote:Original post by gemin ta
Really, you should get into the habit of using buffers instead of the "string" type. e.g:

char name[32];


When working with those, though, you need to watch out for the potential for buffer overflows... but that may be too much to worry about if you're just starting.


Why would you *voluntarily choose* to use raw char arrays over std::string?
Quote:Original post by gemin ta
Really, you should get into the habit of using buffers instead of the "string" type. e.g:

char name[32];


When working with those, though, you need to watch out for the potential for buffer overflows... but that may be too much to worry about if you're just starting.


What!?!? Why on earth would you suggest something like that? Using character arrays as strings is an unfortunate throwback to C that should NEVER be used in C++. They are less intuitive, prone to security errors, and lead into the whole arrays/pointers syntax nonsense.

To the OP: Stick with std::string. It's a core part of the C++ language and using it correctly will save you hours of debugging and frustration.

Quote:Really, you should get into the habit of using buffers instead of the "string" type. e.g:

char name[32];
Why do you say that? (Just curious...)
Using char arrays to start makes sense from a pedagogical point of view. It reminds you that C++ is first and foremost a highly developed low level language (where you have to manage your memory manually, and watch out for buffer overflows). Attempting to use/teach C++ like a "true" high level language (HLL), like Python for example, you fail to recognize its true nature.
Hit a nerve with that one. ;) Ouch!

I suppose I've just been conditioned by years of ASM programming to use the closest to the 'base level'. There're no real problems with going "raw" if you... I dunno... know what you're doing? Why put up with the overheads otherwise?

I guess (as with many things) the choice depends on your needs and capabilities. I'm just one of those people who likes to promote taking the extra time to handle something on a more "personal" level... even at the cost of time or the expense of possible risk. To put it quite bluntly, I think that while using shortcuts when programming may be efficient if development time is in question, they can also promote laziness and give people warped ideas of how things actually work. I've met many C++ programmers who are heavily into OOP, etc. who actually believe that they're writing 'C++ scripts' for the processor to read and parse.

I can sense a debate already here (usually fuelled by people who argue for the sake of it and/or want to compare the size of their you-know-what) so... I won't be replying again. ;P I've got my point across.
Quote:I suppose I've just been conditioned by years of ASM programming to use the closest to the 'base level'. There're no real problems with going "raw" if you... I dunno... know what you're doing? Why put up with the overheads otherwise?


No, there's no problems if you know "why" you're doing it.

Quote:Attempting to use/teach C++ like a "true" high level language (HLL), like Python for example, you fail to recognize its true nature


Depends. Perhaps it's your perspective that C++ *must* be low-level. But it really doesn't need to be.

Unfortunately, majority of materials available is C with classes, the rest is C++ with C.

But unless the course is taught as part of CS curriculum, there's no reason whatsoever to even mention any C concepts. Standard library provides decent utility (incredibly minimal compared to other languages), but for teaching algorithms and data structures, the basic application concepts (non-GUI), console IO, and various utility programming, there's is exactly zero need to even mention any kind of C legacy.

If the course is intended for industry application (beyond beginner level), then legacy will creep up due to libraries, not C++ itself.

This is easily tested. Implement any algorithm purely with C++ idioms and classes. If you add some form of smart pointers, you can almost copy-paste Java or C# code.

To teach programming, C doesn't need to be mentioned. To teach C++, it needs to be.

Again: "why" is more important than "how". Why is someone learning C++. And as always, one must learn to crawl before one can walk. Pure C++ is hence a better choice, than C/C++ mess.

This topic is closed to new replies.

Advertisement