2009-11-18 47 views
0
/********************************************************************* 
*Program Name  : CSC 110 - 003 Store Unknown Number of Values 
*Author   : Anthony Small 
*Due Date   : Nov\17\09 
*Course/Section : CSC 110 - 003 
*Program Description: Store Unknown Number of Values in an Array 
* 
*BEGIN Lab 7 - CSC110-003 Store Unknown Number of Values 
* init Array to five 
* init Count to Zero 
* Get First Value or Quit 
* WHILE (Value is not Quit) 
*  Store Value into Arry 
*  Add One to Count 
* IF (Array is Full) 
*  Set Value to Quit 
*  Cout Full Message 
* ELSE Get Next Value or Quit 
* End IF 
* END WHILE 
* FOR (Set Value in the Array) 
*  Display Value 
* END FOR 
*End Lab 7 - Store Unknown Number of Values 
*********************************************************************/ 

#include <iostream> 
#include <iomanip> 
//#include <stdlib> 
#include <ctime> //or <ctime> 

using namespace std; 

int main() 
{ 
//local constants 
const int Quit = -1;       //Sentinal value        
const int SIZE = 5;        //Max number of inputs 
//local variables 
int Num ; 
int Count=0; 
int Array [SIZE]; 

//******************************************************************/ 
// Display Input 
cout << "Input first Number or Quit\n"; 
cin >> Num; 
while (Num != Quit); 

     Array [0] = Num;      //Store number into array 
     Count++;        //Add one to count 
    if (Count==SIZE-1)    
    { 
     (Num = Quit); 
     cout <<"Array is full"; 
    } 
    else cout <<"Enter next number or quit\n"; 
     cin>>Num;         //Input next number 

for (int pos = 0;pos < SIZE; pos++) 
    cout << Array [pos];    
return 0; 
//end main program 
} 
+1

請刪除/編輯它以使其更具可讀性,否則您可能會被下調至編碼器地獄。 – 2009-11-18 03:23:55

+1

您是否在編譯時打開了警告? – 2009-11-18 03:27:40

+1

您是否嘗試過調試?正如前面提到的那樣,清理它並提供迄今爲止嘗試的更多細節。記住,你只會在這裏向正確的方向推動。你不會讓某人爲你做功課。 – MadMurf 2009-11-18 03:28:35

回答

3
 
Hint #1 You need to add braces for the while loop... (and remove the semi-column) 
Hint #2 You need to use a different subscript (other that systematically 0 
     for storing into Array. 
+0

應該是SIZE(在SIZE = 5中的常量) – anthony 2009-11-18 03:51:00

+0

@anthony,請考慮使用Count。 – MadMurf 2009-11-18 03:54:05

+1

記住它需要不同每次通過循環 – MadMurf 2009-11-18 03:54:58

7

你實際上意味着:

while (Num != Quit) 
{ 
    // Code here... 
} 
1
  1. 雖然將循環將只執行1次。
  2. 每次執行循環時,都會覆蓋數組的第一個值。
+0

是的,我看到,現在感謝所有的幫助 – anthony 2009-11-18 03:52:55

2

看一看行

while (Num != Quit); 

想想也

Array [0] = Num; 

會在一個循環中做什麼

你怎麼看待下一個輸出/行動將在

cout <<"Array is full" 

縮進,括號等需要清理,使這做你想做的。

+0

亞仍然在風格程序應該停止後「數組已滿」 – anthony 2009-11-18 03:52:04

相關問題