### Noob C++ Need a simple answer

Can someone please explain a few things in the sample code I am playing around with.

When " char mName [51];" is used what does the number 51 mean anddo? That confuses me. Also the line "cin.getline(mName,50);" What does the 50 mean and do? Just those numbers confuse me. For the most part I understand the basics.

Also what does this line do? " cout<<fixed<<setprecision(2);" and "cin.ignore().get();"

Thx in advanced, hope its not confusing to what I am asking. I just want to understand the meaning of these parts.

 ``123456789101112131415161718192021222324252627282930313233343536`` ``````//Box Office Problem. Determine profits of ticket sales #include #include using namespace std; int main() { char mName [51]; int aTicket; int cTicket; int gProfit; double nProfit; double amountPaid; cout<<"Enter movie name: "; cin.getline(mName,50); cout<<"Enter adult ticket ammount: "; cin>>aTicket; cout<<"Enter child ticket ammount: "; cin>>cTicket; gProfit = aTicket * 6 + cTicket * 3; nProfit = gProfit * .2; amountPaid = gProfit - nProfit; cout<<"Movie Name: "<

Last edited on
`char mName [51];` is a so called "C-string". It's an array of 51 characters, the last of which is reserved for the delimiter character '\0'.

Typically, magic numbers like this are frowned upon, because it's bad practice. I'll show you a better way of doing it later on.

`std::cin.getline` extracts characters from the stream (input) and stores them in a C-string, in this case, mName. It continues extracting until a delimiting character is reached, or until n characters have been extracted (where n is the second parameter, in this case 50).
In the context of this program, std::cin.getline() is more applicable than std::cin, because std::cin is picky about white-space characters, where as getline is not. This is important because movie titles may have spaces in them.

`std::fixed` sets the floatfield format flag for the specified stream, which means you're using fixed floating point notation.

`std::setprecision` sets the decimal precision for outputted floating point values for the specified stream.

Look at this documentation for an example and additional info:

http://www.cplusplus.com/reference/iomanip/setprecision/

Anyhow, like I said, magic numbers aren't really a good thing. Here would have been a better way to do it.

 ``123456`` ``````const unsigned short NAME_LEN = 51; char mName[NAME_LEN]; //... std::cin.getline(mName, NAME_LEN-1);``````
Last edited on
Wow thank you very much for the detailed answer. I really appreciate it!
Sure thing!
Hey xismn one last thing. What does the line "cin.ignore().get();" do?
Well, to fully answer that question, here's some small preliminary stuff.

`std::cin` is an `istream` object, that's declared in the `std` namespace.

The line in question calls two functions, first `std::cin.ignore` and then `std::cin.get()`.

The way this has been written as cin.ignore().get() works, because `std::cin.ignore()` returns a reference of the istream object (it returns `*this`, itself). The object that is returned (itself) then calls it's member function get().
This is called method chaining.

Basically, the idea is that with std::cin.ignore(), you are discarding the left-over characters from the istream buffer, and pausing the program with std::cin.get().

However, a call to std::cin.ignore() with no arguments takes on the following default arguements:

`istream& ignore (streamsize n = 1, int delim = EOF);`

That's the function declaration for std::istream.ignore() - where the first parameter is the number of characters to discard, and the second is an enumerated type which represents a special case delimiter character. If you don't provide arguments to the function, ignore() will only discard one character (or if/until end-of-file is reached). This may not be optimal, because there may be for whatever reason, additional extrenuous characters in the buffer.

The reason one should clear the buffer of extra characters, is because istream objects can easily be set into an error state if handled improperly, and this makes them unresponsive or unpredictable.

In your case, I don't think it'll be much of a problem - unless it is, then I can share one way of going about handling that.
Last edited on
Topic archived. No new replies allowed.