input class object into array of objects [closed] - c++

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 5 years ago.
Improve this question
I am trying to write a program that takes a string and an integer in a class object. The program will then sort the class objects, in an array of objects, by the integer, allowing me to then display the names. Unfortunately, when I try to build the array, I have an error on my assignment operator.
My questions are: Do I need to overload the = operator, and if so, how (somehow I've never figured out how to overload operators)? If not, where am I going wrong?
Here is the code:
void InitiativeList::makeList(size_type physicalSize, size_type logicalSize)
{
string sNewActor;
int iNewOrder;
for (size_t index = 0; index < physicalSize; index++)
{
if (logicalSize == physicalSize)
{
grow(physicalSize);
}
cout << "Enter character name: ";
cin >> sNewActor;
if (sNewActor == "Exit")
{
return;
}
cout << "Enter initiative roll: ";
cin >> iNewOrder;
actorOrder[index] = new Actor(iNewOrder, sNewActor);
logicalSize++;
}
}
Thank you for your help.

You don't need new there, because you have an array of actorData and not actorData pointers, and the error is saying that it can't convert actorData pointer to actorData. So replace this line:
actorOrder[index] = new actorData(iNewOrder, sNewActor);
with this:
actorOrder[index] = actorData(iNewOrder, sNewActor);

Related

How can I assign the integers in an input like 1.2.3 to variables in C++? [closed]

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 10 months ago.
Improve this question
Here is an example of what I mean.
Input: 10.20.50
a = 10
b = 20
c = 50
you will need to store the input in a string or char[] and then iterate over the string or char[] and write some code that will identify the separate parts of the input and convert them to ints using stoi().
this would work but just an example (and i think it will not print the final number unless the input is ended with "." but this should give you an idea of what you could do.
std::string i = "";
std::cin >> i;
std::string buffer = "";
for (auto c : i)
{
if (c != '.')
{
buffer += c;
}
else
{
int num = std::stoi(buffer);
buffer = "";
std::cout << num << ", ";
}
}

c++ Send the string to a function to determine how long is the string [closed]

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 5 years ago.
Improve this question
I know how to use a string to calculate the number of characters, but I'm not sure how to use a function to do that. have to use CSTRING. THANK YOU ALL
#include <cstring>
char a[10];
cout << "Please enter anything: ";
cin.getline(a,10);
cout << "You type " << strlen(a) << " letters long"<<endl;
You're probably looking for std::string since you're question mentioned C++ and not only C.
include <string>
std::string myString = "Something";
size_t stringLength = myString.size();
It's simple. Just type your code inside a function()
int stringlengthfunction()
{
char str[80];
int i;
cout<<"\n enter string:";
cin.getline(str,80);
int n=strlen(str);
cout<<"\n lenght is:"<<n;
getch();
return 0;
}
or pass your string as a parameter to the function
int stringlengthfunction(string str)

how to define int number using comparison operators [closed]

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 9 years ago.
Improve this question
I want to declare
int x = it must be more or equal to 1 but less or equal to 100;
How can I do it? I dont want to use if condition, Im looking for something short and clear, if possible.
The x number is input, so program should accept only numbers in this limit.
It seems that you're looking to error check on initialization.
If I were you I'd do something along the lines of.
int x;
cout << "Enter a value: " << flush;
cin >> x;
while(!((x>=1)&&(x<=100))) {
cout << "Try Again: " << flush;
cin >> x;
}

Stuck in C++ data structure program [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Write a program that prompts the user for a string and uses a recursive function to print the string backward. Do not use any global variables; use the appropriate parameters. Could you give me some hints, like pseudocode?
int stringbackwards(string a){
if()
else
}
int main(){
string name;
cout<<"Write a name: ";
cin>>name;
cout<<"Backwards "<<stringbackwards(name);
return 0;
}
Why do you use a recursion for that?
There is a good concept in c++ called iterators that already has this functionality implementet :)
http://www.cplusplus.com/reference/string/string/rbegin/
So in your case:
cout<<"Backwards ";
for (std::string::reverse_iterator rit=name.rbegin(); rit!=name.rend(); ++rit)
{
cout << *rit;
}
But to make it recursive, i would do it like this (Pseudocode).
function backwards(std::string& name, unsigned int length)
{
unsigned int length = name.length();
unsigned int currLength = length - 1;
if (length > 0)
{
backwards(name, currLength);
}
std::cout << name[length - currLength - 1];
}
Hint:
Say the string is "abcd". You would like to print "dcba". In other words, you are printing the last letter first.
Hence you will first go deep into recursion and then after coming back, print the letter 'a'.

C++ code crashing, can't figure out why [closed]

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 9 years ago.
Improve this question
my C++ code is crashing when I execute Search via Word (2) from my part of the code.
What it does is scan for a .txt file, then print out some information then it gives me options, my 2nd option is crashing my code.
The part of my code which is causing it to crash is, it's goal to to read user input then scan the file for a matching word then print out it's definition.
case 2:
{
string searchWord;
cout << "Enter a word to search for: ";
std::getline(std::cin, searchWord);
Word *myWord = Dic.findWord(searchWord);
if (myWord != NULL)
{
cout << myWord->definition;
}
break;
}
Your logic in findWords is wrong, you should check for MAX_WORDS before you try to compare not afterwards. Like this
Word* Dictionary::findWord(string searchWord)
{
int wordIndex = 0;
while (wordIndex < MAX_WORDS) {
if (myWords[wordIndex]->word.compare(searchWord) == 0) {
return myWords[wordIndex];
}
wordIndex++;
}
cout << "word not in dictionary";
return NULL;
}