2013-07-22 124 views
-2

用戶應輸入名稱,然後輸入成績。當用戶擊中-1時,可以不再爲該個人存儲更多等級。當用戶鍵入X時,他/她輸入總數,然後輸入-1表示不能再存儲更多等級。我不明白爲什麼我的for循環以奇怪的方式輸出。 names數組存儲應該轉到numbers數組的int值。將數值存儲在數組中

例如,輸入應該是這樣的:

Jane 3 4 -1 
Lane 4 5 -1 
X 10 10 -1 

輸出應該是這樣的:

Jane 3 4 
Jane had a 7/20 
Lane 4 5 
Lane had a 9/20 

這是迄今爲止我的工作:

#include <iostream> 
#include <cstring> 
#include <string> 

using namespace std; 

int main() 
{ 
string name = " "; 
string names[20]; 
int numbers[20]; 
int i = 0, j=0, number; 

do 
{ 
    cin >> name; 
    names[i]=name; 
    i++; 

    j=0; 
    if(number!=-1) 
    { 
     cin>> number; 
     numbers[j]=number; 
     j++; 
    } 
}while(name!="X"); 

for(int x = 0; x <= i; x++) 
{ 
    for(int y = 0; y <= j; y++) 
    { 
     cout << names[x]; 
    } 
} 

} 
+0

什麼是_your_理論? –

+0

你不用'y'就可以遍歷'y'。爲什麼? –

+0

如果您使用C++,那麼爲什麼不使用STL? –

回答

0

一問題與您的代碼是,

int i = 0, j=0, number; 

你在你的代碼中使用未初始化number

if(number!=-1) 

這包含垃圾值可共順便是-1