2014-04-10 63 views
-5

我的代碼有什麼問題。我想添加10次數據並查看詳細信息,但我無法做到。任何人都可以幫助我嗎?不能超過1次C++

#include <iostream.h> 
#include <conio.h> 
#include<string.h> 

void load_menu(void); 
void analysis(void); 
void details(void); 


int main(int argc, char** argv) 
{ 
    load_menu(); 
    return 0; 
} 
void load_menu(void) 
{ 
    int choice; 

    while (1) 
     { 
      cout<<"*******************************************************************************\n"; 
      cout<<"*       Welcome to SYSTEM analyze       *\n"; 
      cout<<"*******************************************************************************\n\n"; 

      cout<<"   Welcome to SYSTEM analyze \n\n"; 
      cout<<"1.Voter Analysis 1\n2.Voter Details 2\n3.Exit 3\n\n"; 
      cout<<"*******************************************************************************\n"; 
      cout<<"Please enter your Choose : ";cin>>choice; 
      switch(choice) 
       { 
       case 1 : 
        analysis(); 
        break; 
       case 2: details(); 
        break; 
       case 3: cout<<"Going out !\n"; 
        exit(1) ; 
        break; 
       default: cout<<"\n\n\n\n\n\n\n"; 
        cout<<"*******************************************************************************\n"; 
        cout<<"*       PLEASE INSERT AGAIN !!       *\n"; 
        cout<<"*******************************************************************************\n\n"; 
        cout<<"\n\n\n\n"; 
        break; 
       } 

     } 

} 

void analysis(void) 
{ 
    char voterid[10]; 
    char votername[30]; 
    char voteraddr[30]; 
    char phone[15]; 
    int age; 
    char status[20]; 
    { 
     cout<<"get voterid"; 
     cin>>voterid; 
     cout<<"voter's name"; 
     cin>>votername; 
     cout<<"voter's address"; 
     cin>>voteraddr; 
     cout<<"phone number"; 
     cin>>phone; 
     cout<<"voter's age"; 
     cin>>age; 
     if(age>18) 
      strcpy(status,"eligible"); 
     else 
      strcpy(status,"not eligible"); 
     for(int i=0;i<10;i++); 
    } 
} 
void details(void) 
{ 
    char voterid[10]; 
    char votername[30]; 
    char voteraddr[30]; 
    char phone[15]; 
    int age; 
    char status[20]; 
    { 
     cout<<"voter's information\n"; 
     cout<<"--------------------\n"; 
     cout<<"voter id:"<<voterid<<"\n"; 
     cout<<"voter name:"<<votername<<"\n"; 
     cout<<"voter addr:"<<voteraddr<<"\n"; 
     cout<<"phone no:"<<phone<<"\n"; 
     cout<<"voter's age:"<<age<<"\n"; 
     cout<<"status:"<<status<<"\n"; 
     cout<<"-----------------\n"; 
    } 
} 
+1

你在'for'循環中沒有做任何事情。 – Barmar

+1

你的變量對每個函數都是局部的。當您在'analytics'中設置'voterid'時,它將不會在'details'中顯示。您必須將它們放入從一個函數返回並傳遞給另一個函數的結構中,或者使用全局變量。 – Barmar

+0

如果你想添加多個數據,你需要創建一個包含所有這些數據的結構數組。你真的錯過了一些基本概念,我不認爲SO是合適的論壇,試圖教你編程的基礎知識。 – Barmar

回答

0

有很多錯誤的驗證碼

  • 所有變量voterid [10]; votername [30]; voteraddr [30];電話[15];年齡;狀態[20]是功能analysis和功能details的本地功能。因此,您存儲在analysis函數的那些變量中的數據在details中未被訪問。所以,讓這些變量全球化。
  • analysis中,您已將for陳述放在錯誤的地方。 for循環沒有迭代。這就是爲什麼你無法多次插入數據的原因。
  • details中,沒有for循環來從數組中獲取數據。
  • voterid是一個數組。因此,請使用索引將值存入其中 - 例如:voterid[i]其中i是索引。
  • 您可以在頭文件中使用iostream而不是iostream.h