I wrote:
string a[100] = { "B", "F", "R", "P", "D", "\0" };
class BD{
//data member
static int id;
//.......
};
void setid(int d){ id = d;
for (int i = 0; i < 5; i++){
id = i + 1;
user[id];
}
cout << " ID : " << id << endl;
}
int BD::id = 0;
in this code, has an array include name of user and for each user a different ID number but in run (output) always will be 5 !
I know it's a logical error.
when enter : B
ID : 5
but I want to print :
if user enter name `B`
ID : 1
if user enter name F
ID : 2
You are rewriting id in the loop
id = i + 1;
Thus as the last i used in the body of the loop is equal to 4 then you always get that id = 5
EDIT:
If you want to find a string in an array of strings and return its index + 1 then the function can look like
int getId( const std::string &s )
{
string a[100] = { "B", "F", "R", "P", "D", "\0" };
int id = 0;
while ( a[id] != "\0" && a[id] != s ) ++id;
return a[id] == "\0" ? 0 : id + 1;
}
However it seems you do not understand the difference between objects of type std::string and character arrays and string literals. So you need something different than the function I showed.
Related
Code works fine except I need to add this constraint.
"If input is NULL, return -1".
Im just wondering how I can do this. Everytime I put NULL in for s, it crashes.
Side Note: If you need to know, this converts the excel titles to numbers like A = 1, Z = 26, AA = 27, AB = 28, etc.
#include <iostream>
using namespace std;
class CIS14
{
public:
int convertExcelTitleToNumber(string* s)
{
string str = *s;
int num = 0;
for (unsigned int i = 0; i < str.length(); i++)
{
num = num * 26 + str[i] - 64;
}
return num;
}
};
int main()
{
CIS14 cis14;
string s = "AA";
cout << cis14.convertExcelTitleToNumber(&s) << endl;
return 0;
}
Everytime I put NULL in for s, it crashes.
That doesn't surprise me at all, dereferencing a null pointer (with string str = *s in your case) is undefined behaviour.
To prevent this when passing a null string pointer:
cout << cis14.convertExcelTitleToNumber(nullptr) << endl;
you need something like this as the first thing in your function, before trying to dereference s:
if (s == nullptr)
return -1
Feel free to use NULL instead of nullptr if you're stuck in the dark ages :-)
I create a program who I need to compare two Chinese characters.
I use this function to compare :
void fengshuitradition::comparerAuto()
{
Stockage obj_stockage;
Lunar lunar;
LunarObj* obj = lunar.solar2lunar(ui->SBSelection_4->value(), ui->SBSelection_3->value(), ui->SBSelection_2->value());
string day = obj->ganzhiDay;
for(int i = 0; i<64; i++)
{
string jourComparer = obj_stockage.appelStockage(i,1);
string jourComparer2 = obj_stockage.appelStockage(i,2);
if (day.compare(0,6,jourComparer,0,6) == 0 && day.compare(7,6,jourComparer2,0,6) == 0)
{
ui->label_0->setText(obj_stockage.appelStockage(i,0));
ui->label_1->setText(obj_stockage.appelStockage(i,1));
ui->label_2->setText(obj_stockage.appelStockage(i,2));
ui->label_3->setText(obj_stockage.appelStockage(i,3));
ui->label_4->setText(obj_stockage.appelStockage(i,4));
ui->label_5->setText(obj_stockage.appelStockage(i,5));
ui->label_6->setText(obj_stockage.appelStockage(i,6));
return;
}
}
}
My values are stock like this :
const char* stockage[64][7] = {
{"1",
"\u7532 B+",
u8"\u5b50 E+ hiver",
"24 F",
"8",
"癸E-",
". ."},
// ainsi de suite
And I use a library :
static std::string Gan[] = {"\u7532","\u4e59","\u4e19","\u4e01","\u620a","\u5df1","\u5e9a","\u8f9b","\u58ec","\u7678"};
static std::string Zhi[] = {"\u5b50","\u4e11","\u5bc5","\u536f","\u8fb0","\u5df3","\u5348","\u672a","\u7533","\u9149","\u620c","\u4ea5"};
Thanks for your help.
I'm trying to replace multiple words with their "pirate pair", for example:
Normal: "Hello sir, where is the hotel?"
Pirate: "Ahoy matey, whar be th' fleagbag inn?"
This is what I tried before:
#include<iostream>
#include<string>
#include<conio.h>
using namespace std;
void speakPirate(string s);
int main()
{
string phrase;
cout << "Enter the phrase to Pirate-Ize: ";
getline(cin, phrase);
speakPirate(phrase);
_getch();
return 0;
}
void speakPirate(string s)
{
int found;
// list of pirate words
string pirate[12] = { "ahoy", "matey", "proud beauty", "foul blaggart", "scurvy dog", "whar", "be", "th'", "me", "yer", "galley", "fleabag inn" };
// list of normal words
string normal[12] = { "hello", "sir", "madam", "officer", "stranger", "where", "is", "the", "my", "your", "restaurant", "hotel" };
for (int i = 0; i < s.length(); i++)
{
found = s.find(normal[i]);
if (found > -1)
{
string left = s.substr(0, found - 1); // get left of the string
string right = s.substr(found + pirate[i].length(), s.length()); // get right of string
s = left + " " + pirate[i] + " " + right; // add pirate word in place of normal word
}
}
cout << s;
}
But it didn't really work and was very buggy, so I tried using the replace() function instead:
#include<iostream>
#include<string>
#include<conio.h>
using namespace std;
void speakPirate(string s);
int main()
{
string phrase;
cout << "Enter the phrase to Pirate-Ize: ";
getline(cin, phrase);
speakPirate(phrase);
_getch();
return 0;
}
void speakPirate(string s)
{
int found;
// list of pirate words
string pirate[12] = { "ahoy", "matey", "proud beauty", "foul blaggart", "scurvy dog", "whar", "be", "th'", "me", "yer", "galley", "fleabag inn" };
// list of normal words
string normal[12] = { "hello", "sir", "madam", "officer", "stranger", "where", "is", "the", "my", "your", "restaurant", "hotel" };
for (int i = 0; i < s.length(); i++)
{
found = s.find(normal[i]);
if (found > -1)
{
s.replace(found, found + pirate[i].length(), pirate[i]);
}
}
cout << s;
}
I'm not sure why, but this doesn't really work either. I also notice that when I try changing a larger word into a smaller word, some of the original word is leftover, for example:
Enter the phrase to Pirate-Ize: hello
ahoyo
And I just noticed that it sometimes might not even change the word at all, for example:
Enter the phrase to Pirate-Ize: where
where
How come? Could someone please tell me what I need to do or a more effective solution that I could implement? Thanks a lot.
Here you iterate over the length of the text:
for (int i = 0; i < s.length(); i++)
It should be the length of the array of texts, something like
for (int i = 0; i < 12; i++)
However, you should use a std::map to model the mapping between the normal words and their pirate version.
std::map<std::string, std::string> words = {
{"hello", "ahoy"},
// .. and so on
};
for(auto const & kvp : words)
{
// replace kvp.first with kvp.second
}
Marius is correct that the major error is that you need to iterate over the length of the arrays. A different way than mapping would be to use erase() and insert() where you used replace(). replace() does not account for the lengths of the strings being different, but removing a substring and then adding in a new substring will. This can be done as follows
for (int i = 0; i < 12; i++)
{
found = s.find(normal[i]);
// Locate the substring to replace
int pos = s.find( normal[i], found );
if( pos == string::npos ) break;
// Replace by erasing and inserting
s.erase( pos, normal[i].length() );
s.insert( pos, pirate[i] );
}
Here is the code:
string msg;
niceArray = txtReader("chatTest/replaces.txt");
vector<vector<string>>vMaster;
if(vMaster.size() <1){
string arr[] = { "a","A","á","#","à ","â","ã","ÃÃ","€Ã","ƒÃ"};
vector<string> tempA(arr, arr+4);
vMaster.push_back(tempA);//"aAá#à âãÃÀÃÂ"
}
string ex;
while(sstr.good()){
sstr>>ex;
vectorCheck.push_back(ex);
}
for(int e = 0; e < vectorCheck.size(); e=e+1){
//if(e > vectorCheck[e].length()) break;
auto str = vectorCheck[e];
for(int b = 0; b < vMaster.size(); b=b+1){
for(int j=0; vMaster[b].size(); j=j+1){
//int f = str.find(vMaster[b][j]);
if(str.find(vMaster[b][j]) != std::string::npos){
int f = str.find(vMaster[b][j]);
//if(vMaster[b][j].length() > 1){
str.replace(f,2,vMaster[b][0]);
//break;
// }
//
}
}
}
for(int i = 0; i < xingArray.size(); i=i+1){
if(str == xingArray[i]){
vectorCheck[e] = niceArray[rand() % niceArray.size()];
}
}
}
So for each sentence i type i am checking each word and looking if there is any of that string arr characteres in it, if there is i replace it for the vector[0] in this case "a".
The problem is that this line str.find(vMaster[b][j]) != std::string::npos never returns me -1... Even when i type like "c" in finds c in there or "f" or any word and i get an error. The funny stuff is that when i type like "á" that turns into "Ã|" it works and with the "ã" that turns into "ã" it doesnt give me 0 again... I really dont know whats going on... I really tried hard here and if anyone has any opinion i would like to hear thanks.
std::string str ("potato.");
std::string str2 ("ta");
std::size_t found = str.find(str2);
if ( found != std::string::npos)
std::cout << "first 'ta' found at: " << found << '\n';
str.replace( found, 2, "");
I dont think C++ has any single method to find and replace so why not use inbuilt find and then replace.
For example:
void find_and_replace(string& source, string const& find, string const& replace)
{
for(std::string::size_type i = 0; (i = source.find(find, i)) != std::string::npos;)
{
source.replace(i, find.length(), replace);
i += replace.length() - find.length() + 1;
}
}
int main()
{
string text = "potato";
find_and_replace(text, "ta", "");
cout << "After one replacement: " << text << endl;
find_and_replace(text, "te", "");
cout << "After another replacement: " << text << endl;
return 0;
}
Please note that below code is only the idea, and not the actual code. How do I print the characters inside my teststring[] array one at a time when a button is clicked?
const string teststring[] = {
"a",
"b",
"c",
"d",
};
if ( button ).onclick() == true {
int i = 0;
printf("output: %s\r\n", teststring[i]);
i++;
}
It is similar to the code in http://jsfiddle.net/Dfprp/, but this is in JavaScript and instead of numbers, I want the characters that are stored in my teststring array.
If I understood your question correctly, this is what you are looking for
std::string teststring = "abcd"
private void myMethod()
{
int len = teststring.length();
for(int cnt=0; cnt< len; cnt++)
printf("output: %c\n", teststring[i]);
}