Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
The Assignment was to create a PairList class that held a vector of a Pair class. The Pair class is a template class that holds any two variables of one type.
The code prompting that question:
template <class T>
void PairList<T>::printList()
{
for(unsigned int i = 0; i < this->pList.size(); i++)
{
cout << i+1 << ".\t"
<< (this->pList[i]).getFirst()
<< "\t" <<(this>pList[i]).getSecond()
<< endl;
}
}
Something my teacher said about this code:
"One thing I will say about the ones I like is that both used cout in functions and it doesn't belong. I would like your input as to why it is not correct and what things could have or should have been done to eliminate the use of cout in these functions
This is a warning for now. In the future I will start docking serious points like the 70% I do in the level 1 class."
He wasn't very specific on what functions he wanted and let us create whatever we thought was necessary. I received an A but wanted to know why I shouldn't use cout in this function.
Why is using cout in a utility function a bad thing to do?
What should I do instead?
I'm sorry this is such an ambiguous question. I realize that after posting. Also I'm overwhelmed by all extremely fast feedback!
I suspect that whoever said using cout in a class is bad is thinking that, instead of doing
type function ()
{
. . . .
cout << "WHATEVER" << endl ;
. . . .
}
that you should have
type function (std::ostream &strm)
{
. . . .
strm << "WHATEVER" << endl ;
. . . .
}
There are two issues at play here. Regarding headers, you should not typically place implementation in your header files, so a std::cout command would be inappropriate there. Regarding your class, it greatly depends--in some cases, using cout in a class function is perfectly fine.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm having trouble creating the pause manipulator for the code below.
"m" is an object that prints the given numbers in morsea code. This works for me, but I don't know how to make a manipulator for it. Can you help me with this problem?
long x = 0x2A6B5B5A;
double y = 8.23786789;
m<< x << pause << y ;
It is not quite clear what your code is supposed to do. However, all you need is this:
struct pause_t {};
std::ostream& operator<<(std::ostream& out, const pause_t& p) {
// put code here
return out;
};
Put the code that does whatever you like to happen in place of //put code here, then use it like this:
pause_t pause;
std::cout << 42 << pause << 42;
If m in your example is not a std::ostream then you just need to adjust above overload for << accordingly.
PS: Strictly speaking the above is not an io manipulator. io manipulators are typically implemented as functions that take the ostream as parameter and std::ostream has an overload of << for such functions. However, that distinction is probably not important in your case.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Hello I'm wondering how I can call functions within parameters in my class in main?
class processChoice {
public:
void processInput(string, int, string, int);
};
void processChoice::processInput(string processInput_UN,
int processInput_PC,
string initial_UN,
int initial_PC) {
for (; (processInput_UN != initial_UN) || (processInput_PC != initial_PC);
cout << endl) {
cout << "Enter your username: " << flush;
cin >> initial_UN;
cout << "Enter your 4 digit pincode: " << flush;
cin >> initial_PC;
cout << endl;
if ((processInput_UN == initial_UN) && (processInput_PC == initial_PC)) {
cout << "Access granted!" << endl;
} else {
cout << "Username and/or pincode doesn't match, try again..."
<< endl;
}
}
int main() {
userPinchoice Choice;
Choice.chooseUsername();
Choice.choosePincode();
cout << endl;
initial Values;
Values.initialUsername();
Values.initialPincode();
processChoice Input;
Input.processInput();
return 0;
What am I suppose to put in the round brackets at Input.processInput()?
I have been trying to get it to work but I just can't seem to access the function. I'm new to this so any help would be welcome.
Thanks in advance!
The bulk of your problem lies in the class processChoice. Here are some of such errors:
Firstly, the parameters specified in the function declaration in your class is faulty:
void processInput(string, int, string, int);
Here, you have only specified 4 data types, not variables. Kee in mind, these are variables that store the data that is passed from another function. In order to do this, you need to have these variables declared in the above line, variables with specific names that can be identified inside a function. You should have this line in your function declaration:
void processChoice::processInput(string processInput_UN, int processInput_PC, string initial_UN, int initial_PC)
This brings up another problem. Your function header is diffent upon declaration, and its header is different upon defining. The compiler sees it as 2 different functions. Therefore, you should keep your function headeer the same when declaring it and defining it.
Secondly, the for-loop inside your function has syntax errors:
for (; (processInput_UN != initial_UN) || (processInput_PC != initial_PC);
cout << endl) {
Firstly, judging from the syntax of the condition statement of the loop, it should be a do-while loop, not a for loop. Secondly, the cout << endl; should not be inside the condition statement of the for-loop; it should be implemented with the rest of the function. Thirdly, you do not put a semicolon at the end of your condition statement; it tells the compiler that this an empty loop without a body. Your loop should be something like:
do
{
cout << endl; // this is where you put the cout statement
// add rest of function code here
}
while ((processInput_UN != initial_UN) || (processInput_PC != initial_PC))
Another side note, why do you need 4 input parameters in your function? You take the pin number and username as input from the user, eliminating the need for 2 parameters. Something like:
string initial_UN;
int initial_PC;
If you declare this inside your function, just before the do-while loop I suggested above, then that eliminates these 2 variables from being parameters.
Now, to get to your question, if we have the following declaration:
Input.processInput();
Then we need to pass 4 parameters (2 if you follow my notes above) to it, and that is what the brackets next to the function name in this line of code a for. To pass a value to the function, simply, do the following (I'm only passing 2 parameters here, you can pass how many ever parameters you have defined in yout class only, not less or not more):
Input.processInput("Username", "password");
There are 2 input parameters that specify what the username and password should be. To differentiate the parameters being passed, syntax requires a comma to split them. You can also pass variables as arguments; however, make sure that you have initialized these variables.
This is a long post, so I may have made some errors I didn't notice. If there is any other mistake I noticed, then please inform me in the comments.
Good luck!
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have this class definition:
class event {
public:
wstring type;
int pos;
int sen;
event(const string &t) : type(util::string2wstring(t)) {}
~event() {};
};
and the global variable:
list<event> events;
This variable was initialize in one function and after that I want to modify differents values of this events like:
for(auto ei : events) {
ei.pos = (*w).get_position();
ei.sen = sen;
cout << "pos: " << ei.pos << " in sentence " << ei.sen << endl;
++w;
++sen;
}
event ei = events.front();
cout << "pos2: " << ei.pos << " in sentence2 " << ei.sen << endl;
Then the first cout print the correct values of the new pos and sen but the second (outside the for scope) print the oldest values.
If I print the values in other function the oldest values are printed.
I think that the problem is no reference access to the global variable but is it true?
And the most important, how can I fix it?
Thanks for your time,
Regards.
The problem is that ei in your range-for loop is a value and not a reference. That means it is a copy of the element in the container, and modifying a copy of course does not modify the original.
To use references you need to specify it:
for(auto& ei : events) { ... }
// ^
// Note ampersand here
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am working with a src someone made for a game I am making. the person recently has left and I am trying to pick up the pieces, I don't really know c++ but I was hoping to get some help with a compile issue coming from a specific file I am trying to compile. is there a way I can stretch the limits of a const or perhaps use a different type that will hold much larger values?
The numeric limits for the fundamental types of your implementation are defined in the <limits> header, and the trait class std::numeric_limits provides the values for your machine. That's the end of it, you cannot "beat" those limits with fundamental types. Example (from cppreference.com):
#include <limits>
#include <iostream>
int main()
{
std::cout << "type\tlowest\thighest\n";
std::cout << "int\t"
<< std::numeric_limits<int>::lowest() << '\t'
<< std::numeric_limits<int>::max() << '\n';
std::cout << "float\t"
<< std::numeric_limits<float>::lowest() << '\t'
<< std::numeric_limits<float>::max() << '\n';
std::cout << "double\t"
<< std::numeric_limits<double>::lowest() << '\t'
<< std::numeric_limits<double>::max() << '\n';
}
Live on Coliru
If you want more than this, or even arbitrary precision/length numbers, then you need to use a multi-precision library, e.g. Boost.Multiprecision.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
so I have a C++ bool function that I've written that looks like this:
bool EligibileForDiscount(const char CompanyUsed, const char CompanySubscribed)
{
bool eligible = false;
if (CompanyUsed==CompanySubscribed)
eligible = true;
return (eligible);
}
Now in my main() this function is called as the only parameter for an if statement:
if (EligibleForDiscount(CompanyUsed, CompanySubscribed))
{
ApplyDiscount(Cost, CompanySubscribed);
cout << "\nAfter your discount, your rental is: $"
<< fixed << showpoint << setprecision(2) << Cost << ".\n";
}
The main function was written by my teacher and we wrote the other functions, so this if statement isn't supposed to be changed.
So I understand what the if statement is trying to accomplish, by basically saying "if (true) do this..." since the EligibleForDiscount will return a boolean value.
However, g++ is giving me an error with the if statement, telling me that EligibleForDiscount is not declared in this scope.
But I'm not trying to use it as a value but as a call to a function.
It may be because of two reasons:
You misspelled the function name when called : if (EligibleForDiscount(CompanyUsed, CompanySubscribed)) should be written like your implementation of the function, which is EligibileForDiscount.
This can happen if you forgot to declare the prototype of the function, which is an indicator to the program that you're going to use that function. You simply need to write somewhere before you use the function bool EligibileForDiscount(const char , const char)
One of these should work!
Because : EligibileForDiscount != EligibleForDiscount with an additional "i", just a typo.
p.s. you can write EligibleForDiscount like this:
bool EligibleForDiscount(const char CompanyUsed, const char CompanySubscribed)
{
return CompanyUsed==CompanySubscribed;
}