2013-11-26 142 views
1

我正在試圖使用二維數組和嵌套循環來打印出12年的銷售額,爲期三年。我很困惑。有人能告訴我使用這些方法我的代碼有什麼問題。我不想要替代品。二維數組和嵌套循環

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

using namespace std; 
int x = 0; 
int v = 0; 
int y = 0; 
int sum = 0; 
const int year = 3; 
const int month = 12; 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    int sales[year][month]; 
    char * date[12] = {"january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"}; 
    for(int z = 0; z < 3; z++) 
    { 
     { 
      cin >> v; 
      sales * year[z] = v; 
     } 

     for(int x = 0; x < 12; x++) 
     { 
      cout << "Please enter the sales for month " << date[x] << ":\n"; 
      cin >> y; 
      sales * month[x] = y; 
      sum += y; 
     } 
    } 
    cout << "There are the sales of the c++ crook: \n"; 

    cout << sales[3][12] << endl; 
    //cout << "Month 1 = " << year[0] << " " << month[0] << endl; 
    //cout << "Month 2 = " << year[0] << " " << month[1] << endl; 
    //cout << "Month 3 = " << year[0] << " " << month[2] << endl; 
    //cout << "Month 4 = " << year[0] << " " << month[3] << endl; 
    //cout << "Month 5 = " << year[0] << " " << month[4] << endl; 
    //cout << "Month 6 = " << year[0] << " " << month[5] << endl; 
    //cout << "Month 7 = " << year[0] << " " << month[6] << endl; 
    //cout << "Month 8 = " << year[0] << " " << month[7] << endl; 
    //cout << "Month 9 = " << year[0] << " " << month[8] << endl; 
    //cout << "Month 10 = " << year[0] << " " << month[9] << endl; 
    //cout << "Month 11 = " << year[0] << " " << month[10] << endl; 
    //cout << "Month 12 = " << year[0] << " " << month[11] << endl; 

    //cout << "Month 1 = " << year[1] << " " << month[0] << endl; 
    //cout << "Month 2 = " << year[1] << " " << month[1] << endl; 
    //cout << "Month 3 = " << year[1] << " " << month[2] << endl; 
    //cout << "Month 4 = " << year[1] << " " << month[3] << endl; 
    //cout << "Month 5 = " << year[1] << " " << month[4] << endl; 
    //cout << "Month 6 = " << year[1] << " " << month[5] << endl; 
    //cout << "Month 7 = " << year[1] << " " << month[6] << endl; 
    //cout << "Month 8 = " << year[1] << " " << month[7] << endl; 
    //cout << "Month 9 = " << year[1] << " " << month[8] << endl; 
    //cout << "Month 10 = " << year[1] << " " << month[9] << endl; 
    //cout << "Month 11 = " << year[1] << " " << month[10] << endl; 
    //cout << "Month 12 = " << year[1] << " " << month[11] << endl; 
    //cout << "The annual sales for c++ crook is: " << sum << " ;]"; 
    cin.get(); 
    cin.get(); 


    return 0; 
} 
+0

爲什麼在兩個const int變量('month'和'year')上使用'operator []'?關於不想「替代」,你想要你的代碼編譯或不? – WhozCraig

+2

您能告訴我們您遇到的問題或您遇到的錯誤嗎? – theharshest

+2

'sales * year [z] = v;'? – John3136

回答

2

幾件事情:

1)要確保表達式的左邊的東西是一個有效的「左值」 - 也就是說,它轉化爲一個位置,在那裏可以存儲評估RHS的結果。像

sales *month[x] = v; 

不符合這一點。

另一個錯誤:當你聲明數組

sales[year][month]; 

,你需要確保兩個yearmonth存在(已申報),並具有有效的值(也許312?) - 你有數組稱爲date但你是指month

sales * month[x] = v; 

正如我所說,你不能只是乘上的事情等式的左邊。你可能會考慮

sales[year][month] = v; 

在你的情況,你的外環z去從02, - 這可能是你year;內循環從011,所以我認爲這是一個月。然後,你可以做

sales[z][x] = y; 

這可能是你真正想要錄製的「年,這些銷售數字適用於」,在這種情況下,你需要創建一個數組

salesYears[3]; 

並存儲那裏的價值。它是提示用戶,當你期望輸入一個很好的主意 -

std::cout << "Please enter the year of the sales" << std::endl; 

這些都只是一些指點。你的代碼真的很亂。請記住:

Declare all variables 
Make sure all arrays are the right size 
Prompt for inputs 
Check that inputs are valid 
Address 2D arrays with arrayName[index1][index2] 
Thing on left hand side of equation must be "valid lvalue" 
You might need additional variables to store both the year and the sales 
+0

https://github.com/ GuangchuangYu/CxxPrimerPlus/tree/master/chapter5 我找到了問題的解決方案。我的代碼非常混亂。我想我需要回顧過去的五章,並做一些額外的練習。我剛剛從本書開始學習C++。謝謝你的幫助。 – AEGIS