2016-12-05 6 views
-3

私は現在C++を勉強していますが、少し遅れてしまったので、私の質問が明らかであれば謝ります。プログラムはコンパイルされますが、スイッチは無視されます。

私は、学生の名前、GPA、入学年を求め、その人のために生成されたランダムな5桁の数字を取得するプログラムを作成する必要があります。学生の数は42を超えません。

私のプログラムはコンパイルされています(何とか)、私は無効なメニュー選択でエラーを出すことができます。

多分私は何かが不足している、これが私が助けを必要とする理由です。

ここに私のコードです。

#include <iostream> 
#include <stdlib.h> 
#include <time.h> 
using namespace std; 




//print all the menu options 
void print_menu() 
{ 
    cout<<"\nRCNJ Registrar Menu:"<<"\n" 
     <<"\n" 
     <<"[1] Add a student"<<"\n" 
     <<"[2] Display all students"<<"\n" 
     <<"[3] Display by year"<<"\n" 
     <<"[4] Display statistics"<<"\n" 
     <<"[5] Quit"<<"\n"; 

} 



//get and return the student's name 
void get_name(string& student_name) //call student_name after that. 
{ 
    cout<<"Please enter the sudent's name: "; 
    cin >> student_name; 
    cout<<"\n"; 
} 

//validate and return gpa 
double get_gpa() 
{ 
    double student_gpa = 0; 

     cout<<"Please enter the GPA: "; 
     cin >>student_gpa; 
     cout<<"\n"; 

      while (student_gpa > 4 || student_gpa < 0) 
      { 
       cout<<"Please enter a valid GPA for the student (0.00 - 4.00): "; 
       cin >> student_gpa; 
       cout<<"\n"; 
      } 

    return student_gpa; 
} 

//validateand return year 
int get_year() 
{ 
    int student_year = 0; 

    cout<<"Please enter the year: "; 
    cin >> student_year; 
    cout<<"\n"; 

     while (student_year >2016 || student_year <1972) 
     { 
      cout<<"Please enter a valid year (min 1972, max 2016): "; 
      cin >> student_year; 
      cout<<"\n"; 
     } 

    return student_year;  
} 

//generate the student's R# 
int generate_number() 
{ 
    int r_number; 

     srand (time(NULL)); 
     r_number = rand() % 89999 + 10000; 

    return r_number; 
} 


//save info. Include get_name, get_gpa, get_year 
void input_new_student() 
{ 
    string student_name; 
    double student_gpa; 
    int student_year; 
    int r_number; 

    int s_name, s_gpa, s_year, r_num; 



    get_name(student_name); 
    get_gpa(); 
    get_year(); 
    generate_number(); 


} 


//display all students in the proper format 
void print_all() 
{ 

} 

//get a year as selection and print all students that are the same year 
void print_by_year() 
{ 

} 


//display statistics based on entered students 
void print_statistics() 
{ 

} 

//validate and return the menu option selected by the user. 
//it should call print_menu defined earlier 
int get_selection(int menu_choice) 
{ 
    menu_choice = 0; 

    cout<<"\n" 
     <<"Selection: "; 
    cin >> menu_choice; 
    cout<<"\n"; 

     while (menu_choice > 5 || menu_choice< 1) 
     { 
      cout<<" Menu choice is invalid. Please re-enter (1 - 5): "; 
      cin>> menu_choice; 
      cout<<"\n"; 
     } 

    return menu_choice; 
} 


int main() 
{ 
    string student_name; 
    double student_gpa; 
    int student_year; 
    int r_number; 
    int menu_choice; 

    int s_name=0; 
    int s_gpa=0; 
    int s_year=0; 
    int r_num=0; 

       string nameArray[42]; 
      s_name++; 

      double gpaArray[42]; 
      s_gpa++; 

      int yearArray[42]; 
      s_year++; 

      int ramapoArray[42]; 
      r_num++; 

     print_menu(); 
     get_selection(menu_choice); 

      switch (menu_choice) 
      { 
       case 1: 
       input_new_student(); 

       nameArray[s_name] = student_name; 
       gpaArray[s_gpa] = student_gpa; 
       yearArray[s_year] = student_year; 
       ramapoArray[r_num] = r_number; 
       break; 
      } 







return 0; 
} 
+2

このような問題を解決する適切なツールは、デバッガです。スタックオーバーフローを尋ねる前に、コードを一行ずつ進める必要があります。詳しいヘルプは、[小さなプログラムをデバッグする方法(Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)を参照してください。最低限、問題を再現する[最小、完全、および検証可能](http://stackoverflow.com/help/mcve)の例と、その問題を再現するためのデバッガ。 –

+1

4つの別々の配列を管理するのではなく、 'struct'を作成し、' struct'の配列を作ります。 – PaulMcKenzie

+1

*それを実行しないで*、 'void f(int i){i = 5;}を書くときint main(){int x = 7; f(x) cout << x;} 'それは5または7を印刷すると思いますか? – immibis

答えて

0

私はコメントする権限がありませんので、ここに追加してください。 あなたのメイン()、 get_selection(menu_choice); switch(menu_choice) menu_choiceを返しますが、値を取得することはできません。初期化されていないのでガベージ値を使用して終了します。 menu_choiceのアドレス/参照を渡すか、戻り値のどちらかを使って、2つの方法で実行できます。私はあなたのプログラムの残りの部分を通過していないが、これらのいずれかを試してみてください。 他の人に示唆されているように、デバッガを試してみてください。 gdb?

+0

スイッチ内には、メニューからユーザーが選択した内容をプログラムが知る方法がないと言っていますか? –

+0

はい、これを試してください menu_choice = get_selection(); そして、get_selection()にパラメータを渡さないでください –

+0

それは働いた。ありがとうございました。今は配列が機能しているかどうかを調べるだけです。私は構造体を使ってみましたが、私はそれに精通していません。 –

関連する問題