Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am trying to write a programm that reads the surname introduce and gives back the info of the student (birthdate, group, etc). The compiler does not recongize the == operator, I guess it doesnt know what to compare, either the address or the value?(I would appriciate an explanation)
I read similar cases and they suggest to use bool operator. As far as I know bool operator just returns true or false(I dont see the way to use it to print the students info)
Also I was considering usind char or using strcmp (srting functions for comparing).
void find_fam ( struct st student[], int length, string & fam)
{
for(int i=0; i<length; i++)
if ( student[i]== fam[i]){
cout << "Student:\n"<< student[i].imia << student[i].otchestvo << student[i].familia <<
student[i].gruppa << student[i].grozhdenia);
}
}
If you have a struct like this for example
struct st
{
std::string name;
int age;
};
Then you would want to compare the fam to that member of your struct
if (student[i].name == fam)
So now .name is the corresponding std::string that you are trying to compare. Otherwise you are trying to compare a std::string to a st struct.
In this example you've shown, you're comparing an entire instance of the struct to the string, which is invalid.
What you should do is compare the string attribute to the string you take in.
Assuming the family of the student is called family and is of type string:
if(student[i].family==fam)
{
//statements
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I wanted to assign a value to a constant but as it is constant, we cant assign value to it through cin. So I came up with this idea. Is it okay if we use it like this?
{
int data = 0;
cout << "enter number of students"<<endl;
cin>>data;
const int number = data;
cout << number<<endl;
}
This is fine. const values do not need to be compile time constants. They just can't be changed after they are defined.
Similarly you could do:
int getNumberFromStdin() {
int data;
cin >> data;
return data;
}
int main() {
const int number = getNumberFromStdin();
}
Yes, because it is not an assignment; it is a declaration.
Despite looking quite similar, the two constructs are different: assignment changes something that is already declared, while the declaration sets up something new, and optionally gives it a value. The value does not need to be hard-coded, though: it can be a result of some processing, as is the case in your example.
Naturally, assignment is not allowed for constants, because they already have a value.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Very simple question but unfortunately I am at a loss. I am trying to build a program where a user is asked to enter nicknames for a pet. Each time they enter a nickname I would like to store that nickname into an array. After they enter a nickname I would also like to list all of the nicknames that they previously entered as well. I would provide some source code but I am not sure where to begin. Any tips or feedback would be greatly appreciated!
Using C++ 11, a sample code with comments is provided.
// header file for standard i/o stream
#include<iostream>
// header file for string
#include<cstring>
// header file for vector container usage
#include<vector>
// use standard namespace, std
using namespace std;
// main start from here
int main() {
// use vector which is a c++ stl container
vector<string> names;
// a temp var
string name;
// input until EOF
while (getline(cin, name)) {
// push back to the vector container
names.push_back(name);
// output what are inside the vector
// Here I use C++ 11 auto feature
for (auto pet_name : names) {
cout << pet_name << ", ";
}
cout << endl;
}
return 0;
}
I would use std::vector.
Internally std::vector use a dynamically allocated array to store their elements.
So you can simply use std::vector::push_back(petNickName) to store nickname of pet represented as string and not worrying about size of array and other things... .
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
If each of these string variables in the left portion certainly contain their literal equivalents in the latter, then why do I not get my message?
if (make_tuple(this->currentState, inputSymbol, stackTop) == make_tuple("q0", "a", "0"))
cout << "These tuples are equal" << endl;
The reason I ask is because I am using a map with a tuple as the key and when I try to use find(), it behaves as if the key does not exist in the mapping, which I am certain it does because I went through the map using an iterator and displayed all of the keys (each element in the tuples). I suspect that the error has something to do with the above code because those should be equal but they are not. (I am using map.find(make_tuple(blah, blah, blah)) and comparing that to the map.end()) Thoughts?
Here's my best guess at your question:
const char input[] = "hello";
if (std::make_tuple(input) == std::make_tuple("hello")) {
// won't get here
std::cout << "equal\n";
}
The reason is that tuple<>::operator== is just element-wise equality. In this case, we have two tuple<const char*>, but while the strings they point to are the same, the actual pointers themselves are different, and we're just comparing the pointers.
If you want this to work, you'll need the left-hand-side to be a type that has an operator== that does the right thing. Like, say, std::string:
const char input[] = "hello";
if (std::make_tuple(std::string(input)) == std::make_tuple("hello")) {
// now it happens
std::cout << "equal\n";
}
Or, since you're using a std::map, you will not want to use the default tuple<>::operator< and instead provide your own that uses strcmp instead of the raw pointer <.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I've looked around and seen quite a few of these, but none of them provided the solution for my problem. I'm getting this compilation error with the following code:
THE ERROR:
THE CODE:
const int TOP_WORDS = 25;
...
void topWords(Hash t, string word, string topA[]);
int main()
{
...
Hash table1;
string word = "example";
string topWordsArr[TOP_WORDS];
table1.addItem(word);
topWords(table1, word, topWordsArr);
...
}
...
void topWords(Hash t, string word, string topA[])
{
int i = 0;
int tempCount = t.itemCount(word);
int tempCount2 = t.itemCount(topA[i]);
while (tempCount > tempCount2 && i < TOP_WORDS) {
i++;
tempCount2 = t.itemCount(topA[i]);
}
if (i > 0)
All the other posts I've seen about this error involved an incorrect syntax with declaring/passing the string array parameter, but I've double and triple checked it all and I'm certain it's correct; though I've been wrong before..
Using my crystal ball:
you're passing the Hash by value
this requires the copy constructor,
you don't have one (or it's botched, private or explicit)
So, take the Hash by reference
void topWords(Hash const& t, std::string const& word, std::string* topA);
Also,
string[] is not a type in C++
don't use using namespace std;
don't use raw arrays; use std::vector<std::string> (or std::array<std::string, N>)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am implementing linked list in c++. In that am trying to compare a data stored in the node with a string. Here is my code:
String f;
cin>>f;
if(strcmp(temp->data,f)==0)
{ cout<<"same"; }
else
{ cout<<"not same"; }
Here is my error:
"assignment1.cc", line 160: Error: Cannot cast from std::string to const char*.
"assignment1.cc", line 160: Error: Cannot cast from std::string to const char*.
How to compare those two strings?
If you only need to check for equality, you can simply use operator== to compare two strings. In your case, that seems to be:
if (data->temp == f)
However, if you want the functionality provided by strcmp (that is, if you need to know which string is larger lexicographically in case they are not equal), you can use string::compare:
if ( s1.compare(s2) < 0 )
You can use the std::string::c_str method:
std::string f;
cin>>f;
if(strcmp(temp->data,f.c_str())==0)
cout<<"same";
else
cout<<"not same";
You can either use f's "compare()" operator (http://en.cppreference.com/w/cpp/string/basic_string/compare) or you can use operator ==
#include <iostream>
#include <string>
int main() {
std::string f("hello world");
const char* p = "hello world";
if (f == p)
std::cout << "f == p" << std::endl;
}
See http://ideone.com/TTXRZv