So many responses! ._.
Thank you everyone for your responses!
Furry Guy wrote: |
---|
A std::string is a complex data type, not simple Plain Old Data like an int or double. Copying one is an expensive operation. Using a reference or pointer to pass a string into a function doesn't create a temporary duplicate copy in the function. It passes the address of the string.
Using const as part of the parameter declaration is to prevent the function from potentially modifying the string. The compiler will slap your hand and complain you committed a boo-boo trying to change a string that is not to be changed.
Now, if you WANT to change the string, then don't declare the parameter const. |
Ah okay. Also, is it a good programming practice to name const for everything that cannot be changed? Including double, int, etc? So if you're working with other people it makes it easier to tell what is being modified and what isn't. I ask because:
http://www.cplusplus.com/forum/beginner/180986/#msg888218
------------------------------------------------
Duthomhas wrote: |
---|
Ah hah!
Yes, that is exactly the problem. And it is what ne555 was talking about.
ASCII 13 is the code for Carriage Return, which is '\r' in C and C++.
There are a number of ways to fix it, but the simplest is simply to get rid of any trailing CR when you read the string. Use a function:
1 2 3 4 5 6
|
std::istream& getline2( std::istream& ins, std::string& s )
{
getline( ins, s );
if (s.size() and (s.back() == '\r')) s.resize( s.size() - 1 );
return ins;
}
|
Use that for getting input instead of getline(). |
Ah okay, I thought unix was an operating system, not command prompt. Oops.
For the code, couldn't you not return anything and make the function void because you use a reference, which, from my understanding will update the variable automatically?
For example, if I were to do the following in the readFile() function:
getline2(iFile, pName);
Then it would go through the function, automatically resize pName if needed, then automatically update pName without needing to return it.
Also, is
s.size()
checking if the string isn't empty, then if it is NOT empty, it will tell the if statement true?
Duthomhas wrote: |
---|
One other point: always resync your inputs as soon as possible. Remember,
The user will always press enter after every requested input.
So, for example, when adding a player:
....
Lines 8 and 12 automatically synchronize to the next line.
But line 16 does not, so we have to do it manually on line 17. |
Could you please explain what you mean by "resync your inputs"?
Also, could you explain more in depth about what the parts in the line below does:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // resync now!
For the line in the code:
#ifdef DEBUG
Wow! That is so useful! Just added ifdefs for Debugging through my code! Thanks a ton!
Somewhat unrelated to the original post, but do you think it's better to include
#ifdef DEBUG
messages (but DEBUG not defined) for homework assignments
and software engineering work? Or only use it for yourself but remove it for the final product you submit to your professor or employer? I think other developers may find it useful when editing your code, but maybe it'll just make your code look messy?
Is it considered good practice to indent between #ifdef and #endif? Or bad practice? Or neither?
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
if (helloVar == "Hello World") {
std::cout << "YEAH! Hello to you all as well! <3\n";
#ifdef DEBUG
std::cout << "[Debug] if (helloVar == "Hello World") ran.\n";
#endif
// VS:
/*
#ifdef DEBUG
std::cout << "[Debug] if (helloVar == "Hello World") ran.\n";
#endif
*/
}
|
Dothomhas wrote: |
---|
Inside the function it is const — the function promises not to change it.
Outside the function it is whatever you supply. Including things constructed on-the-fly.
[More text]
But it will if the argument is a const reference.
Remember, a const is only a contract qualifier that says, “With great (const reference) power comes great responsibility (to not const_cast and change the referenced object)”.
Keep up the good work!
|
Ah okay, thanks! (And you as well, for good help)! :P
Duthomhas wrote: |
---|
You can quote someone by saying .... |
Ahh okay, thanks! I've been wondering if there is a better way to show who I'm responding to!
--------------------------------------------
Enoizat wrote: |
---|
I think the biggest problems emerge when we try to use std::cin >> and std::getline in the same code.
I think in general the solution is to avoid std::cin >> unless we can use only it. |
Yeah, I think I've been told that in the past but I don't know of a way to get input for numbers other than cin (or iFile >> variableName).
Enoizat wrote: |
---|
The following code does’t give me any problem in a W7 machine, whether the EOL is \r\n, or it is \n:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
[Lots of code]
for(std::string name; std::getline(fin, name) && !name.empty(); /**/) {
std::string team;
std::getline(fin, team);
std::string many;
std::getline(fin, many);
std::istringstream iss { many };
double points, rebounds, assists;
iss >> points >> rebounds >> assists;
players.push_back( Player{ name, team, points, rebounds, assists } );
}
|
|
Looks like a short/easy way to get what I want done. Could you explain what this line does though:
std::istringstream iss { many };
Also how you make these doubles instead of strings:
iss >> points >> rebounds >> assists;
Unless you didn't, in which case, I forgot to specify that I'm trying to do it with doubles not strings for number values.
Phew, done replying to everyone. :)
Edit:
By the way, just so everyone's clear where my actual code is at the moment, thanks to your replies it does successfully remove '\r' and print the info for the specific player stated! :)
So the original purpose of this thread has been solved. Just getting some clarification on some parts now.
Current Output:
1 2
|
Please enter input file name: players.txt
Successfully opened players.txt!
|
--------------------------------------------------------------------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
Options:
1. Add a new player
2. Display all player data
3. Display individual profile data
4. Edit specific player's data (team, points, rebounds, assists)
--------------------------------------------------------------------
Enter the number you would like to do: 3
Which player would you like to print data for?
Type their full name (CaSE-SenSiTiVE): Stephen Curry
--------------------------------------------------------------------
Stephen Curry's Data:
Player Name: Stephen Curry
Team Name: Golden State Warriors
Points: 29.8
Rebounds: 5.2
Assists: 6.5
|