I tried to use,
if(isalpha(card[i].name))
...
but it says that i can't use it with char type here is my code:
This is the struct:
struct firm {
unsigned egn;
char name[80];
char lastname[80];
char department[80];
unsigned salary;
}card[100];
This is the function where i want to make the check if only letters then to continue:
void enter()
{
int i, n;
char temp[80];
do{
cout<<"Enter how many workers you want to add: ";cin>>n;
}while(!(n>0 && n<101));
for(i=top;i<n;i++)
{/*ЕГН + проверка*/
cout<<"Enter EGN: ";do{
cin>>temp;
if((strlen(temp)!=10))
cout<<"Enter EGN: ";
}while(strlen(temp)!=10);
card[i].egn = (unsigned) atoi (temp);
/*Име Фамилия*/
cout<<"Enter name(only letters): ";cin>>card[i].name;
cout<<"Enter lastname(only letters): ";cin>>card[i].lastname;
cout<<"Enter department: ";cin>>card[i].department;
cout<<"Enter salary: ";cin>>temp;
card[i].salary = (unsigned) atoi (temp);
}
}
When i try to use this kind of code:
cout<<"Enter name(only letters): ";do{
cin>>card[i].name;
if(isalpha(card[i].name))
.....;
and it says that i can't use char in int (isalpha uses int? )
You need to loop over every character of the string, isalpha() only tests a single character.
You cannot use isalpha on an entire char array, only on individual characters. To check that every character in a char array is alphabetical, you can use the std::all_of algorithm:
auto name_begin = std::begin(card[i].name);
auto name_end = std::end(card[i].name);
bool name_alpha = std::all_of(name_begin, std::find(name_begin, name_end, '\0'), std::isalpha);
if (name_alpha) {
std::cout << "It's alphabetical!" << std::endl;
}
If you're using a compiler without the necessary C++11 support, you can do:
char* name_begin = card[i].name;
bool name_alpha = std::all_of(name_begin, name_begin+std::strlen(name_begin), std::isalpha);
if (name_alpha) {
std::cout << "It's alphabetical!" << std::endl;
}
Then the boring way without using the standard library algorithms:
bool name_alpha = true;
for (char* character = card[i].name; *character != '\0'; character++) {
if (!std::isalpha(*character)) {
name_alpha = false;
break;
}
}
if (name_alpha) {
std::cout << "It's alphabetical!" << std::endl;
}
The problem is that name is itself an array of chars, so you need to test each each element something like this:
.....
cout<<"Enter name(only letters): ";do{
cin>>card[i].name;
bool bIsAlpha = true;
for (int j = 0; j < strlen(card[i].name); ++j)
{
if(!isalpha(card[i].name[j]))
{
bIsAlpha = false;
break;
}
}
if (bIsAlpha)
{
.....;
}
Related
I am writing a C++ program for homework, and it needs to count the characters in a char arr[n] string. However, my counter keeps returning the wrong values. I have looked through other answers to similar questions, however, they are not specific to C++ and none of the answers explain the value I am getting.
#include<iostream>
using namespace std;
#include<string.h>
#include <stdlib.h>
class Counter
{
public:
char word[20];
int totChar{ 0 };
void setWord(char word)
{
this->word[20] = word;
}
void setCount(int totChar)
{
this->totChar = totChar;
}
int getLength()
{
return totChar;
}
void charCount()
{
int n = 0;
for (int i = 0; word[i] != '\0'; i++) {
if (word[i] != '\0')
{
n++;
}
}
setCount(n);
}
};
int main()
{
char text[20];
cout << "Enter the string:" << endl;
cin >> text;
Logic input;
input.setWord(text[20]);
input.charCount();
// input.resetWord();
cout << input.getLength();
}
So it seems you haven't figured out how arrays and C strings work in C++ yet.
void setWord(const char* word)
{
strcpy(this->word, word);
}
and
Logic input;
input.setWord(text);
Your code is a bit weird, I guess you are just experimenting, but I think those two changes should make it work.
So writing a palindrome with pointers and boolean. I have it working with a single word but then I began building it to work with a sentence. The problem is I am unsure how to keep the new modified sentence after making it lowercase and getting rid of the spaces for it to return whether it is or isn't a palindrome. It keeps returning the palindrome as false and when I went to check why I see that the program ignores the modification and kept the original string. I can't use "&" on the parameter as I tested it out. Any hints or takes on what I can do to keep the new modified string?
int main()
{
userInput();
return 0;
}
void userInput()
{
char str[90];
std::cout<<"Please enter a string to check if it is a palindrome: ";
std::cin.getline(str, 90);
modifyString(str);
}
void modifyString(char *string)
{
int count = 0;
for (int i=0; i<strlen(string); i++)
{
putchar(tolower(string[i]));
}
for (int i = 0; string[i]; i++)
{
if (string[i] != ' ')
{
string[count++] = string[i];
}
}
string[count] = '\0';
std::cout<<string<<std::endl;
results(string);
}
bool checkPalindrome(char *string)
{
char *begin;
char *end;
begin = string;
end = (string + strlen(string)-1);
while(begin != end)
{
if ((*begin) == (*end))
{
begin ++;
end--;
}
else
{
return false;
}
}
return true;
}
void results(char *string)
{
bool isItPalindrome;
isItPalindrome = checkPalindrome(string);
if( isItPalindrome == true)
{
std::cout<<"\nCongrats, the string is a palindrome!";
}
else
{
std::cout<<"\nThis string is not a palindrome.";
}
}
For starters this definition of main
int main()
{
userInput();
return 0;
}
does not make a sense. According to the function name main the function should perform the main task that is to output whether the entered sentence is a palindrome or not.
This for loop
for (int i=0; i<strlen(string); i++)
{
putchar(tolower(string[i]));
}
does nothing useful. It just outputs the string in the lower case.
This statement
end = (string + strlen(string)-1);
can invoke undefined behavior if an empty string was passed.
This while loop
while(begin != end)
{
if ((*begin) == (*end))
{
begin ++;
end--;
}
else
{
return false;
}
}
also can invoke undefined behavior for a string containing an even number ofo characters because after this if statement
if ((*begin) == (*end))
{
begin ++;
end--;
}
if the two adjacent characters are equal then begin after incrementing will be greater than end after its decrementing. And as a result the loop will continue its iteration.
In general the approach when the original string is changed is just a bad approach.
Your program has too many functions. It is enough to write one function that will determine whether the passed string is a palindrome or not.
Here is a demonstrative program.
#include <iostream>
#include <cstring>
#include <cctype>
bool checkPalindrome( const char *s )
{
const char *t = s + std::strlen( s );
do
{
while ( s != t && std::isspace( ( unsigned char )*s ) ) ++ s;
while ( s != t && std::isspace( ( unsigned char )*--t ) );
} while ( s != t &&
std::tolower( ( unsigned char )*s ) == tolower( ( unsigned char ) *t ) &&
++s != t );
return s == t;
}
int main()
{
const size_t N = 100;
char s[N] = "";
std::cout << "Please enter a string to check if it is a palindrome: ";
std::cin.getline( s, N );
std::cout << '\n';
if ( checkPalindrome( s ) )
{
std::cout << "Congrats, the string is a palindrome!\n";
}
else
{
std::cout << "This string is not a palindrome.\n";
}
return 0;
}
Its output might look like
Please enter a string to check if it is a palindrome: 1 23 456 6 54 321
Congrats, the string is a palindrome!
Okay, I solved it!
As one of the users on here brought up a point that my lowercase did not modify the string and only prints it out. I try my best to solve the problem and I think I found the solution and everything works perfectly fine. comment back to debug it if you like to see how it looks but what I did was create a for loop again for the lower case but made another pointer with it. here how it looks.
for (char *pt = string; *pt != '\0'; ++pt)
{
*pt = std::tolower(*pt);
++pt;
}
Now that definitely changes the string into a lower case and keeps it as a lower case.
so now the modified function looks like this and ready to take any sentence palindrome you give it. Example: A nUt fOr a jAr of tUNa. We make this all lowercase and take out space and boom palindrome and return true.
void modifyString(char *string)
{
int count = 0;
for (char *pt = string; *pt != '\0'; ++pt)
{
*pt = std::tolower(*pt);
++pt;
}
for (int i = 0; string[i]; i++)
{
if (string[i] != ' ')
{
string[count++] = string[i];
}
}
string[count] = '\0';
//take out the forward slash below to see how it looks after being modified
// std::cout<<std::endl<<string<<std::endl;
results(string);
}
I'm trying to have a string only work if it matches an int in the list.
Code
int Keys[] = { 23454563, 1262352, 634261253, 152352 };
string key;
int main()
{
cout << ("Please Enter Key: ");
cin >> key;
if (key = Keys)
{
//Activate Code
}
else
{
//Don't activate
}
}
I've tried searching around and I can't find any valid methods. I did try
if (sscanf(key.c_str(), "%d", &Keys) == 1)
^this works, but any number works and that isn't what I'm looking for.
hmm. First, I don't know why you have to input a 'string' instead of an 'int'. Also, why do you make 'key' a global variable? Just put it inside 'main'. Moreover, 'Keys' is an array and you can't compare a variable with an array.You have to search through the array using a loop.
My prefer answer
#include <iostream>
int main()
{
constexpr int Keys[] { 23454563, 1262352, 634261253, 152352 };
int key;
bool isNumberMatched {false};
std::cout << ("Please Enter Key: ");
std::cin >> key;
for (auto number : Keys) //Search through Keys array
{
if (key == number)
isNumberMatched = true;
break;
}
if (isNumberMatched)
//Activate Code
else
//Don't activate
}
I'm trying to do parsing to some input string reactions read from file at formula :2W+B=8A+10Z, I'm not interested in characters i need only to split and extract the integer values to put them in a vector i.e vector associated with the reaction here is :[2 1 8 10]
i thought about many things: std::strtok(),isdigital(),find_first_of() but they all didn't work for integer values ... can any body help ??
here my try:
int main()
{
std::string input;
std::getline(std::cin, input);
std::stringstream stream(input);
while(1) {
int n;
stream >> n;
char * pch;
pch = strtok (input," ");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.");
}
}
}
This will do what you want in this particular case. However, i suggest that you look into regex to parse your equation better. You may want to consider all possible cases for your input. This includes \,-,* and other operators that you may want to add in your equation. Also, I'm assuming variables in your equation has only one character.
int main()
{
string input;
getline(std::cin, input);
stringstream stream(input);
char tmp[256];
const char *in = input.c_str();
char str[256];
strcpy(str,in);
int x;
tmp[0]='\0';
char c;
vector<int> vec;
//Scan for the digit
//if it is, store the rest of the string back to str
//if it isn't, store the part of the string before a digit to tmp
while (sscanf(str,"%d%s",&x,str) || sscanf(str,"%[^0123456789]%s",tmp,str) > 1)
{
//check if tmp has the form [variable name]+[a string]
//a string can include another variable name and an operator, = in this case
while(sscanf(tmp,"%c+%[^0123456789]",&c,tmp) > 1)
vec.push_back(1);
if (tmp[0]=='\0')
vec.push_back(x);
tmp[0]='\0';
}
//just in case there're more special cases
while(sscanf(str,"%c+%[^0123456789]",&c,str) > 1)
vec.push_back(1);
for(int i = 0; i < vec.size(); i++)
cout << vec[i] << endl;
}
Output:
2
1
8
10
See comments for explanation.
EDIT
Be careful when you have a special case 2W+B=8A+10Z+C+D. Notice the last C D should both have coefficients 1. This could happen in the middle of the equation too.
Here is another solution:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
string equ;
vector<int> digits;
cout << "enter your equation: \n";
cin >> equ;
for (auto i : equ)
{
if (isdigit(i))
{
digits.push_back(stoi(string{i}));
}
}
for (auto& i : digits)
{
cout << i << endl;
}
system("pause");
return 0;
}
You could simply do something like this, for comments see code
#include <iostream>
#include <string>
#include <vector>
std::vector<int> Split(std::string str)
{
std::vector<int> result; // will contain the different ints
// set pointer to first character in the string
char const* pch = str.c_str();
std::string digit; // buffer to keep digits if more than one
int sign = 1; // each number has a sign -1 or 1
for (; *pch; ++pch)
{
if (std::isdigit(*pch)) // if a digit, put in temp buffer
{
digit += *pch;
}
else if (std::isalpha(*pch)) // if not a digit evaluate the ones we have
{
if (digit.empty()) // none so assume 1 before letter e.g. W+2B
{
result.push_back(1*sign);
}
else
{
result.push_back(stoi(digit)*sign);
digit = "";
}
}
else // determine sign of number
{
digit = "";
if (*pch == '+')
{
sign = 1;
}
else if (*pch == '-')
{
sign = -1;
}
}
}
return result;
}
int main()
{
using namespace std;
string expr{"-2W+B=-8A+10Z"};
auto digits = Split(expr);
for (auto& digit : digits)
{
cout << digit << endl;
}
return 0;
}
I am having trouble with this program. I'm not so familiar with the functions strcpy and strcmp.
Can any professionals help me or give me some advice?
As others have stated, choose either C-style strings or C++ std::string.
I highly recommend not creating a separate function for searching the string array because passing arrays to functions is difficult for beginners.
Try something like:
#include <string>
#include <iostream>
#define MAX_NAMES 16
int main(void)
{
std::string name_container[MAX_NAMES];
unsigned int names_in_container = 0;
while (1)
{
std::string name_from_user;
std::cout << "Enter name: ";
if (!getline(std::cin, name_from_user))
{
break; // Exit from the "while" loop
}
// Search the name container for the name.
unsigned int array_slot = 0;
bool name_found = false;
for (array_slot = 0; array_slot < names_in_container; ++array_slot)
{
if (name_from_user == name_container[array_slot])
{
std::cout << "\nName exists in slot " << array_slot << "\n";
name_found = true;
}
}
if (!name_found)
{
if (array_slot >= MAX_NAMES)
{
std::cout << "Name container full, cannot add name.\n";
break;
}
else
{
name_container[names_in_container] = name_from_user;
++names_in_container;
}
}
}
}
Please recognize that you are comingling C++ string (std::string) and the C library strcpy and strcmp functions. They are two different things. You probably need to look at some of the methods available under the std::string class (.c_str, =, ==),
Here are a few comments, and suggestions,
void search (string sid[], //do you intend to pass a string sid, or an array of srings?
string name[], //do you intend to pass a string name, or an array of strings?
int No_of_data_input)
{
char id[100];
string input;
char namename[100];
//comments ignored/removed
//these two lines declare the function strcpy
char * strcpy ( char * namename, const char * name );
char * strcpy ( char * id, const char * sid );
//do you intend to copy name and sid instead?
//do you want: strcpy(namename, name[i].c_str() );
//do you want: strcpy(id, sid[i].c_str() );
//if so, you want to do this inside your loop on i, below,
int sameName=0;
int j=0;
cout<<"enter id or name";
getline(cin,input); //you probably want: cin >> input;
//do you intend to declare the function strcpy yet again?
char * strcpy ( char * inputinput, const char * input );
//or do you intend to copy input to some char[]?
//you probably want: strcpy( inputinput, input.c_str() );
//you probably want number of elements in sid[] or name[]?
for(int i=0;i<4;i++){
//you probably want sid[i] here
if ((strcmp(inputinput,id[i])==0) || (strcmp(inputinput,name[i])==0)){
//you could rewrite this as:
//if( (input == id[i]) || (input == name[i]) ){
sameName++;
j=i;
} //do us a favor
} //make the ending braces clearer
cout<<sameName;
if (sameName==0)
cout<<"No student found: ";
else if (sameName==1)
cout<<"student found: "<<name[j]<<endl<<id[j];
else if (sameName>1)
cout<<"More than one student found,please enter id";
}
Do you want (or need) to comingle std::string and strcpy, strcmp?
Editing your code, you might want to just use std::string,
void search (string sid[], //do you intend to pass an array of srings?
string name[], //do you intend to pass an array of strings?
int count)
{
string input;
int same=0;
int j=0;
cout<<"enter id or name";
cin >> input;
//you probably want number of elements in sid[] or name[]?
for(int i=0;i<count;++i){
if( (input == sid[i]) || (input == name[i]) ){
++same;
j=i;
}
}
cout<<"same: "<<same<<endl;
if (same==0)
cout<<"No student found: "<<endl;
else if (same==1)
cout<<"student found: "<<name[j]<<","<<id[j]<<endl;
else if (same>1)
cout<<"More than one student found,please enter id"<<endl;
}
Maybe you could use a vector of strings? Then you could use an iterator on the vector.
You may have more success if you define a student record (containing sid and name), and pass a vector of student records. Then use an iterator over that vector -- read about vector and iterators.