2013-07-17 81 views
-7

我對C++的經驗非常有限,我想從代碼中替換「goto」構造。對於重構過於替換goto聲明

int main() 
{ 
    int count; 
    int countSub = 0; 
    int userCount = 0; 
    int roleCount = 0; 
    int parentGroup; 
    cout<<"enter a number of parentGroup"<< endl; 
    cin>> parentGroup; 
    int subGroup; 
    cout<<"enter a number sub Group"<< endl; 
    cin>> subGroup; 
    int rolePerGroup; 
    cout<<"enter a number role per Sub Group"<< endl; 
    cin>> rolePerGroup; 
    int userPerGroup; 
    cout<<"enter a number user per Role"<< endl; 
    cin>> userPerGroup; 

    do 
    { 
     if (parentGroup == 0) 
     { 
      cout<<"Error"<<endl; 
      exit(EXIT_FAILURE); 
     } 
     else 
     { 

      for(count=1;count <= parentGroup; count ++) 
      { 
       do 
       { 
        if(subGroup == 0) goto hello; 

        else 
        { 
         for(countSub = 1;countSub<=subGroup; countSub ++) 
         { 
          do 
          { 
           hello: 
           if (rolePerGroup == 0) 
           { 
            cout<<"Error"<<endl; 
            exit(EXIT_FAILURE); 
           } 

           else 
           { 

            for(roleCount = 1; roleCount<=rolePerGroup; roleCount ++) 
            { 

             do 
             { 
              if(userPerGroup == 0) goto print; 
              else 
              { 

                for(userCount = 1; userCount<=userPerGroup; userCount ++) 
               { 

                 print: 
    cout<<"Parent groups are: "<< count <<" | "<<"Sub group are : "<<countSub<<" | "<<"Role per Sub group are : "<< roleCount <<" | "<<"User per role are : "<< userCount <<endl; 
               }} 
              userCount --; 
             }while(userCount < 0); 
            }} 
           roleCount --; 
          }while(roleCount < 0); 
         }} 
        countSub --; 
       }while(countSub < 0); 
      }} 
     count --; 
    }while(count < 0); 
} 
+5

我的第一個建議是這個分裂成多個功能。這就是6​​個嵌套for循環? – Borgleader

+1

創建打印中正在執行的任何功能,並調用該函數而不是「goto」。 –

+0

if(userPerGroup == 0)轉到打印; –

回答

0

請讓我知道如果我錯過了什麼

#include "stdafx.h" 
#include <iostream> 

using namespace std; 


void Print(int count, int countSub, int rolePerGroup, int userCount, int userPerGroup) 
{ 
for(int roleCount = 1; roleCount<=rolePerGroup; roleCount ++) 
{ 
    if(userPerGroup == 0) 
    { 
     cout<<"Parent groups are: "<< count <<" | "<<"Sub group are :  "<<countSub<<" | "<<"Role per Sub group are : "<< roleCount <<" | "<<"User per role are : "<< userCount <<endl; 
     continue; 
    } 

    for(userCount = 1; userCount<=userPerGroup; userCount ++) 
     cout<<"Parent groups are: "<< count <<" | "<<"Sub group are : "<<countSub<<" | "<<"Role per Sub group are : "<< roleCount <<" | "<<"User per role are : "<< userCount <<endl; 
} 
} 

int main() 
{ 
int userCount = 0; 
int roleCount = 0; 
int parentGroup; 
cout<<"enter a number of parentGroup"<< endl; 
cin>> parentGroup; 

if (parentGroup == 0) 
{ 
    cout<<"Parent Group should not be zero"<<endl; 
    exit(EXIT_FAILURE); 
} 

int subGroup; 
cout<<"enter a number sub Group"<< endl; 
cin>> subGroup; 
int rolePerGroup; 
cout<<"enter a number role per Sub Group"<< endl; 
cin>> rolePerGroup; 

if (rolePerGroup == 0) 
{ 
    cout<<"Role per Group should not be zero"<<endl; 
    exit(EXIT_FAILURE); 
} 

int userPerGroup; 
cout<<"enter a number user per Role"<< endl; 
cin>> userPerGroup; 


for(int count=1;count <= parentGroup; count ++) 
{ 
    if(subGroup == 0) 
    { 
     Print(count, 0, rolePerGroup, userCount, userPerGroup); 
     continue; 
    } 

    for(int countSub = 1;countSub<=subGroup; countSub ++) 
    { 
     Print(count, countSub, rolePerGroup, userCount, userPerGroup); 
    } 
} 
} 
+0

謝謝它的作品,因爲我想要之前 – ephti2000

+0

任何錯誤答案是不被接受的? –

+0

沒有它完美的工作,但我想輸出的「用戶每角色是:1 ...」,而不是打印重複的數字,我想要一個有爭議的,例如,如果我們輸入2,2,2,2我想用戶部分要打印1到16 ...你可以有任何想法如何做到這一點 – ephti2000

0

一個要刪除的goto的任何建議:

if(subGroup == 0) goto hello; 
else 
{ 
    for(countSub = 1;countSub<=subGroup; countSub ++) { 
     do { 
      hello: 
... 

所以,如果我理解正確的話,你想,如果subGroup等於0或1,那麼你做1次迭代。轉到一次就進入for-loop

我建議你使用一個變量,我會打電話N一個好名字),做:

int N = subGroup == 0 ? 1 : subGroup; 
for(countSub = 1;countSub <= N; countSub++) { 

我看不到了,我不會嘗試修復等一但我猜這是同一個問題。