Hello this is a segment of my code of which i am trying to implement the Morris-Pratt algorithm.
When i am comparing my variables if find that they dont match, this is because one of my variables "Temp" is geting extra characters added to the end of the array.
here is my code...
// Calculate the next talbe
char test[searchLen];
for(int i = 0; i < searchLen; i++)
{
test[i] = fileContent[currPos+i];
}
cout << "SEARCHLEN: " << searchLen << endl;
cout << "TEST: " << '\t' << '\t' << test << endl;
cout << "SEARCH: " << '\t' << search << endl;
cout << strcmp(test,search) << endl << endl;
// Determine if a match is detected
if(strcmp(test,search)==0)
{
cout << "----------------------> Match detected at: " << currPos << endl;
}
currPos ++;
}
return numberOfComparisons;
}
The output looks like this...
SEARCHLEN: 8
TEST: athsoutg5?h
SEARCH: brilling
-1
As you can see the 5?H is not supposed to be there and is breaking my code.
You need to add a null terminator.
char test[searchLen + 1];
test[searchLen] = '\0';
It does look like your string is not terminated with \0, maybe you forgot to copy it / put it in there?
Related
I'm working on a school project for C++ with involves on working with polimorfism, dynamic objects, heritage. My project consists on an main.cpp with will run a loop with options for the user to select. I will be storing some data in a file, the data will be appended so I don't loose the data that was already in the file, but my file works like the following example:
2
Data;12;21;0
Data2s;22;61;7
This is what I want to happen:
3
Data;12;21;0
Data2s;22;61;7
Data3s;8;2;9
What I'm attempting to do is, I have to change the first line to the next variable number, and I can't loose the data under it.
After I change the line, I will go to the end of the file (after numb 7) so I can add another line of data.
attempt 1:
void Empresa::save_data(){
ofstream arquivoE;
arquivoE.open("data.txt", ios::app);
if (temp > 0){
arquivoE.seekp(0) << num_func;
arquivoE.seekp(0, ios_base::end);
arquivoE << endl;
}else{
arquivoE << num_func << endl;
}
for (int i = temp; i < num_func; i++)
{
arquivoE << func[i]->getNome() << ";" << func[i]->getEmissao() << ";" << func[i]->getSalario() << ";" << func[i]->last_field() << endl;
}
cout << " Sucesso ao gravar arquivo!" << endl;
arquivoE.close();
}
attempt 2:
void Empresa::save_data(){
ofstream arquivoE;
arquivoE.open("data.txt", ios::out);
arquivoE.seekp(0, ios::beg) << num_func;
arquivoE.seekp(0, ios::end);
arquivoE << endl;
for (int i = temp; i < num_func; i++)
{
arquivoE << func[i]->getNome() << ";" << func[i]->getEmissao() << ";" << func[i]->getSalario() << ";" << func[i]->last_field() << endl;
}
cout << " Sucesso ao gravar arquivo!" << endl;
arquivoE.close();
}
num_func is the the variable from the first line, which I need to change
Temp variable is just to keep track of where to add the line
This is the data line:
arquivoE << func[i]->getNome() << ";" << func[i]->getEmissao() << ";" << func[i]->getSalario() << ";" << func[i]->last_field() << endl;
I'm a little confused as to how to go around this. I have some array variables with some information, and I want to print them out after some calculations. If the value is 0, then I want to print a " " instead. There are 3 arrays that need to get checked however, how would I change the output statement to cater for all 3 checks and print an empty string instead of the value?
for(int start = 1; start < 13; start++)
{
if(check[start] == 1)
{
cout << checkMonth(start) << ": " << setprecision(1) << fixed << averagespeed[start] << "(" << setprecision(1) << fixed << sdSpeed[start] << ")," << setprecision(1) << fixed << averagetemp[start] << "(" << setprecision(1) << fixed << sdTemp[start] << ")," << setprecision(1) << fixed << Solar[start] << '\n';
}
/*if(sumTemp[start] == 0 || sumTemp[start] == 0 || sumSpeed[start] == 0){
}*/
}
Example Output looks like this:
January,5.5(1.2),25.5(12.2),196.4
For example if Sum of Speed is 0, that means all values of speed were 0 or null. So it should change to this:
January,,25.5(12.2),196.4
A single line to std::cout doesn't need to be done in one statement. For example:
std::cout << "First";
std::cout << ", second"
std::cout << ", third\n"
Prints the following line:
First, second, third
Now we can use an if to conditionally print the middle part of the string:
std::cout << a;
if (b != 0) {
std::cout << ", " << b;
}
std::cout << ", " << c << '\n';
I am working on a palindrome program for class. I've written the program and it works. The issue I'm having is the output. I can't figure out how to change the characters into the number they are associated with. Here is my code:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main ()
{
string word;
int i;
int length;
int counter = 0;
cout << "Please enter a word." << endl;
getline (cin,word);
cout << "The length of the word is " << word.length() << "." << endl;
length = word.length();
for (i=0;i < length ; i++)
{
cout << "Checking element " << word[i] << " with element " word[length-i-1] << "." << endl;
if (word[i] != word[length-i-1])
{
counter = 1;
break;
}
}
if (counter)
{
cout << "NO: it is not a palindrome." << endl;
}
else
{
cout << "YES: it is a palindrome." << endl;
}
return 0;
}
The output I'm getting displays all the characters of the string and looks like this:
my output
Please enter a word
hannah
Checking element h with element h
Checking element a with element a
Checking element n with element n
(etc)
Yes: it is a palindrome.
But, I need the output to display the characters as their placement number in the string, which looks like this:
what output should be
Please enter a word
hannah
Checking element 0 with element 5
Checking element 1 with element 4
Checking element 2 with element 3
Yes: it is a palindrome.
Any hints or tips would be great. I just feel like I've tried everything I know, and it still won't look right. Thank you!
Instead of using :
cout << "Checking element " << word[i] << " with element " word[length-i-1] << "." << endl;
why not use:
cout << "Checking element " << i << " with element " << (length-i-1) << "." << endl;
This line:
cout << "Checking element " << word[i] << " with element " word[length-i-1] << "." << endl;
should be written as
cout << "Checking element " << i << " with element " << length-i-1 << "." << endl;
will give you what you want.
I need help getting declared string function to change white space of input file to a specific character.
if (infile.fail())
{
cout << "The file doesn't exist";
exit(-1);
}
else
{
numBooks = readFile (infile, magSub, 260);
for (i=0; i<numBooks; i++)
{
cout << "Last Name: " << magSub[i].lastName << endl;
cout << "First Name: " << magSub[i].firstName << endl;
cout << "Street Address: " << magSub[i].address << endl;
cout << "City: " << magSub[i].city << endl;
cout << "State or Province: " << magSub[i].state << endl;
cout << "Country: " << magSub[i].country << endl << endl;
cout << "Zip or Postal Code: " << magSub[i].zip << endl;
cout << "Expiration Date: " << magSub[i].expDate << endl;
cout << "Subscriber Number: " << magSub[i].subNum << endl << endl;
}
writeFile(outfile, magSub, numBooks);
}
}
void fillSpace (string &expDate)
{
for (int i=0; expDate.length(); i++)
{
if (isspace(expDate[i]))
expDate[i] = '0';
}
}
I have the function declared above main. I know I need to call the function but I can't get it to change the white spaces.
In your code for fillSpace, you are not checking for the end of string condition. You should use i<expDate.length() for checking the end of string.
You have missed the check condition in for loop of fillSpace function.
for (int i=0; i < expDate.length(); i++)
And for calling the function
you have to declare a string which will store the string from the magSub[i].expDate.
and then pass that string to the function fillSpace.
After that you will get the string with replaced char space with '0'.
cout << "Expiration Date: " << magSub[i].expDate << endl;
please use the following code:
string temp = magSub[i].expDate; // copy the string to the temp string/char array
fillSpace (temp); // Missing Line for function call
cout << "Expiration Date: " << temp << endl; // replace line with
Hope
this will Help you.
I am trying to search a structure for a value I entered. However I want the search to work even if I only enter a part of the word. How do I do this?
Here is my Code:
for (int i = 0; i < size; i++)
{
if (searchName == ptrCandy[i].name)
{
cout << "Name: " << ptrCandy[i].name << "\n" << "Quantity: " << ptrCandy[i].quantity;
cout << "\n" << fixed << setprecision(2) << "Cost: $" << ptrCandy[i].cost << "\n" << endl;
}
}
I am assuming that if you type in n characters, you want to only match candy elements that begin with those n characters, and not attempt to do any autocorrection or spell-checking or the like. If these are correct assumptions, then let the STL do the work for you:
int searchLength = searchName.length();
for( int i=0; i<size; i++ ) {
if ( ptrCandy[i].name.length() >= searchLength &&
searchName == ptrCandy[i].name.substr( 0, searchLength ) ) {
// Found it!
}
}
For example, if you have a candy named "snickers" and "snack mix", then if you enter "sn", it will return both of these, but if you enter "sni", you will only get "snickers".
first, you need a threshold for how close the match has to be. is 1 letter ok?
second, decide if it must match from the beginning, or beginning of a word eg does do work for "odor"?
Then if you don't want to use another library, loop through each char and continue until you reach the threshold or the end of the string
In other words, if the name is "Long dude one" and the search string is "dude", then start at name[0] and do a 4-iteration loop (four letters in dude) and check each one with the corresponding one from name. If any letter does not match, exit that loop. Then do the same thing starting from name[1] to name[4], then from name[2] to name[5] etc all the way until you are checking the last 4 letters of the search string " one" against dude. However, you wouldn't get that far because on the 6th attempt, eg looping through name[5] to name[8] all 4 letters would match so you would set match=true and exit.
put that in a function and call it for each name.
Assuming these types are of std::string:
for (int i = 0; i < size; i++)
{
size_t pos = ptrCandy[i].name.find(searchName);
if (pos != string::npos)
{
cout << "Name: " << ptrCandy[i].name << "\n" << "Quantity: " << ptrCandy[i].quantity;
cout << "\n" << fixed << setprecision(2) << "Cost: $" << ptrCandy[i].cost << "\n" << endl;
}
}
If you wanted to do it case-insensitive, just convert both strings to either all upper or all lower case and then do the search on the modified strings.
You can use member function compare the following way
for (int i = 0; i < size; i++)
{
if ( ptrCandy[i].name.compare( 0, searchName.size(), searchName ) == 0 )
{
cout << "Name: " << ptrCandy[i].name << "\n" << "Quantity: " << ptrCandy[i].quantity;
cout << "\n" << fixed << setprecision(2) << "Cost: $" << ptrCandy[i].cost << "\n" << endl;
}
}