# C++: Two cin.getline's in a row.



## mussavcom (Mar 29, 2004)

This: 

```
#include <iostream> 
using namespace std; 

#include <stdlib.h> 

int main() 
{ 
        cout << "What is your name?\n"; 
        char student[20]; 
        cin.getline (student,20); 
        cout << "Hello, " << student << endl; 
        cout << "What school do you go to?\n"; 
        char school[100]; 
        cin.getline(school,100); 
        cout << "You go to " << school << "?!\n"; 
        system("pause"); 
        return 0; 
}
```
does not work properly.

When the name entered to student is 20/less characters, it works fine (it asks the school, inputs school, and displays it OK): 

```
What is your name? 
Michael G. 
Hello, Michael G. 
What school do you go to? 
Moody Middle 
You go to Moody Middle?! 
Press any key to continue . . .
```
But if it's larger than 20 characters: 

```
What is your name? 
John Jacob Jingle Hymer Schmidt 
Hello, John Jacob Jingle H 
What school do you go to? 
You go to ?! 
Press any key to continue . . .
```
it doesn't work. (It displays "You go to ?!" right after displaying What school do you go to?)

What should I do?


----------



## Regicide (Aug 8, 2003)

Basicly, the varible 'student' can only hold 20 characters. Once you try to put more into it, the extra characters are not being put into the 'student' string. Where are they going? Well they aren't going into the next varible, 'school, because the user didn't input those characters into the dos window. That might not make sense, but cin.getline() reads until there is a return, a '\n'. The first cin.getline() reads all 20 characters that it can, then stops. The extra info then goes to the 'school' varible, but sense there is not a new '\n' character, it doesn't read anything.

A rule of thumb is to make a character arrays big enough to hold all the user's info. If you try to put too much "stuff" into a varible, it spills over into the buffer, or the next varible. Once that happens, don't expect to get the extra "stuff back". My suggestion is make both varibles have 100 in the array. Hope this helps.


----------



## mussavcom (Mar 29, 2004)

Thanks for clearing this up.

Is there a disadvantage to making the char a huge size, like 1000 or something? I'll probably just make it 100, I'm just wondering, for future reference, if it would take up a lot of memory.

edit: Oh, and also-- is there a way not to specify a size for the char[], to just make it the size of the string the user inputs?


----------



## Regicide (Aug 8, 2003)

There is a small disadvantage to make HUGE arrays. See, each character, whether it be in an array or by it self, takes up memory. Off the top of my head, I think each character takes up 8 bits, don't quote me on that. Which really, isn't that much.



> Oh, and also-- is there a way not to specify a size for the char[], to just make it the size of the string the user inputs


Well, if you are using Visual C++, there is a String class that is pretty good. Basicly, it is good for user input because the coder never actually defines the limits to the array of the string. You can find many tutorials online about how to use it. If you are not using VC++, I've got the code for a string class I made myself. I could upload that to you.


----------



## Shadow2531 (Apr 30, 2001)

```
#include <iostream> 
#include <string>
#include <cstdlib>

using namespace std; 

int main() { 
    system("cls");
    cout << "What is your name? "; 
    string student = " "; 
    getline(cin,student); 
    cout << endl << "Hello, " << student << ".  What school do you go to? "; 
    string school = " "; 
    getline(cin,school); 
    cout << endl << "You go to " << school << "?" << endl << endl; 
    system("pause"); 
}
```
return is not needed for main in c++.

If you want, you can just define the student and school strings without an = " "; by:

string student;
string school;

(they will get initialized by getline anyways, but I chose to initialize them right away.)

Also, the system() function resides in cstdlib, but depending on your compiler, you might not need to include it to compile, but you should include it.

It's also suggested to not use tabs for indentation. Use 4 spaces instead or set your editor to insert 4 spaces when tab is pressed or automatic indention is invoked.

Since you are using the namespace std, you should use cstdlib instead of stdlib.h.


----------



## mussavcom (Mar 29, 2004)

I learned that you should always include "return 0" in main to return that the program ended properly or something, but I don't see why I should. *shrug*

Thanks for the help with the #include <cstdlib> for the system function, I just found what I had on the 'net somewhere.


----------



## Regicide (Aug 8, 2003)

> return is not needed for main in c++


No offense Shadow, but it is needed. This way, the OS that you are running the program on knows its the end and can stop allocating memory for the program.


----------



## Shadow2531 (Apr 30, 2001)

no offense taken.

return 0 is the same thing as exit(EXIT_SUCCESS).

However, you do not need to explicitly return 0.

*return is unnecessary at the end of a c++ program.*

If the program executed the last instructions sucessfully and there is no code left to execute, the program exits with (EXIT_SUCCESS). return 0 is already implied and the compiler knows that.

However if you think that you need to explicitly return 0, then go ahead. It will not hurt anything.

Here is an example were it would be necessary for return. (or exit() )


```
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[]) {
    if (argc == 2) {
        string param = argv[1];
        if (param == "--hide") {
            return 0;
        }
    }
    cout << endl << "Hello World!" << endl;
}
```
execute the program with *--hide* as a paramater.


----------



## mussavcom (Mar 29, 2004)

"It's also suggested to not use tabs for indentation. Use 4 spaces instead or set your editor to insert 4 spaces when tab is pressed or automatic indention is invoked."

Why's that?


On a side note, one thing I was wondering about--will a program I compile act identically on all computers? Like, if I don't include cstlib and my program works with my compiler anyway, will the EXE not work with other machines? (I doubt it.)


----------



## Shadow2531 (Apr 30, 2001)

It avoids problems between different editors and also avoids problems with posting on forums.

A regular tab is usually 8 spaces in length. A lot of editors will adjust the VIEW so that an 8-spaced tab looks like 4 spaces in length, but when you post it on a forum or copy it to another editor (that doesn't do that), you will end up with indents of 8 spaces instead of 4. An 8-spaced indent just looks funny. Now you might think that it would then be desired to set a tab to be 4 spaces in length and everything would be fine. Not always and you can still have problems posting on forums etc.

So to sum it up, tabs are evil and cause inconsistencies across editors. Also posting code with tabs in it can be inconsistent from browser to browser. (*Opera*).

4 spaces as an indent, just solves all those problems and makes sense.

This suggestion does not apply to makefiles. (They need tabs)

On the compiler note, if your program compiles without cstdlib, then the executable should work with most windows versions with almost al x86 processors, just as long as you didn't use any optimizations that would require say a PIV to run the program etc. It depends on your compiler though.

If cstdlib is not needed to compile then don't worry about it. The compiler will let you know if exit() or system() or whatever is not defined etc.

If someone else tries to compile your code that doesn't include <cstdlib> on a different compiler and it throws an error saying exit() or system() is not defined, they might not know what include file exit() or system() is in, but if you have cstdlib there and their compiler needs it explicity, then they will not have a problem.

You could also just comment out

//#include <cstdlib>

, so if someone can't compile your code, then they can uncomment it and give it a shot.

However you don't really need to comment it out. It will not increase the executable size if exit() or system() is already defined in <iostream> etc or in your compiler itself.

The gcc compiler I know does not require cstdlib to be included to use exit() or system(), but others may so I include it , becuase that's were exit() and system() live anyways.

If you don't want to worry about the exit() deal and cstdlib, just use return.

return 0 = exit(EXIT_SUCCESS)
return 1 = exit(EXIT_FAILURE)

For system(), it would be a good idea to include <cstdlib> whether your compiler needs it or not.


----------

