Thread: updating an array problem

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    27

    updating an array problem

    While trying to incorporate an error check to an update list function, the problem is that the list doesn't update. If the updated stock number that you enter is not a number, or another item of stock already has that number then the user is asked to input another number.

    When the user enters a successful number, the screen clears and the list is shown. The problem is that in the updated number does not appear in the list.

    Code:
    /UPDATE DETAILS OF ITEMS ON THE LIST
    void update( int numitems)
    {
        // declare local variables
        int i,j;
    	char tempstr[80];
    	char updatechoice[80];
    	
            do{       	
               system ("cls");     	    
               listitems (numitems);
               
               cout <<("\n\t\t ======UPDATE AN ITEM IN STOCK======\n\n");
    		   cout << "\t\t 1.  Update stock number\n";
    		   cout << "\t\t 2.  Update description\n";
    		   cout << "\t\t 3.  Update supplier\n";
    		   cout << "\t\t 4.  Update quantity\n";
    		   cout << "\t\t 5.  Update price\n";
    		   cout << "\t\t Q.  End update \n";                        		                
    		   cout << "\n\t\t ================\n";
    		   cout << "\n\t\t Press appropriate key to select > ";
               cin >> updatechoice;
    		     
    		   if (!strcmp(updatechoice, "1"))
    		          {
                       cout <<("\nEnter stock number of item to be updated  ...  ");
                       cin >> tempstr;
    	               j = atoi(tempstr);
    	               cout <<"\nSearching for item number " << j;
    	                for(i=0;i < numitems; i++)
                        if(j == slist[i].stocknumber)
                           {
    	    	            cout <<"\nItem is: "  ;                        
                            cout.width (6);
                            cout << slist[i].stocknumber ;
                            cout.width (25);
                            cout << slist[i].description ;
                            cout.width (25);
                            cout << slist[i].supplier ;
                            cout.width (4);
                            cout << slist[i].quantity ;
                            cout.width (10);
                            cout << slist[i].price ;
               
                    cout <<("\n\n\t\t Enter the stock number of the new item   ");
    	            } stockcheck:
        cin >>tempstr;
        
        if ((atoi(tempstr)) == 0)
        {
                          cout << "\n not valid";
                          cout <<("\n\t\t Enter the stock number of the new item   ");
                          goto stockcheck;
                          }
                          
        
        
       for(i=0; i < numitems; i++)
            
            {
                if((atoi(tempstr)) == (slist[i].stocknumber))
                { 
                cout <<"\n\nnumber alredy exists";
                 cout <<("\n\t\t Enter the stock number of the new item   ");
                goto stockcheck;
                }
            }
     
       slist[i].stocknumber = atoi(tempstr);
    	            
     			            		   }

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    It's very difficult to follow what you are trying to do

    by the time you get to here
    Code:
    slist[i].stocknumber = atoi(tempstr);
    i will always equal numitems. You always run to the end of the for loop.

    If you are inserting a new item shouldn't you increase numitems?

    You never try to update an item, You search for it , and print it, but never update it.

    You should really try to remove your goto's

    It looks like you have too much going on for a function called "update"
    Last edited by spydoor; 05-19-2006 at 09:44 AM.

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    I agree, the updatechoice variable should just be a single character or integer. A string seems like an akward way of doing it ... to my mind anyways.

    Your indentation is better than before, but it would be nicer if it was neater, and by nicer I mean better because more people will be likely to read it, and you'll get more solutions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM
  5. From stream/file to a string array problem
    By dradsws in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 06:24 PM