Verify if two strings are equal in C++ [closed] - c++

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 9 years ago.
Improve this question
I'm doing an assignment that requires me to use a function to check whether two strings are equal. I keep getting a parse error on line 20, where the function is called, and I don't know what's wrong. Please take a look and let me know if you see what could be causing the problem. Thanks!
#include <iostream>
#include <string>
using namespace std;
bool checker(string firstWordParameter, string secondWordParameter);
int main()
{
string firstWord, secondWord;
bool match;
cout << "Hello user.\n"
<< "This program will determine whether two words are the same.\n"
<< "Please enter your first word you would like to check: ";
getline(cin, firstWord);
cout << "Great, now enter the second word: ";
getline(cin, secondWord);
match = bool checker(firstWord, secondWord);
if(match == true){
cout << "Match.";
}else{
cout << "Totally not a match.";
}
return 0;
}
bool checker(string firstWordParameter, string secondWordParameter)
{
if(firstWordParameter == secondWordParameter){
return true;
}else{
return false;
}
}

Try changing
match = bool checker(firstWord, secondWord);
into
match = checker(firstWord, secondWord);

Line 20 is
match = bool checker(firstWord, secondWord);
Change it to
match = checker(firstWord, secondWord);
Also when you see error in compiler, double click it then it will show you the line with the error.

Related

value is always set to 1 even if the input is 0 [closed]

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 2 years ago.
Improve this question
ok so i writ this little code in c++. keep in mind i just started learning c++ so this problem confused me a lot
when i run this code in code blocks (
using namespace std;
int Answer;
int main()
{
cout << "yo, are u male?"<< endl;
cin >>Answer;
if (Answer = 1){
cout << "ur male"<< endl;
} else {
cout <<"ur female"<< endl;
}
cout <<Answer;
}
)
the answer value is always set to one even if i type 0
i tried coding another if statement for the answer value if it was 0 but that didn't work either
In C++, = operator is an assignment operator and it sets value of left operand to value of right operand. Therefore the value of Answer becomes 1 thanks to Answer = 1.
You should use == operator to check equality.
Use the == operator in your code, as = operators does the job of assigning values.
using namespace std;
int Answer;
int main()
{
cout << "yo, are u male?"<< endl;
cin >>Answer;
if (Answer == 1){
cout << "ur male"<< endl;
} else {
cout <<"ur female"<< endl;
}
cout <<Answer;
}
)

My output file is not displaying the solution [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
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.
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.
Improve this question
My output file is supposed to show to answer to the function it calls on. The program runs fine, however it is not displaying the text in the "prime" function. the output file, when checked, only displays 1's. I believe this is due to the fact that its declared as a bool function, and set to return true. However, how would I get this code to return the solution in Prime to the output file?
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
bool prime(int);
int main()
{
int reader;
ifstream Infile;
Infile.open("numlist.txt");
ofstream outputFile;
outputFile.open("theoutput.txt");
while (Infile >> reader)
{
outputFile << prime(reader) <<endl;
}
Infile.close();
outputFile.close();
}
bool prime(int p)
{
if (p % 2 == 0)
cout << "\n" << p << "\n Is not a prime number";
else if (p % 2 != 0)
cout << "\n" << p << "\n is a prime number";
return true;
}
No errors, however the output file is only showing 1's.
This is happening because in your prime() function, all the output is going to cout and not into outputFile. The prime() function returns a bool which is what is sent to outputFile.
If you'd like to have output of the function go to outputFile, you can either pass outputFile as a parameter and use that instead of cout or make it global.
A few more comments on your code: you don't need the full else if (p % 2 != 0) in the else statement. You can just use else, because p % 2 is either 0 or it's not, there's no other option.
Also, strongly recommend using braces around if statements, even if they are just a single line.

The output begins with space [closed]

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 7 years ago.
Improve this question
I made a function that will reverse the string, but the output of the reversed string always shifts towards the right by one character.
#include <iostream>
#include <string>
using namespace std;
void reverse(string string1)
{
cout << endl;
for (int i = string1.size(); i >= 0; i--)
{
cout << string1[i];
}
cout << endl;
}
int main()
{
string string1;
getline(cin, string1);
reverse(string1);
system("pause");
return 0;
}
Your first output is of a character that does not exist.
std::string's leaky abstraction means that your first iteration is printing '\0', which apparently looks like a space in your configuration.
Begin at string1.size() - 1.

why am I getting random results when incrementing an int [closed]

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
I have to write a function that goes through a string, finds any capital letters present and makes them lowercase. I decided to add a bit of code to show what letters were found and how many were found. Although on each compilation, the value of 'cnt' yields ridiculous results.
#include <iostream>
#include <vector>
using namespace std;
int upper(string s) {
int cnt;
vector<char> v{};
for (auto& i : s) {
if (isupper(i)) {
v.push_back(i);
i = tolower(i);
++cnt;
}
}
cout << "new string is '" << s << "'\n"
<< "number of capitals found is " << cnt << "\n"
<< "letters found were ";
for (auto l : v)
cout << l << " ";
return 0;
}
int main() {
string l = "This IS a TeSt";
upper(l);
}
I'm sure I must have done something wrong with the loop but whatever the problem is, I cannot find it.
The variable cnt is never initialized when used, change
int cnt;
to
int cnt = 0;
You failed to initialize the local variable cnt. Using an uninitialized value provokes undefined behavior, under which basically anything can happen.
Use int cnt=0; and please turn on all your compiler warnings.

C++ Xcode lldb error: loops never work [closed]

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 9 years ago.
Improve this question
thanks for taking the time to help me out.
I'm really new with C++ and Xcode. I was working on a simple program to help me understand loops, so my goal was to make a simple "echo machine". This is my code:
string words;
int main()
{
do {
cout << "Enter text.";
cin >> words;
cout << "You entetered " << words << "!";
}
while (words != "goodbye");
return 0;
}
My result is nothing but lldb in parenthesis. I am very frustrated and can't find what I'm doing wrong anywhere. Please help and thank you so much.
Are you just missing the include directives for the standard headers you're using?
Try this:
#include <iostream>
#include <string>
using namespace std;
int main() {
string words;
do {
cout << "Enter text: ";
cin >> words;
cout << "You entered " << words << "!\n";
} while (words != "goodbye");
return 0;
}