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;
}
Related
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 << ", ";
}
}
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
I want to find index of '-' in string "book-Buch". What should I do? I mustn't use this function or any others. Any ideas?
int indexOf(int ch, int fromIndex)
#include <iostream>
#include <string>
int main() {
std::string word("book-Buch");
// The easy way
std::cout << word.find("-", 0) << '\n';
// The manual way
for (std::size_t i = 0; i < word.length(); ++i) {
if (word[i] == '-') {
std::cout << i << '\n';
}
}
}
If you just want to find the index that a certain character occurs in, you just need to look at each character and check if it's the one you want.
A string can be treated as an array of characters. It's unknown whether you actually want an array of string objects, or are just confused.
Other questions that would need to be answered: do you need to find all occurrences, or just the first? Are you reading the words out of a file? You don't clearly explain how a file comes into play.
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 3 years ago.
Improve this question
I am working on a homework assignment where I need to error check the first line of a .txt file. In this case, the first line of the text file should read "Number of samples: ", followed by an unspecified number. If the line DOES NOT start with "Number of samples: ", I need to terminate the program after displaying an error message. Assuming that the line DOES start with the proper string, I need to read in the number as an unsigned integer.
In summary, I need to read in 19 characters from a .txt file line. How do I go about doing that, while still being able to read in the rest of the line to store it as a variable?
The libraries I'm using are iostream, string, ofstream, and ifstream if that matters.
ifstream x;
char addToString;
string check = "Number of samples: ";
string temp = "";
int num;
x.open("file.txt");
for (int i = 0; i < 19; i++){
x.get(addToString);
temp += addToString;
}
if (check == temp){
x >> num;
// other stuff
}
else {
cout << "error message" << endl;
}
x.close();
return 0;
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);
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 have a project in c++, and need to find largest filename from the text file.
My text file example is:
foundedindex = instline.find(" ");
inst_host = instline.substr(0, foundedindex);
//cout << inst_host << " a" << endl;
obj[count].sethost(inst_title);
So, I want to read only "index.html", "23,html", "24.html" etc.
When I seperate all line by one by like in code, sorting takes too much time.
Please help me.
This is a way to get the columns of each line.
std::string temp;
std::vector<vector<std::string>> data;
while(std::getline(file, temp))
{
std::vector<std::string> x;
std::istringstream liney(temp);
while(std::getline(liney, temp, ' '))
{
x.push_back(temp);
}
data.push_back(x);
}
// Then using a column loop through a 2d array.
int row = 0;
for(int i = 0; i<data[row].size(); i++)
{
for(int s = 0; s<data.size(); s++)
{
data[s][i]; // Do something
}
row++;
}
Hope this helps.