|
|
Furry guy wrote: |
---|
If you are passing an object into a function, either as a copy or as itself by pointer/reference, and you want to indicate the function is not supposed to alter the object, declare the parameter const. You are setting a contract for you, the programmer, the compiler and other users of your function the object should not be modified. What went in should come out the same, which doesn't matter much if the object was copied locally. A devious programmer could get around the request by casting away the constness. Think "SHOULD NOT" instead of "CAN NOT" be changed. Dammit, would you stop asking such good questions that make me actually have to work to explain them? :Þ (No, not really. You asking questions actually is fun for me to try to dig down and dredge up a worthwhile and meaty answer.) |
Duthomhas wrote: |
---|
Re: OS vs Command Prompt When you have the VM up and running, you are running two operating systems at the same time. That has less to do with it than the newline conventions expected by the C library. The Wikipedia article on Newline is a good read. https://en.wikipedia.org/wiki/Newline Er... my head is really fuzzy and I am tired right now. I’ll respond to your other questions sometime tomorrow... Sorry. |
Enoizat wrote: |
---|
Please note my English is poor, so I’m never sure I’ve clarified what I meant to say - that’s why I’m so verbose. But, despite the number of rows, the underlying idea is pretty simple. |
Enoizat wrote: |
---|
In C++ a std::fstream is a stream. In C++ std::cin is a stream. In C++ a std::stringstream is a stream. std::getline extracts the ‘\n’ character, but then discards it: https://en.cppreference.com/w/cpp/string/basic_string/getline So, if we read a ‘line’ of any stream by std::getline (with the default settings), we ‘clean’ the stream from the final '\n', without taking it along. Later, we can use that std::string to initialize another stream, like a std::istringstream, and use it exactly the same way we could use std::cin. .. <Rest of your msg> |
iss >> newVarNme
?
|
|
|
|
Enoizat wrote: |
---|
- - - Yes, this is definitely too long. My bad. |
ne555 wrote: | ||
---|---|---|
consistency with `getline()', whose prototype is istream& getline (istream& is, string& str); note that you may use the return value like this
|
ne555 wrote: |
---|
and as you see it (again), printing debugging have its shortcomings, so I'll suggest you to use a debugger to watch your variables. |
Duthomhas wrote: |
---|
By “synchronizing inputs” is meant that you are keeping track of where you are in your input stream by some semaphore — in our case, the newlines. We do this because we know that certain inputs should have an newline after them. For example: ... In each of these cases we know the user will press Enter after answering the question. So we make sure to leave the input stream in a common state: having read the last newline and ready to begin reading a new line. This allows us to program future inputs without having to worry whether the last input has left an extra newline lying around. That last line tells the input to read and discard all input until it reads and discards a newline. When in doubt, read the documentation. |
Okay, so to clarify, basically stringstream will: 1. Take all user input 2. Store it into a string data type, but with the \r(?) and \n stripped (not stored in string) |
3. Then you can take parts of that string and assign it to other data types (like int and double) through iss >> newVarNme? |
What am I doing wrong? |
|
|
Please input player's name (can have spaces): John Doe Please input player's points (int): 13 Please input player's health (double): 66.6 Please input player's name (can have spaces): Ann Smith Please input player's points (int): 14 Please input player's health (double): 77.7 Please input player's name (can have spaces): Barbara Williams Please input player's points (int): 15 Please input player's health (double): 88.8 Please input player's name (can have spaces): Cindy Jones Please input player's points (int): 16 Please input player's health (double): 99.9 John Doe 13 66.6 Ann Smith 14 77.7 Barbara Williams 15 88.8 Cindy Jones 16 99.9 |
Enoizat wrote: |
---|
Well, not exactly... You need to change the subject: std::getline takes the user input, normally untill the first ’\n’ is met, discards the ’\n’ and store the rest into a std::string. .... <rest of message> ... |