I am making a program to input a student's information (name and birthday) and his/her grade in percent, and will display it, including letter grade. It was working fine before I wanted to verify if the person's name had a number. The errors I am getting occur on line 45 and 63. The error is
"No matching function for call to 'verifyString'"
Line 43, you declare a variable of type constchar *, but the function verifyString has a parameter of type char *.
Since the function does not modify the string, you can make that clear in the function declaration, by making the parameter a const.
Change lines 26 and 136
bool verifyString (constchar *)
Implicit conversion loses integer precision: 'size_t' (aka 'unsigned long') to 'int'
This is usually harmless, it is a warning that the signed integer uses one less bit of precision than an unsigned integer (the most significant bit is used for the sign). A simply fix is to change lines 139, 140
1 2
int i = 0;
int length = strlen(pass);
to
1 2
size_t i = 0;
size_t length = strlen(pass);
size_t is defined in one of the header files, usually an alias for an unsigned integer large enough to hold any required value, it is used to represent values which are the size of something.
verify that the name doesn't contain special characters
Would it be correct to assume that only alphabetic characters (a-z and A-Z) are permitted? What about spaces? Are any other characters allowed?
If it is just alphabetic, then use the isalpha() function. The logic will be the opposite though, since that would be a switch from checking what is not wanted , to checking what is wanted.
1 2 3 4 5 6 7
bool verifyString (const std::string & s)
{
for (char c : s)
if (!isalpha(c)) // note the ! (not)
returntrue;
returnfalse;
}
The & operator here is used to declare a reference.
Effectively it makes the variable an alias for another variable.
In the example above, it is entirely optional, I was trying to make the code a little shorter and more readable, it simply allows in this case something with a long and cumbersome name to be given a shorter name, purely for convenience.
Here's a brief example. Below there is an integer variable a. The variable c is not really a separate integer, it is another way of referring to a.
#include <iostream>
usingnamespace std;
int main()
{
int a = 4;
int &c = a;
cout << "a = " << a << "\nc = " << c << "\n\n";
c = 16;
cout << "a = " << a << "\nc = " << c << "\n\n";
a = 3;
cout << "a = " << a << "\nc = " << c << "\n\n";
}
At line 5 the if tests for percent > 89. If the condition is true, the grade is set to 'A'.
If it is false, we know that percent is <= 89. Hence in the else condition at line 8, the test for
89 >= student[i].courseGrade.percent
is unnecessary duplication - and can even be a cause of errors, sometimes this style of coding can leave gaps where some values are not accounted for.
It is good practice to remove those extra checks, the code will look like this:
Looks like the date entry section has grown a bit complex. There are error messages issued in three different places, lines 97, 104, 111, and no less than three while loops. That suggests something could be simplified. There are a couple of minor issues, the call to isleapyear() at line 101 doesn't do anything useful (nothing is done with the result).
Another problem there are two sets of cin >> statements here:
That actually means the program doesn't lock up. What happens is the user enters the date at line 105, and the program sits there waiting for it to be entered all over again at line 107.
The logic needs a bit of a rethink, there are two main situations which need to be tested
1. invalid input such as alphabetic character when number is expected.
2. valid input but the date itself is not valid
Thanks. Though my goal here is to try to help you learn some of this stuff, if your teacher is pleased but you are still confused then I failed. Time will tell I suppose...