2016-11-14 14 views
0

私はエラーが発生しました:宣言されていない識別子 'again'の使用。 私は答えを得た後、int mainからvoidへと何度も行き来しようとしています。 なぜそれが動作しないか私に説明してください。ありがとう。ここで関数と未使用の式エラーを呼び出す方法

は私の完全なコードです:

あなたはそれを呼び出す前に、 "もう一度" あなたのfonctionを宣言する必要が
#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

#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関数を複数回呼び出すことができます(これは定義上、プログラムのメインエントリです)。私の意見では、メイン関数の中で、ユーザーが情報を追加したいかどうかを知るためにテストケースでループを使用するaskInformationのような別の関数を呼び出す必要があります。

関連する問題