-
Arrays & functions
This Lab has to parts.I am done with the first part.The second parts which is the arrays and function ,I can't get the to work.Please some one help.
Second part. Include the product description, the name of each item (Wilson Golf balls, Soloman Skis, HO Water Ski, etc.) Create a getstring() function to get the description and uppercase every letter of the entire description. You must create your own function to do this. You cannot use the strupr() function if it is available on your compiler. If someone enters: penn tennis balls, it is altered to: PENN TENNIS BALLS. (As a bonus, uppercase the first letter only of each word in the description, instead). Also modify the show() function to display the uppercased product descriptions with the other data.
Sierra Sporting Goods
Enter the product number: __
Enter the product type : __
Enter the description : ____________________
Enter the quantity : __
Enter the unit cost : __
Enter the unit price : __
The product number is --> ????
The product type is ----> ?
The description is -----> ??????????????????
The quantity is --------> ???
The cost is ------------> ???.??
The price is -----------> ???.??
-
So, what's your question?
-
I can't get the function for arrays to work.
-
The call should look like this:
getstring ( desc );
The functions should look like this:
Code:
/* Very bad code, but that's life. ;) */
void getstring(char desc[])
{
puts("\tEnter the description : ");
fflush(stdin);
gets(desc);
upcase(desc);
}
void upcase(char *s1)
{
while(*s1)
*s1++ = toupper(*s1);
}
Since you are obviously using a compiler that fflush works for input streams, it will suffice for your homework. But keep in mind that the C standard defines fflush for output streams, so fflush ( stdin ) is undefined whether it works or not. And you should consider using fgets instead of gets, it will check your array boundaries instead of overwriting memory you don't own. I won't bother detailing the flaws in your code since your instructor should be doing that, if she/he doesn't then come back and I'll be glad to give you a detailed description as well as ways to improve. :)
-Prelude
-
@Shila:
Well, there are arrays everywhere. ;) Please, tell us the name of the function!
Code:
switch (key)
{
case '1': case 'A': add(); break;
case '2': case 'R':
case '3': case 'D':
case '4': case 'C': ("\nNot Implemented"); break;
case '5': case 'Q': more = FALSE; break;
default : ("\nError in selection\a");
}
Is the code above a valid switch/case construct?
And btw never forget to end each 'case' statement with break;.
And in line 96 delete the second semicolon at the end of the line. ;)
-
The switch code. Those are from my instructor..By the way my instructor sucks I am a beginner in C ,I had to take this class because it was requirement.the getstring function and upcase function are my problems.
Thanks
-
Line 84 should look like this:
This is my suggestion for your nemesis functions: ;)
Code:
void getstring(char desc[])
{
puts("\tEnter the description : ");
gets(desc);
upcase(desc);
puts(desc);
}
void upcase(char s1[])
{
while(s1 && (*s1 != '\0'))
{
*s1 = toupper(*s1);
s1++;
}
}
I didn't test the code ... it may not work.
@Prelude:
Oh, I am very sorry! You already did the code for the two functions above!
-
Hello;
I did'nt work ,But thank you very much for trying.
-
I really appreciate everyone's, help you all have been great ,I got my messy codes to work.
Thanks very much