开发者

newbie needs help i can't find whats wrong. i enter num and it stops

/*********************************************************************
 *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 Val开发者_StackOverflowue 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
}


When you do

while   (Num != Quit);

Do you actually meant:

while (Num != Quit)
{
  // Code here...
}


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.


Have a look at the line

while   (Num != Quit);

also think about what

Array [0] = Num;

is going to be doing in a loop

what do you think the next output/action is going to be after

cout <<"Array is full"

indenting, parentheses etc need to be cleaned up to make this do what you want.


  1. While will loop will only execute 1 time.
  2. You will overwrite the first value of the array each time you execute the loop.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