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;
}
Related
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)
#include <iostream>
#include <string.h>
using namespace std;
void convert(string& str) {
for (size_t i =0; i <str.length(); i++) {
str[i] = tolower(str[i]);
}
}
bool isPalindrome(string str) {
int length = str.length();
for (int i = 0; i < length / 2; i++) {
if (str[i] != str[length -1 -i]) {
cout << str << " is not a palindrome" << endl;
return false;
} else if (str[i] == str[length -1 -i] && toupper(str[i]) == toupper(str [length -1 -i])) {
cout << str << " is a palindrome" << endl;
} // for loop
return true;
}
return false;
}
int main () {
string str;
getline(cin, str);
convert(str);
isPalindrome(str);
return 0;
}
For some reason the output box in a homework grader for my coding assignment is showing blank but when I run my program it says whether or not it is Palindrome. Also the automatic grader also searches my code for a specific pattern like .+isPalindrome(\"Madam\").+ but the automatic grader says I only got 10 points because my code had a specific pattern of \s\isPalindrome(string str). I am really confused and do not know why my output is being shown blank and the automatic grader is looking for a specific pattern but I do not know what pattern???
You are just misunderstanding the output terminal. Everything is working fine just required to ask the user to enter a value.
#include <iostream>
#include <string.h>
using namespace std;
void convert(string& str) {
for (size_t i =0; i <str.length(); i++) {
str[i] = tolower(str[i]);
}
}
bool isPalindrome(string str) {
int length = str.length();
for (int i = 0; i < length / 2; i++) {
if (str[i] != str[length -1 -i]) {
cout << str << " is not a palindrome" << endl;
return false;
} else if (str[i] == str[length -1 -i] && toupper(str[i]) == toupper(str [length -1 -i])) {
cout << str << " is a palindrome" << endl;
} // for loop
return true;
}
return false;
}
int main () {
string str;
cout<<"Enter a string to check is it palindrome or not."<<endl;
getline(cin, str);
convert(str);
isPalindrome(str);
return 0;
}
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 want to search for a name from a list of names and if its found I should return the whole info of that person else not found. I dont know why my code is not working. I can print the info of the person, eg:
Input:
3
Steve 9812761810 017
Wayne 8299915781 102
Ronnie 9161462903 120
Wayne
Output:
Wayne 8299915781 102
#include <iostream>
#include <string.h>
using namespace std;
int main() {
string name[100];
long long number[100];
int year[100], i, n, check = 0;
string inp;
cin >> n;
for (i = 0; i < n; i++) {
cin >> name[i] >> number[i] >> year[i];
}
cin >> inp;
for (i = 0; i < n; i++) {
if (inp == name[i]) {
check = 1;
}
}
if (check == 0)
cout << "Info Not found";
else
cout << "The Entered Name is found";
for (i = 0; i < n; i++)
cout << name[i] << number[i] << year[i];
return 0;
}
After reading an integer from the input stream, you should use cin.ignore(); before reading any strings from the input stream.
cin.ignore(); ignores the "new line" character.
Your modified code:
#include <iostream>
#include <string.h>
using namespace std;
int main() {
string name[100];
long long number[100];
int year[100], i, n, check = 0;
string inp;
cin >> n;
cin.ignore();
for (i = 0; i < n; i++) {
cin >> name[i] >> number[i] >> year[i];
cin.ignore();
}
cin >> inp;
for (i = 0; i < n; i++) {
if (inp == name[i]) {
check = 1;
}
}
if (check == 0)
cout << "Info Not found";
else
cout << "The Entered Name is found";
for (i = 0; i < n; i++)
cout << name[i] << number[i] << year[i];
return 0;
}
Read more here: Using getline(cin, s) after cin
At the end of the program, you are ignoring the value of check and just printing everything in your list regardless of whether the entered name was actually found or not.
Try something more like this instead:
#include <iostream>
#include <string>
using namespace std;
int main() {
string name[100];
long long number[100];
int year[100], i, n, idx = -1;
string inp;
cin >> n;
for (i = 0; i < n; i++) {
cin >> name[i] >> number[i] >> year[i];
}
cin >> inp;
for (i = 0; i < n; i++) {
if (inp == name[i]) {
idx = i;
break;
}
}
if (idx == -1)
cout << "Info Not found";
else {
cout << "The Entered Name is found: ";
cout << name[idx] << " " << number[idx] << " " << year[idx];
}
return 0;
}
You might also consider using a struct to organize your data better, eg:
#include <iostream>
#include <string>
using namespace std;
struct myData {
string name;
long long number;
int year;
};
istream& operator>>(istream &is, myData &data) {
is >> data.name >> data.number >> data.year;
return is;
}
ostream& operator<<(ostream &os, const myData &data) {
os << data.name << " " << data.number << " " << data.year;
return os;
}
int main() {
myData data[100];
int i, n, idx = -1;
string inp;
cin >> n;
for (i = 0; i < n; i++) {
cin >> data[i];
}
cin >> inp;
for (i = 0; i < n; i++) {
if (inp == data[i].name) {
idx = i;
break;
}
}
if (idx == -1)
cout << "Info Not found";
else
cout << "The Entered Name is found: " << data[idx];
return 0;
}
My target is to validate input value before add it into array. Current code used:
int main()
{
int temp;
int arr[5];
for(int i = 0; i < 5; i++)
{
// validate here
cin >> arr[i];
}
return 0;
}
and my validation method:
int validateInput(string prompt)
{
int val;
while (true)
{
cin.clear();
cin.sync();
cout << prompt;
cin >> val;
if (cin.good() && val >= -50 && val <= 50)
{
break;
}
else
cin.clear();
cout << "Invalid input! number must be between -50 and 50" << endl;
}
return val;
}
How is that possible?
Your validateInput should just deal with validation: it should answer "is x valid or not valid?"
bool validateInput(int x)
{
return val >= -50 && val <= 50;
}
When reading from stdin, use validateInput and branch accordingly:
for(int i = 0; i < 5; i++)
{
int temp;
cin >> temp;
if(cin.good() && validateInput(temp))
{
arr[i] = temp;
}
else
{
cout << "Invalid input! number must be between -50 and 50" << endl;
// handle invalid input
}
}
If you want to further abstract the idea of "only reading valid numbers from std::cin", you can use an higher-order function:
template <typename TFValid, typename TFInvalid, typename TFInvalidIO>
decltype(auto) processInput(TFValid&& f_valid, TFInvalid&& f_invalid, TFInvalidIO&& f_invalid_io)
{
int temp;
cin >> temp;
if(!cin.good())
{
// Invalid IO.
return std::forward<TFInvalidIO>(f_invalid_io)();
}
if(validateInput(temp))
{
// Valid IO and datum.
return std::forward<TFValid>(f_valid)(temp);
}
// Valid IO, but invalid datum.
return std::forward<TFInvalid>(f_invalid)(temp);
}
Usage:
for(int i = 0; i < 5; i++)
{
processInput([&](int x){ arr[i] = x; },
[](int x){ cout << x << " is invalid"; },
[]{ cout << "Error reading from cin"; });
}
If you want more genericity you can also pass validateInput and the type of input as an additional parameters.
Vittorio's answer above is spot on. For completeness, if you want exactly 5 elements:
#include <string>
#include <iostream>
using namespace std;
bool validateInput(int inValue)
{
return inValue >= -50 && inValue <= 50;
}
int main()
{
int _arr[5];
int _currentIdx = 0;
int _tmp;
while (_currentIdx < 5)
{
cin >> _tmp;
if (validateInput(_tmp))
{
_arr[_currentIdx] = _tmp;
_currentIdx++;
}
else
{
cout << "Invalid input! number must be between -50 and 50" << endl;
}
}
}
replace
cin >> temp;
with
arr[i] = validateInput("some str");