Thread: Arrays and storing values

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    25

    Arrays and storing values

    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:

    Code:
    #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

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    how do i store the values entered for each of the commands prompted ?
    They are already being stored, when you type cin >> salescode it saves the users input into the variable salescode .. all your information is saved in your declared variables
    Code:
    int salescode, bikeprice, modelcode, quantity;
    oh yeah, if i type in any other non integer, the program crashes.
    what would you suggest.
    You will have to take input and save it into a string, then examine the string to confirm that all characters are in fact numbers.
    There is a method called isdigit which is in <cctype> , so you can make a method called isNum() and it would look like this
    Code:
    bool isNum(string input)
    {
       for(int i=0; i < input.length(); ++i)
       {
       if( !isdigit(input[i]) )
          return false;
       }
       return true;
    }
    isdigit - C++ Reference

    There might be another simply way but I cant think of one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. storing strings in arrays
    By smp in forum C Programming
    Replies: 6
    Last Post: 12-16-2008, 09:37 AM
  2. Storing input values while iterating
    By russel1013 in forum C Programming
    Replies: 11
    Last Post: 07-29-2008, 08:32 AM
  3. Help passing and storing
    By devilsknight in forum C Programming
    Replies: 7
    Last Post: 07-03-2006, 03:20 AM
  4. Storing values in arrays
    By Ripper1 in forum C++ Programming
    Replies: 3
    Last Post: 08-25-2003, 11:04 AM
  5. integers storing as symbols in arrays
    By rjcarmo in forum C++ Programming
    Replies: 4
    Last Post: 05-19-2003, 01:17 AM