Palindrome boolean not checking false - c++

For some reason my boolean is not checking false, can anyone help me figure out why? Also how would I go about making this program ignore spaces if it were a phrase (i.e. "never odd or even")
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string input;
int i = 0;
bool flag = true;
int size = input.size();
cin >> input;
for (i = 0; i < size; i++)
{
if (input.at(i) != input.at(size - i - 1))
{
flag = false;
}
}
if (flag = true)
{
cout << "palindrome: " << input << endl;
}
else
{
cout << "not a palindrome: " << input << endl;
}
return 0;
}

New answer
You are getting the size of the string before the user has entered the string.
Change
int size = input.size();
cin >> input;
to
cin >> input:
int size = input.size();
Previous answer
You are using a single = rather than two in the if statement.
Change
if (flag = true)
to
if (flag == true)

Related

How to stop the program when the user inputs a char or string in an int variable?

So i have this homework to do where the user inputs a number and how many number he wants to check and the computer tells him the numbers for which the first number is divisible.
The thing is that I wanted to do a little extra on the homework to block the program once a char or string is inputted, because else the program would crash
#include <iostream>
#include <stdlib.h>
#include <vector>
using namespace std;
int main() {
unsigned int myNum, nTimes;
bool Continue = true;
char ContinueYN;
vector<int> nDiv;
while (Continue) {
cout << "Insert a number.";
cin >> myNum;
cout << "How many numbers do you want to check?";
cin >> nTimes;
for (int I = 1; nTimes >= I; I++) {
if (myNum % I == 0) {
nDiv.push_back(I);
}
}
cout << "The number is divisible for the following numbers: ";
for (int i = 0; i < nDiv.size(); ++i) {
cout << nDiv[i] << ' ';
}
cout << "\nPress any letter to continue or press n to stop\n";
cin >> ContinueYN;
ContinueYN = tolower(ContinueYN);
if (ContinueYN == 'n') {
Continue = false;
}
nDiv.clear();
system("cls");
}
return 0;
}

string of letters only , without any special character

i wanted to take input of letters only but its not working properly.
if i input a random string like abcd123
it repeats try again when i enter again same abcd123
it will output ignoring the condition
i want this only to take abcd.
#include <iostream>
#include <string>
using namespace std;
bool IsLetters(string &input)
{
for (int i = 0; i < input.size(); i++)
{
int uppercaseChar = toupper(input[i]); //condition for letters
if (uppercaseChar < 'A' || uppercaseChar > 'Z')
{ cout<<"try again\n";
cin>>input;
return IsLetters;
}
}
return true;
}
int main()
{
string x;
cout<<"enter your name\n";
cin>>x; //string input
if (IsLetters(x)) // function call
{
cout << "your name is\n"<<x;
}
return 0;
}
you need to change logic, pls try following
#include <iostream>
#include <string>
using namespace std;
bool IsLetters(string& input)
{
for (int i = 0; i < input.size(); i++)
{
int uppercaseChar = toupper(input[i]); //condition for letters
if (uppercaseChar < 'A' || uppercaseChar > 'Z')
{
return true;
}
}
return false;
}
int main()
{
string x;
cout << "enter your name\n";
cin >> x; //string input
while (IsLetters(x)) // function call
{
cout << "try again\n";
cin >> x;
}
cout << "your name is\n" << x;
return 0;
}
You can try this straightforward solution.
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cout <<"enter your name\n";
cin >> s;
int flag = false;
while (flag != true)
{
for (int i = 0; i < s.size(); i++)
{
if (!(s[i] >= 'A' && s[i] <= 'Z') && !(s[i] >= 'a' && s[i] <= 'z'))
{
flag = true;
break;
}
}
if (flag == true)
{
cout<<"try again\n";
flag = false;
cin >> s;
} else {
cout << s;
break;
}
}
return 0;
}

s.erase is not working when trying to remove spaces from a string

