Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm experimenting with classes and I'm wondering why I'm getting an error saying "calvin" is not defined in the scope. Thanks.
#include <iostream>
#include <string>
using namespace std;
class people
{
public:
string name;
int age;
};
int main()
{
people peeps[10];
peeps[1].name = calvin;
peeps[1].age = 21;
cout << peeps[1].name << peeps[1].age;
}
Without quotes, calvin is a variable (which is undefined). You should make it a literal (i.e. "calvin").
firstly i would suggest if you had made and age private class members and also create get name/age and set name/age member functions to protect your data and finally
peeps[1].name = calvin;
//calvin is an undefined variable
you should have used string literals for example
peeps[1].name = "calvin";
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
I'm working on this function for one of my classes and my pass count works just fine, however, my fail count ALWAYS prints out 12. I've been reading my code top to bottom and just can't seem to find the problem.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
string passAndFailCount(string grades){
int pass_count;
int fail_count;
istringstream myStream(grades);
string grade;
while(myStream>>grade){
int i_grade=stoi(grade);
if (i_grade>=55){
++pass_count;
}
else{
++fail_count;
}
}
cout<<"Pass: "<<pass_count<<endl<<"Fail: "<<fail_count<<endl;
}
int main()
{
string grades;
getline(cin, grades);
passAndFailCount(grades);
}
Your problem are uninitialized variables.
int pass_count = 0;
int fail_count = 0;
and you're set.
For an explanation. Non-global variables (which automatically get initialized to 'default' (0) as per the standard), automatic and dynamic variables (like the one you are using), assume the value of 'whatever was in memory at the time of allocating the variable'.
Memory is never empty, there's always 'something' written in there.
So the memory of your fail_count variable just 'happened to be' 12 during allocation, which is why you start with that value. This value can be anything within scope.
By explicitly assigning a variable, you 'initialize' the variable, putting it into a defined state (0) and your program should start working as expected.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I need to compare a character in a string that is in an array with another string. This is a functioning but simple version of my problem:
#include <iostream>
using namespace std;
int main() {
string a_ray[1] = {"asd"};
if (a_ray[0][0] == "a") {
bool a;
}
return 0;
}
Error message: ISO C++ forbids comparison between pointer and integer [-fpermissive]|
What causes this? And how can I do what I want to do in the correct way?
Thank you in advance!
Since you are comparing against a character, your code should be
if (a_ray[0][0] == 'a')
You are trying to compare a character with a character array, hence the error message.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
A very short program
#include <iostream>
using namespace std;
int main()
{
cin>> int x;
return 0;
}
it's not compiling, why can't declare after cin?
You first have to declare a variable before using it. Or in other words: Variables have to be in scope when using them. That's the way it works in C++.
cin is the object in c++ of class istream.
It is Used to accept the input from input devices like keyboard and
int x; is declaration of variable i.e. allocating the space where our number from input stream will get stored.>> is extraction operator which receives the stream.So declaration and taking input are 2 different things. 1) allocate memory(int x)2)write into it.
These 2 things won't happen together.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
My compiler gives me an error when I do this:
strcpy(skin.conclusion[0], "Mel");
My struct looks like this:
struct list{
char conclusion[10][4] = {};
}skin;
What am I doing wrong or is there something else other than strcpy that I'm supposed to use.
Here is the full code that worked for me. Of course, this is C style C++ and not modern C++, there are many reasons to prefer std::string
#include <iostream>
#include <cstring>
struct list{
char conclusion[10][4] = {};
}
skin;
int main()
{
strcpy(skin.conclusion[0], "Mel");
std::cout<<skin.conclusion[0];
return 0;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm new to C++ and coding entirely.
When I try to build my code it gives me "error: 'count' was not declared in this scope"
Everything I look up either tells me to add "using namespace std;" or add "int main()" but neither works for me.
#include <iostream>
using namespace std;
main()
{
int A = 4;
count << &A;
}
There is a typo in your identifier.
count should be cout.
Also, main should have the return type of int as it isn't standard C++ to automatically deduce the return type as int if not specified. In short, int main() is required.