/*********************************************************************
*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
}
views:
102answers:
4
+3
A:
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.
mjv
2009-11-18 03:29:15
should it be SIZE as in the (const in SIZE =5)
anthony
2009-11-18 03:51:00
@anthony, think about using Count.
MadMurf
2009-11-18 03:54:05
remember it needs to be different every time through the loop
MadMurf
2009-11-18 03:54:58
+7
A:
When you do
while (Num != Quit);
Do you actually meant:
while (Num != Quit)
{
// Code here...
}
rockacola
2009-11-18 03:29:46
+1
A:
- While will loop will only execute 1 time.
- You will overwrite the first value of the array each time you execute the loop.
rerun
2009-11-18 03:30:15
+2
A:
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.
MadMurf
2009-11-18 03:30:56