【发布时间】:2013-11-15 17:06:27
【问题描述】:
感谢您的阅读。我正在制作一个脚本来读取这种格式的生日:月/日/年,并将年、日和月分开。我得到了年份部分,但对于白天部分,我尝试使用 string.subtr(,) 减去第二个“/”的位置值和最终位置值。因此,例如,我试图在 findDay() 函数中从 01/26/1994 获取 01/26。 但我似乎在第 55 行收到“字符串没有名为 'subtr' 的成员”错误。有人可以指导我,因为我是一个全新的程序员。另外,感谢您一直以来的帮助,因为在收到本网站的意见后,我的知识翻了一番。
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
using namespace std;
void findYear(string &);
void findDay (string &);
void findMonth(string &);
int main()
{
string birthday;
cout << "Enter birthday: " << endl; // 01/26/1994
cin >> birthday;
string year = birthday;
string day = birthday;
string month = birthday;
findYear(year);
cout << year << endl;
findDay(day);
cout << day << endl;
system("pause");
return 0;
int slashpos = birthday.find('/');
}
void findYear(string &year)
{
int slashpos = year.find('/');
int i = 0;
string temp2;
while(year.at(year.length()-1-i)!='/')
{
temp2 += year.at(year.length()-1-i);
i++;
}
string rtemp2 = "";
for(int k = 0; k < temp2.length(); k++)
{
rtemp2 += temp2.at(temp2.length()-1-k);
year = rtemp2;
}
}
void findDay (string &day)
{
string tempday1 = "";
string temp2 = "";
int i = 0;
tempday1 = day.subtr(day.rfind('/'),day.length()-1); /* error here! [Error] 'std::string' has no member named 'subtr'*/
while(tempday1.at(tempday1.length()-1-i)!='/')
{
temp2 += tempday1.at(tempday1.length()-1-i);
i++;
}
string rtemp2 = "";
for(int k = 0; k < tempday1.length(); k++)
{
rtemp2 += tempday1.at(tempday1.length()-1-k);
day = rtemp2;
}
}
【问题讨论】:
-
你需要
std::string::substr。 -
为什么所有的反对票?这是一个好问题,代码有一个简单的错误,答案解决了它。
标签: c++ string subtraction