storing values. ARRAYS ETC

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#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
Hi! Significant improvement to the size of your code can be achieved with the use of functions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <string>

using namespace std;

int get_int(const char * prompt, const char * error, int min=INT_MIN, int max=INT_MAX);

int main()
{
    int salescode, bikeprice, modelcode, quantity;
    string date;
    char answer; //either yes or no

    salescode=get_int("Please enter your identification code: ",
        "THIS I.D. CODE IS NOT VALID. TRY AGAIN",1,20);

    bikeprice=get_int("Please enter the bike price (one unit = 1 Euro): ",
        "INVALID PRICE. Bike prices cannot be 0 or more than 500. TRY AGAIN",1,500);

    modelcode=get_int("Please enter the 3 digit model code: ",
        "INVALID MODEL CODE. TRY AGAIN",0,999);

    quantity=get_int("Please enter the quantity sold: ",
        "INVALID QUANTITY. TRY A NUMBER FROM 1-10",1,10);

    while (true)
    {
        cout << "Please enter the date in this format dd/mm/yyyy: ";
        cin >> date;

        cout << "Is this the correct date ?\t" << date << "\t(press y/n): ";
        cin >> answer;   //gets answer from user

        if (answer=='y') {cout << "Thank You!" << endl; break;}
    }

    return 0;
}

int get_int(const char * prompt, const char * error, int min, int max)
{
    int integer;

    while (true)
    {
        cout << prompt;
        cin >> integer;

        //if the user enters non-numeric input cin.good() returns 0, so !cin.good() evaluates to true
        //cin.get()!='\n' checks if the user enters "more" info than he is asked to...
        if (!cin.good() || cin.get()!='\n')
        {
            //clear cin so it can be used for input again
            cin.clear();

            //ignore every character until the end of line (including the newline character)
            while(cin.get()!='\n');

            cout << "please don't enter crap..." << endl;
            continue;
        }

        if (integer<min || integer>max)
        {
            cout << error << endl;
            continue;
        }

        break;
    }

    return integer;
}


Here I made a simple integer input function which also takes care of crap input (i.e. the user enters non-numeric data or tries to enter more than one integer values at once etc...) If you're not familiar with functions try taking a look here:

http://cplusplus.com/doc/tutorial/functions/
http://cplusplus.com/doc/tutorial/functions2/

So if you run my program as it is, how do i store the values entered for each of the commands prompted ?
To be honest I didn't quite get that one... Each value the user enters is stored in the corresponding variable. What exactly do you mean?...
Last edited on
ok, my variables in my program would be salescode, bikeprice, modelcode, quantity right ?

i could output these values.
But when the program is looped again and different values are entered as the values of variables the previous values will be "written over" and disappear.

i wanted to know how i could recall any values that might have been entered earlier.

u understand what i am trying to say ?
Ok, I got it. If you know the number of iterations (i.e. how many times the loop will run) in compilation time, let's say it's n (where n is a (literal) constant), use an array with n elements for each variable. If you don't know this number use vectors and push_back the newly inputted values in each iteration.

Info on arrays -> http://cplusplus.com/doc/tutorial/arrays/
Info on vectors -> http://cplusplus.com/reference/stl/vector/
Last edited on
Topic archived. No new replies allowed.