I am trying to remove the spaces from a string to validate a Palindrome phrase. I have looked up other methods, but my professor literally copy and pasted the remove space for loop in our instructions but I can't get it to work and he says he doesn't want us going to the internet for help. I am trying to remove spaces from a phrase like "too hot to hoot" to validate it. I can get my program to work with single words like "bob", but not phrases.
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char input[100];
cout << "Please enter a word/phrase: ";
cin >> input;
for (int i = 0; i < strlen(input); i++)
{
while (s[i] == ' ')//getting "s" is undefined error
s.erase(i,1);
}
int i = 0;
int j = strlen(input)-1;
bool a = true;
for (i = 0; i < j; i++)
{
if (input[i] != input[j])
{
a = false;
}
j--;
}
if(a)
{
cout << input << " is a Valid Palindrome." << endl;
}
else
{
cout<< input << " is not a Valid Palindrome." << endl;
}
system("pause");
return 0;
}
Maybe you have not copy the result from temporary variable 's'. So, the modified codes should be:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <cstring>
using namespace std;
int main(int argc, char *argv[])
{
char input[100];
cout << "Please enter a word/phrase: ";
fgets(input, 100, stdin);
string s(input); // define a temporary variable 's'
int i = 0;
while (i < s.length())
{
if (s[i] == ' ' || s[i] == '\n')
{
s.erase(i, 1); // erase from variable 's', other then 'input'
continue;
}
i++;
}
// copy result from 's' to 'input'
sprintf(input, "%s", s.c_str());
int j = strlen(input) - 1;
bool a = true;
i = 0;
for (i = 0; i < j; i++)
{
if (input[i] != input[j])
{
a = false;
}
j--;
}
if (a)
{
cout << input << " is a Valid Palindrome." << endl;
}
else
{
cout << input << " is not a Valid Palindrome." << endl;
}
system("pause");
return 0;
}

I have a hangman game I'm making, and I need help figuring out why it's not fully working

I'm making a hangman game in C++ and I am close to finishing, however I have one major issue. I have to get the game to only allow the user 4 guesses, but my program doesn't register the correct number of guesses.
I've tried to change variables as well as the conditions within the if and else statements regarding guessing.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
cout << "Welcome to hangman!" << endl;
char choice = 'y';
while (choice == 'y') {
string word;
cout << "Enter a word to guess: ";
getline(cin, word);
if (word.empty()) {
cout << "The word should not be blank.\n";
continue;
}
bool contain_space = false;
for (char c : word) {
if (isspace(c)) {
contain_space = true;
break;
}
}
if (contain_space) {
cout << "The word cannot contain spaces.\n";
continue;
}
vector <bool> index;
for (int i = 0; i < word.size(); i++) {
index.push_back(false);
}
**int guess_correct = 0;**
int guess_wrong = 4;
char letter;
while (guess_wrong >= 0 && guess_correct < word.size()) {
bool valid_guess = true;
cout << "Guess a letter." << endl;
cin >> letter;
for (int i = 0;i < word.size(); i++) {
if (word[i] == letter) {
valid_guess = true;
index[i] = true;
guess_correct++;
break;
}
else {
guess_wrong = guess_wrong - 1;
}
}
for (int i = 0; i < word.size(); i++) {
if (index[i] == true) {
cout << word[i] << "\t";
}
else {
cout << "___\t";
}
}
cout << endl;
}
cout << "Would you like to play again? (y/n)" << endl;
cin >> choice;
cin.ignore();
}
return 0;
}
The black ticks show the beginning of the code section I'm stuck on. Each time I run it, it will let me go through the game with correct guesses, but incorrect guesses don't allow for 4.
You are decrementing guess_wrong for each letter in the word that doesn't match, not once for the "whole guess".
you probably want to move the guess_wrong = guess_wrong - 1; // aka guess_wrong-- out of the for loop and only do it if (!valid_guess).

why i always get "false" in my code

I wrote code to check the input, I set the HavePunct flag always false. However when I input hello,world!! it returns the wrong results to me. Please let me know if you see any problems with my code:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main() {
string s,result_s;
char ch;
bool HavePunct = false;
int sLen = s.size();
cout << "Enter a string:" << endl;
getline(cin, s);
//检测字符串是否有符号
for (string::size_type i = 0;i != sLen; ++i) {
ch = s[i];
if (ispunct(ch)) {
HavePunct = true;
}
else
result_s += ch;
}
if (HavePunct) {
cout << "Result:" << result_s;
}
else {
cerr << "No punction in enter string!" << endl;
system("pause");
return -1;
}
system("pause");
return 0;
}
You are computing the length of the line before you enter any input. Hence, sLen is always zero. Move that line so it is after the line where you read the input.
cout << "Enter a string:" << endl;
getline(cin, s);
int sLen = s.size();
I cannot be sure, however it would seem because your iterator's upper bound is determined by the variable sLen, which you assign to be s.size() before you receive a string, therefore effectively making your upper bound 0 and causing your for loop never to execute.
Try this and let me know:
getline(cin, s);
int sLen = s.size();
for (string::size_type i = 0;i != sLen; ++i) {
ch = s[i];
if (ispunct(ch)) {
HavePunct = true;
}
else
result_s += ch;
}