i am writing a program whereby i have written the instructions the shell of the program, and have written code for inputting values, but how do i collect all the information i have inputted. I am aware it involves arrays but can you help and point me in the right direction ?

Here is part of my program:

#include <iostream.h>
#include <string>
#include <conio.h>
main()
{
int salescode, bikeprice, modelcode, quantity;
   string date;
   char answer; //either yes or no


   do
        //the instructions to enter the i.d. code will be repeated (do...while
        //loop) as long as the salescode is less than 1 or greater than 20.
   	{

      	cout << "\n\nPlease enter your identifiction code ";
         cin >> salescode;

         //the condition is tested
         if (salescode < 1 || salescode > 20)

         	{
            	cout << ("\nTHIS I.D. CODE IS NOT VALID. TRY AGAIN \n");
            }
      }
	//the instructions to enter the i.d. code will be repeated (do...while
   //loop) as long as the salescode is less than 1 or greater than 20.
   while (1 > salescode || salescode > 20);

   //another do...while loop
   do
   	{
      	cout << "\nPlease enter the bike price (one unit = 1 Euro) ";
      	cin >> bikeprice;

         if (bikeprice < 1 || bikeprice > 500)

         	{
            	cout << ("\nINVALID PRICE. Bike prices cannot be 0 or more than 500. TRY AGAIN \n");
            }
      }

   while (bikeprice < 1 || bikeprice > 500);

   //another do...while loop
   do
   	{
      	cout << "\nPlease enter the 3 digit model code ";
      	cin >> modelcode;

         if (modelcode < 0 || modelcode > 999)

         	{
            	cout << ("\nINVALID MODEL CODE. TRY AGAIN \n");
            }
      }

   while (modelcode < 0 || modelcode > 999);

   //another do...while loop
   do
   	{
         cout << ("\nPlease enter the quantity sold ");
      	cin >> quantity;

         if (quantity < 1 || quantity > 10)

         {
         	cout << ("\nINVALID QUANTITY. TRY A NUMBER FROM 1-10 \n");
         }
      }

   while (quantity < 1 || quantity > 10);

	do {


			cout << "\nPlease enter the date in this format dd/mm/yyyy: ";
         cin >> date;

			cout << "\nIs this the correct date ?\t" << date << "\tpress y/n: \n\n";
         cin >> answer;   //gets answer from user

			switch (answer)	{
         	case 'y':
            	cout << "\n\n\nThank You!\n\n";
               break;
            }
         }while (answer != 'y');
   }

So if you run my program as it is, how do i store the values entered for each of the commands prompted ?

I will need these values to be used later on to calculate the weekly sales.

Any ideas please ?
oh yeah, if i type in any other non integer, the program crashes.
what would you suggest.

I am new to c++ and programming
Thanks

Arrays are declared like this :

int int_array[10]; // this is an array of ten integers
char char_array[10]; // an array of chars

the general form being
data type array name [constant integer];

And for filling an array up one would try a loop among many other ways:

int array[10];
for (int i=0;i<10;i++)
  {
    array[i]=i;
  }

just a hint ..

Arrays are structures of information that can be accessed by index.

Declaring an array;

int numbers[10];

This declares an int array with 10 indices. They have not been assigned values yet, so it's necessary to access one index and assign a value;

numbers[0] = 1;

This assigns the value 1 to the int at index 0(the first index).
Array indices start from 0 to the their size minus one, 0 being the first, and 9 being the last. This is because the actual last index in the array(10) stores a special value that represents the end of array.

If you wanted to initialize every value within an array in a few statements, there are is a good way to do this. Note that this is a more advanced algorithm so you might not understand it, and should ignore it if you don't.

for(int i = 0; i < (sizeof(array) / sizeof(type)); i++){
array = value;
}

Example using array "numbers":

for(int i = 0; i < (sizeof(numbers) / sizeof(int)); i++){
    numbers[i] = 0;
}

Edited 6 Years Ago by batchprogram: Clarity

I understand the concept of arrays. As well as yourselves, the text book explained this to me, but applying it to my program is not explained.
how would i assign the values i input, into an array? if someone could show me an example for this piece of code, i might be able to do the rest myself....

#include <iostream.h>
#include <conio.h>
main()
{
int quantity;
do
{
	cout << ("\nPlease enter the quantity sold ");
	cin >> quantity;

if (quantity < 1 || quantity > 10)

	{
	cout << ("\nINVALID QUANTITY. TRY A NUMBER FROM 1-10 \n");
	}

}


while (quantity < 1 || quantity > 10);
getch();
}

for example for 5 different values entered

Just like a normal variable :p
This is an example code. if you need more help just ask .

const int size=10;
int value[size];
for (int i=0; i < size; i++)
{
  cout<<"Enter value - " << i << "- : " ;
  cin >> value[i]; //Assigning from input to array.
}

i'm sorry if i come across dumb, but how would i put your example code into a program like mine ?
i am confused.

