# Simple Help

This topic is 3054 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

##### Share on other sites
You've defined studentName as a single character. Once that single character has been input, the rest of the input is ignored.

Try:

char studentName[64];

Note: when inputting strings using cin, the first space is considered the end of the string.

Try something like "First Name:", "Last Name:" to reduce the chance of getting a space in the input string.

##### Share on other sites
This is a quite a common gotcha when starting C++ that can be quite frustrating. std::cin only asks for input from the user when its buffer is empty. For example, since char only holds a single character, inputting "Bob Saget" for studentName extracts the 'B' but leaves "ob Saget" behind. When you attempt to extract a number, studentSocial, from std::cin, std::cin will set itself to a failure state, and will refuse to process any more data until the state has been cleared (and optionally, the input buffer has been cleared).

##### Share on other sites
Wow, I knew it was going to be something really simply I overlooked, I didn't think of that at all. Thanks a lot to the both of you. I will probably be back here a lot over the course of this semester, this book is alright but I think it tries to cover to much at once.

##### Share on other sites
Quote:
 Original post by BuckeyeTry:char studentName[64];

Or you do yourself a favour and just use std::string without all the wild guessing about how large a name should be or how long a string somebody might enter.

Quote:
 Try something like "First Name:", "Last Name:" to reduce the chance of getting a space in the input string.

What if he NEEDS the full name including spaces?

Hint: there are tons of ways to read, read lines, strings, etc. and cin.getline is not the only getline in the library. Contrary to what most references tell you there is also not just a getline taking a char* as destination, but the required overload is most likely declared in the string header.

Reading the entire input and not stumbling over spaces is still just a single line.

##### Share on other sites
This has always worked for me:

#include <iostream>#include <string>int main(){    std::cout << "What is your name? ";    std::string name;    getline(std::cin, name);    std::cout << "Hello, " << name << "!\n";}

##### Share on other sites
I'm not sure if you're allowed to use std::string in your class, but std::string is always much better than char* to represent text. Use "#include <string>" to be able to use it.

A "char" is a single character.
A "char*" is a pointer to a character or a pointer to the first of a whole array of characters, but you need to manually allocate that memory first (typically some fixed size).
A "char[64]" is an array which can contain only up to 64 characters - so longer names will result in a crash
A "std::string" is an automatically memory managed series of characters that can have any size, grow and shrink, without ever needing to worry.

If you would be using C instead of C++, then unfortunately you're stuck with using char* for texts, but since you're using std::cin and std::cout you're clearly using C++ and std::string is the better option.

##### Share on other sites
For this current project char[64] worked fine, but I changed the program around using string and I found I liked that a lot better. Thanks for all the suggestions these will help a lot in the future.

EDIT: Going back I noticed you all used std::cout instead of using namespace std;
Did you just do that to show me in case I didn't know that or do you use std:: cout/cin in higher level programming because of memory cycles?

[Edited by - Narusuke on September 14, 2009 1:04:16 AM]

##### Share on other sites
Moved to For Beginners.

##### Share on other sites
Quote:
 Original post by NarusukeI noticed you all used std::cout instead of using namespace std;Did you just do that to show me in case I didn't know that or do you use std:: cout/cin in higher level programming because of memory cycles?

Neither. Prefixing standard names with std:: makes clear you are using a name from the standard library and prevents names clashes. using namespace std; does the exact opposite of what namespaces were invented for -- it effectively moves all names into the top-level namespace or whatever it's called in C++. I don't see why anyone would ever want to use using namespace std;, except for making code shorter so that more code fits on a page in a book or something.

##### Share on other sites
Quote:
 Original post by DevFredNeither. Prefixing standard names with std:: makes clear you are using a name from the standard library
One purpose is to save you from some typing, otherwise we could have stayed with C-style names (see OpenGL for instance). There is actually not much difference between writing glBegin() and gl::begin().

Though if you are a vendor of code, then namespaces can indeed make your life a bit easier, like this:

namespace foo {void x () {}void y () {}void z () {}}

void foo_x() {}void foo_y() {}void foo_z() {}

On the gripping hand, if you are not the writer but just a poor maintainer, then the foo_...-convention might actually be easier to read as for the context freenes, or you spread foo::... to make it easier to read again ... Hmm.

Quote:
 prevents names clashes.

For that, namespaces do not work better than C-style naming conventions (btw, I hate when vendors publish classes like MySql::MySqlConnection, that's somehow redundant and then, why put it in a namespace at all?). Neither namespaces nor naming conventions, to say nothing of meaningful names, are enforced, nor is it forbidden to inject into stranger namespaces (sometimes it is even encouraged).

Namespaces also have other uses aside from the obvious but in the end non-existant automatic name clash prevention.

Quote:
 using namespace std; does the exact opposite of what namespaces were invented for -- it effectively moves all names into the top-level namespace or whatever it's called in C++. I don't see why anyone would ever want to use using namespace std;, except for making code shorter so that more code fits on a page in a book or something.
Note that by using using you do not import all names from another namespace into the top-level namespace, but into the namespace or scope where you use it.

int main () {    {        using namespace std;        cout << "hello" << endl; // okay    }    cout << "fubar"; // nope, out of above scope    {        using std::cout;        cout << "hello" << std::endl; // okay    }    {        using std::cout;        cout << "hello" << endl; // fail, only cout was imported into this scope    }}

Anyways, I agree that "using namespace" shall never be written in a global context.

[Edited by - phresnel on September 14, 2009 5:38:03 AM]