2016-11-14 38 views
0

我收到一個錯誤消息:再次使用未聲明的標識符。 我想從int main中獲取答案後再次返回void。 請向我解釋爲什麼它也不起作用。謝謝。如何調用函數和未使用的表達式錯誤

這裏是我的全碼:

#include <iostream> 
#include <vector> 
#include <iomanip> 
#include <algorithm> 
#include <string> 

using namespace std; 

{ 
    string answer; 
    cout << "Would you like to enter another set of data? Y or N?" << endl; 
    cin << answer; 
    string yes = "Yes"; 
    string no = "No"; 
    if(a == yes) 
    { 
     main(); 
    } 
} 
int main() 
{ 
    cout << "Kaitlin Stevers" << endl; 
    cout << "Exercise 11 - Vectors" << endl; 
    cout << "November 12, 2016" <<endl; 
    cout << endl; 
    cout << endl; 
    int size; 
    cout << " How many numbers would you like the vector to hold? " << endl; 
    cin >> size; 
    vector<int> numbers; 
    int bnumbers; 

    for (int count = 0; count < size; count++) 
    { 
     cout << "Enter a number: " << endl; 
     cin >> bnumbers; 
     numbers.push_back(bnumbers); // Adds an element to numbers 
    } 
    //display the numbers stored in order 
    cout << "The numbers in order are: " << endl; 
    for(int bcount = 0; bcount < size; bcount++) 
    { 
     cout << numbers[bcount] << " "; 
    } 
    cout << endl; 
    //display the numbers stored reversed 
    cout << "Here are the numbers in reverse order: " << endl; 

    reverse(numbers.begin(), numbers.end()); 
    for(int rcount = 0; rcount < size; rcount++) 
    { 
     cout << numbers[rcount] << " "; 
    } 
    cout << endl; 
    again(); 
    return 0; 
    } 
    void again() 

}

+0

沒關係我知道我做錯了什麼。 –

回答

1

你需要調用它之前聲明 「再次」 你fonction:

#include <iostream> 
#include <vector> 
#include <iomanip> 
#include <algorithm> 
#include <string> 

using namespace std; 

void again(); 

int main() 
{ 
    cout << "Kaitlin Stevers" << endl; 
    cout << "Exercise 11 - Vectors" << endl; 
    cout << "November 12, 2016" <<endl; 
    cout << endl; 
    cout << endl; 
    int size; 
    cout << " How many numbers would you like the vector to hold? " << endl; 
    cin >> size; 
    vector<int> numbers; 
    int bnumbers; 

    for (int count = 0; count < size; count++) 
    { 
     cout << "Enter a number: " << endl; 
     cin >> bnumbers; 
     numbers.push_back(bnumbers); // Adds an element to numbers 
    } 
    //display the numbers stored in order 
    cout << "The numbers in order are: " << endl; 
    for(int bcount = 0; bcount < size; bcount++) 
    { 
     cout << numbers[bcount] << " "; 
    } 
    cout << endl; 
    //display the numbers stored reversed 
    cout << "Here are the numbers in reverse order: " << endl; 
     reverse(numbers.begin(), numbers.end()); 
    for(int rcount = 0; rcount < size; rcount++) 
    { 
     cout << numbers[rcount] << " "; 
    } 
    cout << endl; 
    again(); 
    return 0; 
} 

void again() 
{ 
    string answer; 
    cout << "Would you like to enter another set of data? Y or N?" << endl; 
    cin >> answer; 
    string yes = "Yes"; 
    string no = "No"; 
    if(answer == yes) 
    { 
     main(); 
    } 
} 

然而,這是一個奇怪的事情使用你想要做什麼的遞歸性以及多次調用main func(這是根據定義,程序的主要入口)。在我看來,你應該在你的主函數中調用另一個名爲askInformation的函數,它使用循環和測試用例來知道用戶是否想要添加信息。

相關問題