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 1 year ago.
Improve this question
int main(){
while(true){
char input = getchar();
int x, y;
POINT xypos;
if (input == 'S' || input == 's'){
std::cout <<"enter new position" << std::endl;
std::cin >> x >> y;
SetCursorPos(x, y);
} else if (input == 'g' || input == 'G'){
GetCursorPos(&xypos);
std::cout << "X: " << xypos.x << "Y " << xypos.y << std::endl;
}
}
return 0;
}
Can someone please explain why with GetCursorPos, it has to reference the xypos object in the parameters? Why is it not possible to directly utilize it? Thanks
You probably mean why GetCursorPos(from the WinAPI) doesn't just return the position instead of taking a pointer and filling that right?
That's how the WinAPI works, almost all functions return BOOL to indicate success or failure and take information they populate by pointer.
Related
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 2 days ago.
Improve this question
This is my code:
int main() {
cout << "Hello, world!" << endl;
return 0;
}
// preprocessor directive#include <iostream>using namespace std;
//Main functionint main()
{
string name;
cin >> name;
cout << "hi, " << name << endl;
cout << "End Program";
return 0;
}
and it says that the bracket below the
//Main functionint main() expects a declaration
I tried moving the bracket up and down, left and right
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 1 year ago.
Improve this question
Is it okay or valid to use this? string answer = "A || B"; than using this if (answer == A || answer == B)?Because in my program I want to use that condition inside a while if statements. I want to put that two option condition in my while statement but it gets error.
// variables
string answerA = "Coin";
string guess;
int guessCount = 0;
int guessLimit = 3;
bool outOfGuesses = false;
cout << "Question A!\n" << endl;
cout << "A. What has a head and a tail, but no body?\n";
// if the answer is not correct, ask again only 3 times
while (answerA != guess && !outOfGuesses) {
if (guessCount < guessLimit) {
cout << "Answer: ";
cin >> guess;
guessCount++;
} else {
outOfGuesses = true;
}
}
if (outOfGuesses)
{
cout << "Your answers are all wrong! Better luck next time :)";
}
else
{
cout << "Your answer is Correct! ";
}
One std::string can hold only one string. You can use std::unordered_set to hold a set of multiple strings.
#include <iostream>
#include <string>
#include <unordered_set>
int main(void) {
std::unordered_set<std::string> accept_list = {"A", "B"};
std::string answer = "A";
if (accept_list.find(answer) != accept_list.end()) {
std::cout << "accept\n";
} else {
std::cout << "reject\n";
}
return 0;
}
(std::unordered_set is available since C++11. If your compiler doesn't support that, try std::set instead. Also initializer lists like accept_list = {"A", "B"} is since C++11, you may have to add each candidates separately in that case)
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
int n, pNumber;
string name, sName;
cin >> n;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
map<string, int> m;
for (int i = 0; i < n; i++) {
cin >> name >> pNumber;
m[name] = pNumber;
}
while (cin >> sName) {
if (m[sName]) {
cout << sName << "=" << m[sName] << endl;
}
else {
cout << "Not found" << endl;
}
}
I've went through numerous posts and haven't seen anyone mention just a single if statement with only the map key. I'm not sure whether this code is bad in practice or not. Any clarification would be very helpful.
This doesn't check whether or not the key exists. It tests if the value corresponding to the key is non-zero, creating it if it doesn't exist.
So, yes, it's bad for checking whether a key exists in a map or not because:
It will return that the key doesn't exist if the corresponding value is zero.
It will create the key if it doesn't exist.
This doesn't check if the value is in map. See std::map::operator[]. Even if it did what you wanted, it would still be less than ideal, because you are doing 2 searchers; 1 to check if the element is in the map and 1 to access that element. The standard way of doing this is using the std::map::find method, which returns an iterator to the element if it exists, and an end-iterator if it doesn't:
if (auto const it = m.find(sName); it != m.end()) {
std::cout << sName << " : " << it->second << '\n';
} else {
std::cout << "Not found\n";
}
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 5 years ago.
Improve this question
I'm pretty new to programming so this might be a question with an obvious answer for you guys and I'm really stumped, why doesn't cout work in the following function? I've included the iostream header so I'm assuming it has something to do with it being in the function?
int inputFileData(song musicLibrary[])
{
char discard = ' ';
int counter = 0, length = 0;
ifstream inData;
inData.open("songs.txt");
if (!inData)
{
cout << "Error: No input file found " << endl;
cout << "Program terminating..." << endl;
return 1;
}
while (!inData.eof())
{
inData.getline(musicLibrary[counter].title, ARRAY_CONST, ';');
inData.getline(musicLibrary[counter].artist, ARRAY_CONST, ';');
inData >> musicLibrary[counter].durationMinutes;
inData.get(discard);
inData >> musicLibrary[counter].durationSeconds;
inData.get(discard);
inData.getline(musicLibrary[counter].album, ARRAY_CONST, '\n');
length = strlen(musicLibrary[counter].album);
if (length = 0)
{
cout << length << endl; //This cout object doesn't work in this function
break;
}
else
counter++;
}
return counter;
}
The line if (length = 0) should be if (length == 0).
Elaborating Eli's answer:
if (length = 0)
Assigns the value 0 to length and then evaluates the expression. Since the expression returns 0 the condition evaluates to false and you don't enter the if clause.
Instead, use if (length == 0)
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 9 years ago.
Improve this question
How can I allow the user to enter a number in the form of e^x, or sqrt(x) when asked, instead of just entering a number in numerical form? Thanks.
Generally this is a question of parsing. In the case of these two forms exactly, a simple solution could be something like:
string input;
getline(cin, input);
if (input[0] == 'e' && input[1] == '^') {
int num = atoi(input.substr(2).c_str()); // probably better to use stringstreams here
cout << exp(num) << endl;
} else if (input.substr(0, 5) == "sqrt(" && input[input.size() - 1] == ')') {
int num = atoi(input.substr(5, input.size() - 6).c_str());
cout << sqrt(num) << endl;
} else {
cout << "error" << endl;
}
Didn't test that but should be roughly right. If you need to handle more nuanced cases or more than just these 2 cases, you'll have to do some more elaborate parsing.