-
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 ;
}
-
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.
-
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? :confused:
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
}
-
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.
-
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:
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.
-
*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 :)
-
[off-topic]Jack-Bauer, do you play Enemy Territory ?[/off-topic]
-
Can't say I do but it sounds good
-
Oh, sorry for the disturbance, then. I know a player named Jack Bauer as well.
-
no worries, I got the name off the tv series 24