#include <iostream.h>
#include <conio.h>
main()
{
int quantity[size];

const int size=10;
do
{
	cout << ("\nPlease enter the quantity sold ");
   for (int i=0; i < size; i++)
   	{
  			cout<<"Enter value - " << i << "- : " ;
  			cin >> quantity[i]; //Assigning from input to array.
		}

if (quantity < 1 || quantity > 10)

	{
	cout << ("\nINVALID QUANTITY. TRY A NUMBER FROM 1-10 \n");
   for (int i=0; i < size; i++)
   	{
  			cout<<"Enter value - " << i << "- : " ;
  			cin >> quantity[i]; //Assigning from input to array.
		}
	}

}


while (quantity < 1 || quantity > 10);
getch();
}

i tried this

To declare an array :
int numbers[10];

[10] being the number of elements stored in the array
Numbers being the name of the array

Let's say you have the numbers 1-10 stored in your array
To view all of these number, you can use a loop

First declare a counter
int x;

for(x=0; x<10; x++)
//the first element is stored in 0 while the last element in the array is NULL
cout<<number[x]<<endl;

This would display:
1
2
3
4
5
6
7
8
9
10


To input into the array you can use the previous loop or individually:

for(x=0; x<10; x++)
cin>>numbers[x];
//this will have you input until the array is full

Edited 6 Years Ago by Epsilon92: n/a

To declare an array :
int numbers[10];

[10] being the number of elements stored in the array
Numbers being the name of the array

Let's say you have the numbers 1-10 stored in your array
To view all of these number, you can use a loop

First declare a counter
int x;

for(x=0; x<10; x++)
//the first element is stored in 0 while the last element in the array is NULL
cout<<number[x]<<endl;

This would display:
1
2
3
4
5
6
7
8
9
10


To input into the array you can use the previous loop or individually:

for(x=0; x<10; x++)
cin>>numbers[x];
//this will have you input until the array is full

#include <iostream.h>
#include <conio.h>
main()
{
int quantity[10], x; //declare numbers[10] as an array and x as a counter
for(x=0; x<10; x++)

do
{
	cout << ("\nPlease enter the quantity sold ");
	cin >> quantity[x];

if (quantity < 1 || quantity > 10)

	{
	cout << ("\nINVALID QUANTITY. TRY A NUMBER FROM 1-10 \n");
	}

}


while (quantity < 1 || quantity > 10);
getch();
}

what am i doing wrong here ?

You have to use quantity with an index.quantity[x] for example.

if (quantity[x] < 1 || quantity[x] > 10)

@Dinglish,
why you are bothering so much with understanding this. Arrays if you feel is difficult, try to understand vectors. I bet, you feel bit hefty in hadling Vectors but once you are through a few examples, you will be out of the blue.

One more question, what happens if the number of entries are more than 10, will you go and change the array size once again. No problem like this in Vectors.

Am just giving a few lines code here. Practise vectors you will understand its flexibility.

typedef vector<int> vsalescode

cout<<"Enter the salescode";
int temp1 = 0;
cin>>temp1;
while(temp1)
{
 vsalescode.push_back(temp1);
}

//How to values back from the Vector
for(int i=0; i<vsalescode.size();i++)
{
 cout<<"Salescode[%d] value is %d"<<i+1<<vsalescode[i]<<endl;
}

Edited 6 Years Ago by bugista: n/a

I don't think you can just skip arrays while trying to understand vectors. I mean .. i didn't work for me :)

@bugista.
because i have to write a pseudocode along with my program.
We also using borland 5.02. so alot of code that 95% progammers use are not recognised.
its a pain in the ...

You have to use quantity with an index.quantity[x] for example.

if (quantity[x] < 1 || quantity[x] > 10)

ok caut_baia, i got it working now where the values are stored.

#include <iostream.h>
#include <conio.h>
main()
{
int quantity[10], x; //declare numbers[10] as an array and x as a counter
for(x=0; x<10; x++)

do
{
	cout << ("\nPlease enter the quantity sold ");
	cin >> quantity[x];

if (quantity[x] < 1 || quantity[x] > 10)

	{
	cout << ("\nINVALID QUANTITY. TRY A NUMBER FROM 1-10 \n");
	}

}


while (quantity[x] < 1 || quantity[x] > 10);
getch();
}

it loops until the 10th value is entered.
but my program requires me to enter values one at a time along with values for other variables.

so after i enter the weekly sales data in my program below (please run) ie. id code, price and quantity, i want to be able to go back to the original menu and be able to enter more values if needed.

#include <iostream.h>
#include <string>
#include <conio.h>

void WeeklySales(); //we make weeklysales a function here too
void yesnofunc();

