Thread: VOID function help

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

    VOID function help

    I'm still learning the ropes of this language so can someone tell me why this does not work as a void function?

    Code:
    void updateroutine ( int numitems)
    {
            int i,j;
    	char tempstr[80];
    
            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 ;
    }

  2. #2
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    Well, what doesn't work? It doesn't compile? Doesn't do what you want at run-time? Throws bananas at you?

    After a quickish look at the code, I'd say you're missing a curly brace at the end of your if statement.
    There is a difference between tedious and difficult.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    12
    Lol, sorry for being so vague earlier. Well here's a larger section of my code, I've deleted some bits that aren't important like long lines of "cout << ...".

    Right now, the program does not compile and I am unsure of why. Suggestions?

    Code:
    void updateroutine ( int numitems)
    {
            int i,j;
    	char tempstr[80];
    
            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 ;
    }}
    
    
    //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");
    	cout << "\n                ======SEARCH MENU======\n";        
    	cout <<                      ("\nUPDATE AN ITEM IN STOCK");
            listitems (numitems);
            cout << "                  1.  Update stock number\n";
            cin >> updatechoice;
    		     
    	if (!strcmp(updatechoice, "1"))
    			                   {
    	updateroutine(numitems)
               
            cout <<("\n\t\t Enter the stock number of the new item   ");
    	cin >> tempstr;
    	slist[i].stocknumber = atoi(tempstr);
    	                     };//End if
    		    			            }
    Last edited by Jack-Bauer; 05-15-2006 at 09:08 AM. Reason: code error

  4. #4
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    Post your error messages. It makes it waaaaay easier to figure it out. Plus I don't have to read all of your code when you have the information I need at your fingertips.

    On a slighty off-topic note, you should look into strings instead of char arrays if you're going to program in C++. Also, someone is bound to chastise you for using system calls, so it might as well be me. Don't use them. For little learning programs, its not a big deal, but don't get in that habit as it creates a sizeable security issue. There's an entry in the FAQ somewhere about clearing the screen.
    There is a difference between tedious and difficult.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You might also want to place your closing braces properly, and cut out the unnecessary use of parentheses with your strings.

    By the way, in updateroutine(), you may find it better to just do a:
    Code:
    cin >> j;
    tempstr seems rather useless. Oh, and j is not a very descriptive name which makes people think that it is to be used as a loop counter or array index.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    12
    *EDIT*

    I knew my coding was messy, I was planning on tidying it up once I got it working, mainly because I didn't know if it was going to work. I thought I'd clean it up anyway before it was working and once I got my curly brackets into a sensible place, I realised I had too many.

    That was the problem, guess I'll keep in mind not to be lazy lol. Thanks again for your suggestions and help
    Last edited by Jack-Bauer; 05-15-2006 at 11:58 AM.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    903
    [off-topic]Jack-Bauer, do you play Enemy Territory ?[/off-topic]

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    12
    Can't say I do but it sounds good

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    903
    Oh, sorry for the disturbance, then. I know a player named Jack Bauer as well.

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    12
    no worries, I got the name off the tv series 24

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM