2013-11-15 32 views
-7

感謝您的閱讀。我正在編寫一個腳本,以這種格式讀取生日:月/日/年,並將年,日,月分開。我得到了年份,但是對於那一天,我試圖用string.subtr(,)減去第二個'/'的位置值和最終位置值。所以,例如,我試圖在findDay()函數中從01/26/1994獲得01/26。 但是我似乎得到了第55行的「字符串沒有成員名爲'subtr'」的錯誤。有人可以引導我,因爲我是一個全新的程序員。此外,感謝您繼續提供幫助,因爲在收到本網站的輸入信息後,我的知識翻了一番。字符串沒有名爲'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; 
     } 
} 
+1

你需要'std :: string :: substr'。 – juanchopanza

回答

3

這是substr與第2 s。 SUB-S TRing。

+0

愚蠢的我。非常感謝你! – NewProgrammer

相關問題