main()
	{
   	int number;

      do
      	{      //displays the menu
         	cout << "******Sales System******";
            cout << ("\n\n");
      		cout << "1. Display Company Logo.\n"; endl;
      		cout << "2. Input/Validate weekly sales data.\n";endl;
      		cout << "3. Calculate weekly sales.\n";endl;
      		cout << "4. Display reciept.\n";endl;
      		cout << "5. SHUT DOWN & LOG OFF.\n";endl;
				cout << "\nEnter number...\n";endl;
         	cin >> number;
            cout << ("\n\n");

      		switch (number)  //<--the expression is a variable (number) & controls the switch
            //the value of (number) is tested against a list of constants.
            //When a match is found, the statement sequence assosciated with that match is executed
               {
      				case 1:    //<-----  const = 1
                     
            			cout << ("\n\tUU\tUU \tEEEEEEEE \tLL\t\t \SSSSSSS\n\tUU\tUU \tEE \t\tLL\t\tS\n\tUU\tUU \tEE \t\tLL\t\tS\n\tUU\tUU \tEEEEEEE \t\LL\t\t\ SSSSSS\n\tUU\tUU \tEE \t\tLL\t\t       S\n\tUU\tUU  \tEE  \t  \t\LL  \t\t       S \n\t UUUUUUUU   *\tEEEEEEEE  *\tLLLLLLL  *\tSSSSSSS   *\n\n\n\n\n");break;

            		case 2:   //<------const = 2. if match found, executes, then calls the weeklysales function below

            			cout << ("Weekly sales data\n\----------------- ");
                                WeeklySales();  //<-------------   function call here
                                break;

            		case 3:

            			cout << ("Calculate weekly sales "); break;

            		case 4:

            			cout << ("Display receipt "); break;

            		case 5:

            			cout << ("Goodbye, and thank you for using U.E.L.S. ");break;

                  //default statement sequence is executed if no matches are found
            		default:

            			cout << ("Enter a number from 1-5 only!\n\n\n\n\n\n");

         		}
      	} while (number !=5); //program will NOT stop looping till 5 is entered
      getch();
   }

void yesnofunc()
{
	char answer;

do {
		cout << "\nIs this correct ?\n";
   	cin >> answer;

   	switch (answer) {
      	case 'y':
         	cout << "\nThank you!!\n";
            break;

      }
	}while (answer != 'y');
}

void WeeklySales() //<--------weekly sales function not part of main() function
{                  //when function is completed, goes back to case 3:
//declare variables
	int salescode, bikeprice, modelcode, quantity;
   string date;
   char answer; //either yes or no
   bool check;
                    //<-----------------I NEED HELP FROM HERE-----------------------------------------------------------------------------

   do
        //the instructions to enter the i.d. code will be repeated (do...while
        //loop) as long as the salescode is less than 1 or greater than 20.
   	{

      	cout << "\n\nPlease enter your identifiction code ";
         cin >> salescode;   //<----i'm trying to store each salescode value so that when option 3 is chosen from the menu the weekly sales can be calculated once the salescode is entered

         //the condition is tested
         if (salescode < 1 || salescode > 20)

         	{
            	cout << ("\nTHIS I.D. CODE IS NOT VALID. TRY AGAIN \n");
            }
      }
	//the instructions to enter the i.d. code will be repeated (do...while
   //loop) as long as the salescode is less than 1 or greater than 20.
   while (1 > salescode || salescode > 20);

   //another do...while loop
   do
   	{
      	cout << "\nPlease enter the bike price (one unit = 1 Euro) ";
      	cin >> bikeprice;

         if (bikeprice < 1 || bikeprice > 500)
                //<-------------also trying to store the bike prices that go with the salescode inputted above
         	{
            	cout << ("\nINVALID PRICE. Bike prices cannot be 0 or more than 500. TRY AGAIN \n");
            }
      }

   while (bikeprice < 1 || bikeprice > 500);

   //another do...while loop
   do
   	{
      	cout << "\nPlease enter the 3 digit model code ";
      	cin >> modelcode;

         if (modelcode < 0 || modelcode > 999)

         	{
            	cout << ("\nINVALID MODEL CODE. TRY AGAIN \n");
            }
      }

   while (modelcode < 0 || modelcode > 999);

   //another do...while loop
   do
   	{
         cout << ("\nPlease enter the quantity sold ");
      	cin >> quantity;

         if (quantity < 1 || quantity > 10)

         {
         	cout << ("\nINVALID QUANTITY. TRY A NUMBER FROM 1-10 \n");
         }
      }

   while (quantity < 1 || quantity > 10);

	do {
			check = true;//check to see whether to stay in loop or not

			cout << "\nPlease enter the date in this format dd/mm/yyyy: ";
         cin >> date;

			cout << "\nIs this the correct date ?\t" << date << "\tpress y/n: \n\n";
         cin >> answer;   //gets answer from user

			switch (answer)	{
         	case 'y':
            	cout << "\n\n\nThank You!\n\n";
               break;
            }
         }while (answer != 'y');
   }

i dunno i am baffled :confused:

How are you testing the loop conditions ? Does a number smaller than 1 makes sense? I don't really understand what you are trying to do but you should check your loop conditions and then we'll talk more. Also between lines 16 and 21 you should place an insertion operator before the 'endl' manipulator. Like this ... << endl;

This article has been dead for over six months. Start a new discussion instead.