: error C2064: term does not evaluate to a function taking 1 arguments
: error C2227: left of '->name' must point to class/struct/union/generic type
how do i fix this so this error doesn't happen
for(int index = 0; index < (numStudents); index++)
{
if (student(index + 1)->score >= 90 )
student(index + 1)->grade = 'A';
else if (student(index + 1)->score >= 80 )
student(index + 1)->grade = 'B';
else if (student(index + 1)->score >= 70 )
student(index + 1)->grade = 'C';
else if (student(index + 1)->score >= 60 )
student(index + 1)->grade = 'D';
else
student(index + 1)->grade = 'F';
}
heres the structure:
struct StudentType
{
string name;
int score;
char grade;
};
and here is the pointer :
StudentType* student;
My guess is that you need to do
student[index + 1]
instead of
student(index + 1)
You should really specify what is student so people can answer this question.
From your comment answers it appears student is a pointer. In that case student(index + 1) is not valid syntax. I think you mean student[index + 1].
Further critique - 1 based arrays in C are bad form. Consider starting at 0 like everyone else in C
Is students a method/function or an array? Perhaps this is what you mean:
for (int index = 0; index < numStudents; index++) {
// ensure that we don't go past the end of the student array
if (index + 1 == numStudents) break;
if (student[index + 1]->score >= 90) student[index + 1]->grade = 'A';
else if (student[index + 1]->score >= 80) student[index + 1]->grade = 'B';
else if (student[index + 1]->score >= 70) student[index + 1]->grade = 'C';
else if (student[index + 1]->score >= 60) student[index + 1]->grade = 'D';
else student[index + 1]->grade = 'F';
}
Though I don't understand why you are avoiding the first student (students[0]). You know arrays are zero-based, right? You could achieve the same effect without all of the 'index + 1' if you initialize the index variable to 1 instead of 0.
I'm pretty sure what it's trying to tell you is that the student function doesn't take just one argument. Since it couldn't actually call the function the second part of the error then tells you it can't be used to the left of ->
It's hard to say without more detail (specifically, what is student), but assuming student is a vector or array most likely you meant to say student[index + 1] instead of using the parentheses.
Related
I am trying to solve the problem "Kick Start" from round G from last year. In short you need to count the number of appearances of the substring "KICK....START" with any number of characters in between the two words. The input is a string with only capital letters.
I wrote some code and the sample works fine. But after sending the code I get a runtime error in the first test case. I Have tried many different things like using the at() function from string, or substring but nothing has solved the problem. Any ideas what the problem could be?
So my function that gets the error looks like this:
long long fragment_count = 0;
long long open_count = 0;
string text;
cin >> text;
for (int i = 0; i < text.size() - 4; ++i) {
if (text[i] == 'K') {
if (text[i + 1] == 'I' && text[i + 2] == 'C' && text[i + 3] == 'K') {
open_count++;
}
} else if (text[i] == 'S') {
if (text[i + 1] == 'T' && text[i + 2] == 'A' && text[i + 3] == 'R' && text[i + 4] == 'T') {
fragment_count += open_count;
}
}
}
cout << fragment_count << "\n";
Edit: Outside of this function is the template I use for the kickstart problems. At the start there are two lines that I read that speed up io:
ios::sync_with_stdio(0);
cin.tie(0);
Then I just read a number with cin, that signifies the test cases and call the fucntion above for every test case.
text.size() is of type std::size_t, which is an unsigned type. If the length of the text is less than 4, text.size() - 4 wraps around and becomes a huge positive number.
Replace the comparison with i + 4 < text.size().
I am trying to solve a problem here. I have two arrays which needs to be added and produce a desired output. {'A','A','A','B','A','X','M'} and {'A', 'B', 'A', 'A', 'B', 'A', 'B', 'B'}. Value of A is 10 and B is 20.
If A or B are repeated consecutively then the bonus numbers are added which is 10. I need to get an output of total score of 90 for first and 140 for second. How Can I write the perfect code.
My Code here is as below: The output should be 90 for the first array and 140 for a second. X and M have 0 value. SO don't mind them.
Here First character in array is A so score is 10
Second is also A so now 10 for A and as the previous was A so 10+10 = 20
Now as third one is also A add another 10 to previous score as 10 + 20(as last two were two as well ) ie. 30
Now fourth element in array is B so its not A and Streak is Broken so add Score For B which is set as 20
For Fifth Element we have A but as previous was Blue so no streak bonus and score of A was reset so add 10 now
Add 0 for X and M element
hence total score is 10+20+30+20+10 = 90
#include <iostream>
int main()
{
int currentEScore = 0;
int TotalScore = 0;
char Box[] = { 'A', 'A', 'A', 'B', 'A', 'X', 'M' };
// A B A R A R A A
for (int i = 0; i < Box[i]; i++) {
if (Box[i] == 'A') {
currentEScore = 10;
if (Box[i - 1] == Box[i]) {
currentEScore += 10;
TotalScore = TotalScore + currentEScore;
}
else {
TotalScore = TotalScore + currentEScore;
}
}
else if (Box[i] == 'B') {
currentEScore = 20;
if (Box[i - 1] == Box[i]) {
currentEScore += 10;
TotalScore = TotalScore + currentEScore;
}
else {
TotalScore = TotalScore + currentEScore;
}
}
else {
currentEScore = 0;
}
}
std::cout << TotalScore;
}
First things first: You are accessing the array out-of-bounds in the first iteration (if (Box[i - 1] == Box[i])). That is undefined behavior and strictly speaking all your code is meaningless, because a compiler is not mandated to do anything meaningful with invalid code. It just happens that this does not affect the outcome of your code. This is the worst incarnation of undefined behavior: It appears to work. You need to fix that.
Next, your loop reads for (int i = 0; i < Box[i]; i++) { and the condition cannot be correct. Again this makes your loop access the array out-of-bounds. I am a bit puzzled how this worked (I didnt realize it myself first). This also has to be fixed! I suggest to use a std::string for character arrays. It is much less error prone and it has a size() method to get its size.
The above issues didn't affect the output (nevertheless they have to be fixed!), so now lets look at the logic of your code. But first a disclaimer: Really the best advice I can give you is to not continue reading this answer. The problem you are facing is a good opportunity to learn how to use a debugger. That is a skill you will need always. If you still decide to read the following, then at least you should try to forget everything this answers says and go trough the same process on your own, by either using a debugger or a piece of paper and a pen.
Lets go step by step in your first example { 'A', 'A', 'A', 'B', 'A', 'X', 'M' }
first character is A
if (Box[i] == 'A') -> condition is true
currentEScore = 10; -> currentEScoe == 10
(we ignore the out-of-bounds for a moment)
TotalScore = TotalScore + currentEScore; -> TotalScore == 10
next character is A
if (Box[i] == 'A') -> condition is true
currentEScore = 10; -> currentEScore == 10
if (Box[i - 1] == Box[i]) -> yes
currentEScore += 10; -> currentEScore == 20
TotalScore = TotalScore + currentEScore; -> TotalScore == 10+20 == 30
next character is A
if (Box[i] == 'A') -> condition is true
currentEScore = 10; -> currentEScore == 10 -> stop... this is wrong !
You are resetting the bonus score on each character and then only check for the previous one. The effect is that you never give bonus more than 20.
Solution: Fix the out-of-bounds access and only reset the bonus when the character is different from the last. Also the code can be simplified a bit, by realizing that the bonus is indepenent from whether the character is A or B. You only have to check if it is the same as the last, hence calculating the bonus and adding points for A and B can be done seperately:
#include <iostream>
#include <string>
int main()
{
int bonus_increment = 10;
int bonus = 0;
int score_A = 10;
int score_B = 20;
int TotalScore = 0;
std::string Box{"AAABAXM"};
for (size_t i = 0; i < Box.size(); i++) {
// determine bonus
if ( i > 0 && Box[i] == Box[i-1] ) {
bonus += bonus_increment;
} else {
bonus = 0;
}
// accumulate score
if (Box[i] == 'A') {
TotalScore += score_A + bonus;
} else if (Box[i] == 'B') {
TotalScore += score_B + bonus;
}
}
std::cout << TotalScore;
}
Don't forget what I said above. You waste this exercise if you simply copy this code and assume that you completed the exercise by doing so.
Here's I have made some changes with your code with minimal optimization.
It'll work for you. Please check it and let me know it's working or not.
#include <iostream>
int main()
{
int currentEScore = 0;
int TotalScore = 0;
char Box[] = {'A','A','A','B','A','X','M'};
// A B A R A R A A
for (int i = 0; i < Box[i]; i++) {
if (Box[i] == 'A') {
if (Box[i - 1] == Box[i]) {
currentEScore += 10;
}
else {
currentEScore = 10;
}
}
else if (Box[i] == 'B') {
if (Box[i - 1] == Box[i]) {
currentEScore += 10;
}
else {
currentEScore = 20;
}
}
else {
currentEScore = 0;
}
TotalScore = TotalScore + currentEScore;
}
std::cout << TotalScore;
}
Alright, so I have two questions and if anyone can help me out I would greatly appreciate it! This is my first programming class, so it would also be my first C++ class and I'm a bit stuck.
So I created a Caesar cipher that shifts the string that the user inputs to the right by a pseudo random number between 8-15. What the complete program needs to do is give the number it is shifted by at the beginning, followed by the encrypted string. If there are spaces in the string that are inputed, they need to take the letter/number that is before the space and shift it by 4. I need to terminate the encryption with an '&' character and that is followed by a '#' character and then the number location of the first space followed by another '#' character and another location of a second space if there is one and so on.
So for example, if I were encrypting a string that was being shifted by 9 and said:
Hello World 123
It should look like this when encrypted:
9qnuuxsfxaumh012#12
My first and more important question. I can't figure out how to make the program output the '#' character followed by the number that tells the location of the space. I've thought of maybe doing some kind of loop that reads the string but I'm coming up blank. If I could get some advice that would be great as this is the only part holding me up from turning this in.
My second question comes from a little confusion within my own code that I would love an English interpretation on how it works since I don't understand it myself. I was first using just for loops to make it so that the character 'z' would wrap back around to 'a' but no matter what I did, I kept getting it to only wrap around after a '{' character which is the next character after 'z' on the ascii table. So I decided to change my method and I read on wikipedia under "Caesar cipher" that you could use a modulus. So I used the equation they gave me which was E(x) = (a + b) mod 26. Well it didn't work. So I started to do a google search and saw 2 different posts where people subtracted the character 'a' and then added the chracter 'a' back on at the end as well as added the variable to itself with +=. So I put it in and it worked.
It looks like this:
output += ((input[count] - 'a' + n) % 26) + 'a';
and I thought it would look like this after reading the wiki and it not working when i put this in
output = ((input[count] + n) % 26)
Same goes for wrapping the numbers as well:
output += ((input[count] - '0' + n) % 10) + '0';
So if someone could explain to me why I am adding output to itself as well as subtracting 'a' in the beginning and then re-adding 'a' at the end so I could understand what's going on. I really don't like having code in a program that I'm going to turn in that I don't even understand myself.
Anyways, I'm sorry for the long read, I just thought I would explain what's going on and what I need clearly so that anyone willing to help would completely understand what I'm saying without me having to follow up with a second post explaining.
And finally here's the full program that I have written:
#include <ctime>
#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
using namespace std;
//random number generator between 8 and 15
int random()
{
int value;
value = rand() % (18 - 10) + 8;
return value;
}
int main()
{
//Give random() a seed
srand(time(NULL));
//Declare variables
string input;
int length;
int n = random();
string output;
//Program
cout << "Enter your secret message: " << endl;
getline (cin, input);
cout << n;
length = input.length();
for (int count = 0; count < length; count++)
{
input[count] = tolower(input[count]);
if (input[count] >= 'a' && input[count] <= 'z')
{
output += ((input[count] - 'a' + n) % 26) + 'a';
}
if (input[count] >= '0' && input[count] <= '9')
{
output += ((input[count] - '0' + n) % 10) + '0';
}
else if(input[count] == ' ')
{
if (input[count - 1] >= 'a' && input[count - 1] <= 'z')
{
output += ((input[count - 1] - 'a' + 4) % 26) + 'a';
}
else if (input[count - 1] >= '0' && input[count - 1] <= '9')
{
output += ((input[count - 1] - '0' + 4) % 10) + '0';
}
cout << output;
}
}
cout << output << endl;
return 0;
}
Thanks so much for anyone willing to help!
Two answer the second question:
input[count] - 'a'
This gives you 0 for the letter a, 1 for the letter b, ... 25 for the letter z.
input[count] - 'a' + n
Then you add the number n. Having "a" as an input and being n==2 you will get a 3. But for a "z" as input you will get a 27.
To solve the problem you use the modulus:
(input[count] - 'a' + n) % 26
The result is a 1 for the "z".
((input[count] - 'a' + n) % 26) + 'a'
Now you transfer the number from 0 to 25 back to the corresponding ASCII code.
The point of the seemingly odd expansion is to do the following:
Create a number from 0..25: input[count] - 'a'
Adjust that number by adding your shift amount: + n
Modulo the result with 26 to wrap overflow of 26+ back into 0..25: % 26
And finally, add that result back to the base character: +a``
Your idea of a shortcut:
output = ((input[count] + n) % 26)
simply takes the ascii value of the input char, adds the shift, then modulo 26. The result is a value in 0..25, nowhere near the range of 'a'..'z'.
And before you think just adding 'a' would work, it isn't that simple. For example, suppose you had a shift of 9 and in input char of 'z'
The presented formula that works: (ch - 'a' + n) % 26 + 'a'
(('z' - 'a' + 9) % 26 + 'a'
((122 - 97 + 9) % 26 + 97
34 % 26 + 97
8 + 97
105, the ascii value of 'i'
Your formula, with 'a' adjustment: (ch + n) % 26 + 'a'
('z' + 9) % 26 + 'a'
(122 + 9) % 26 + 97
131 % 26 + 97
1 + 97
98, the ascii value for 'b'.
The problem is the distance from the beginning of the char sequence that is being modulo-adjusted is never accounted for in the modulo reduction. Thus the reason for the formula you find odd.
Regarding how to accumulate a list of space locations. a ostringstream will make that trivial, as would a std::vector<int>. An example of the former looks like this:
#include <ctime>
#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
#include <sstream> // for std::ostringstream
using namespace std;
int main()
{
//Give random() a seed
srand(static_cast<unsigned>(time(NULL)));
//Declare variables
string input;
int length;
int n = rand() % (18 - 10) + 8;
string output;
//Program
cout << "Enter your secret message: " << endl;
getline (cin, input);
cout << n;
// string stream to hold the list of space locations.
std::ostringstream oss;
length = input.length();
for (int count = 0; count < length; count++)
{
input[count] = tolower(input[count]);
if (input[count] >= 'a' && input[count] <= 'z')
{
output += ((input[count] - 'a' + n) % 26) + 'a';
}
if (input[count] >= '0' && input[count] <= '9')
{
output += ((input[count] - '0' + n) % 10) + '0';
}
else if(input[count] == ' ')
{
if (input[count - 1] >= 'a' && input[count - 1] <= 'z')
{
output += ((input[count - 1] - 'a' + 4) % 26) + 'a';
}
else if (input[count - 1] >= '0' && input[count - 1] <= '9')
{
output += ((input[count - 1] - '0' + 4) % 10) + '0';
}
// add space location with preamble to string stream
oss << '#' << count;
}
}
// append space accumulated list string to the end after '&'
cout << output << '&' << oss.str() << endl;
return 0;
}
with the following code i have managed to update edit control boxes with values on the click of a button.
void SomeDlg::OnBnClickedValue2Plus2()
{
m_control1.GetWindowText(m_value1);
m_control2.GetWindowText(m_value2);
m_control3.GetWindowText(m_value3);
int a,b,c;
if (m_getcheck.GetCheck() == 1
&& _ttoi(m_value2) < 40
&& _ttoi(m_totalvalue) < 100)
{
a = _ttoi(m_value1);
b = _ttoi(m_value2) + 2;
c = a + b;
GetDlgItem(IDC_EDIT1)->SetWindowText(_itot(b, buff, 10));
GetDlgItem(IDC_EDIT3)->SetWindowText(_itot(c, buff, 10));
}
}
void SomeDlg::OnBnClickedValue3Plus3()
{
m_control1.GetWindowText(m_value1);
m_control2.GetWindowText(m_value2);
m_control3.GetWindowText(m_value3);
int a,b,c;
if (m_getcheck.GetCheck() == 1
&& _ttoi(m_value1) < 40
&& _ttoi(m_totalvalue) < 100)
{
a = _ttoi(m_value1) + 3;
b = _ttoi(m_value2);
c = a + b;
GetDlgItem(IDC_EDIT2)->SetWindowText(_itot(a, buff, 10));
GetDlgItem(IDC_EDIT3)->SetWindowText(_itot(c, buff, 10));
}
}
there are 3 edit controls that update when either plus2 or plus 3 button is pressed. when plus2 is pressed edit1 updates with +2, and when plus3 is pressed edit2 is updated with +3. the 3rd edit control displays the total number of edit1 + edit2 which should have a max of 100 with edit1, and edit2 having a maximum of 40.
however this is not the case, the values won't change if the number is the same or over the limit once the button is pressed, but if the value was 39 and i added 3 it would become 42 instead of 40 before becoming inactive .
how would i go about setting a maximum value for the edit controls?
The problem is that you are checking the values before you modify them.
Simply change both "if" statments like this
if (m_getcheck.GetCheck() == 1
&& (_ttoi(m_value2 ) + 2 ) <= 40
&& (_ttoi(m_totalvalue ) + 2 ) <= 100)
And
if (m_getcheck.GetCheck() == 1
&& ( _ttoi(m_value1 ) + 3 ) <= 40
&& ( _ttoi(m_totalvalue) + 3 ) <= 100)
I also changed the < in <= because you said the MAX value for 1 and 2 is 40 and the total is 100.
i solved my problem by changing
void SomeDlg::OnBnClickedValue3Plus3()
{
m_control1.GetWindowText(m_value1);
m_control2.GetWindowText(m_value2);
m_control3.GetWindowText(m_value3);
int a,b,c;
if (m_getcheck.GetCheck() == 1
&& _ttoi(m_value1) < 40
&& _ttoi(m_totalvalue) < 100)
{
a = _ttoi(m_value1) + 3;
b = _ttoi(m_value2);
c = a + b;
GetDlgItem(IDC_EDIT2)->SetWindowText(_itot(a, buff, 10));
GetDlgItem(IDC_EDIT3)->SetWindowText(_itot(c, buff, 10));
}
}
to this
void SomeDlg::OnBnClickedValue3Plus3()
{
m_control1.GetWindowText(m_value1);
m_control2.GetWindowText(m_value2);
m_control3.GetWindowText(m_value3);
BOOL max1 = 40;
BOOL max2 = 100;
int a,b,c;
a = _ttoi(m_value1) + 3;
b = _ttoi(m_value2);
c = a + b;
if (m_getcheck.GetCheck() == 1
&& a <= max1
&& c <= max2)
{
GetDlgItem(IDC_EDIT2)->SetWindowText(_itot(a, buff, 10));
GetDlgItem(IDC_EDIT3)->SetWindowText(_itot(c, buff, 10));
}
}
this means when the value is 39, and i try to add 3 nothing happens. however, if i add 1 it reaches 40 which was the way i wanted to go in the first place, i just thought it would have been simpler if i had the number change to the max if i tried to add 3 to 39. turns out it was much easier to do it the way i originally intended. thanks for all the help.
I have problem with giving new char valure to array. I don't know why I get sign "<" even when n is 12? My program should change expression int char* tab = "93+" to one value in this case 12.
char* tab = "93+";
int b = sizeof (tab);
char* tmp = new char[b] ;
tmp [b-1] = '\0';
if(isdigit(tab[i]) && isdigit(tab[i+1]) ){
int n;
if(tab[i+2]=='+' || tab[i+2]=='-' || tab[i+2]=='*'){
switch(tab[i+2]){
case '+':
n = (tab[i]-'0') + (tab[i+1]-'0');
break;
case '-':
n = (tab[i]-'0') - (tab[i+1]-'0');
break;
case '*':
n = (tab[i]-'0') * (tab[i+1]-'0');
break;
}
tmp[i] = n+'0'; // I should have 12 but I get <
}
else if (tab[i+2]!='+' || tab[i+2]!='-' || tab[i+2]!='*'){
goto LAB;
}
}
The problem is in this line:
tmp[i] = n+'0'; // I should have 12 but I get <
n is 12, but 12 + '0' != '12', since '12' isn't a character. You're putting into tmp[i] the char whose ascii value is 12 more than '0', which is '<'.
I believe declaring (and treating) tmp as an int would be better for your purposes.
Also note that sizeof (tab) is the same as sizeof (char *), and not sizeof ("93+"), so you're likely to always get b==4 (in 32-bit machines).
You indeed should get '<'. Here is why: tmp is an array of chars. You calculated n to be 12. This is correct. You then added '0' which is 48. 48 + 12 = 60. So you store 60 in tmp[i].
An ASCII 60 is '<'.
You could use an int tmp, and not add the '0', and you would get then 12 in tmp[i].