|
-
May 14th, 2013, 05:15 PM
#1
Need Help to Solve Errors Please!
I'm a beginner at c programming and i'm trying to write a program to accept the user's name, address and so on. This program should also display a list of items for them to select from, and also to calculate the cost of multiple items selected. I don't know much about c programming, and so far all i have is this:
Code:
#include <stdio.h>
#include <stdlib.h>
float calcCost();
void main()
{
char FName[100];
char LName[100];
char address[100];
char catType[100];
char rings;
char watches;
char chains;
char date[50];
printf("Please enter your first name:");
scanf("%s", &FName);
printf("\n");
printf("Please enter your last name:");
scanf("%s",&LName);
printf("\n");
printf("Please enter your address:");
scanf("%s",&address);
printf("\n");
printf("Please enter date:");
scanf("%s",&date);
printf("\n");
printf("Item Listing\n");
printf("\n");
printf("Categories\n");
printf("Men's Watches\n");
printf("Men's Chains\n");
printf("Men's Rings\n");
printf("To expand each category, enter a category code.(Either watches, chains or rings):\n");
scanf ("%s",&catType);
printf("\n");
if(catType =="watches")
{
printf("Gold and black watch with rubber bands - Item ID: MGW1\n");
printf("Gold watch with black watch face - Item ID: MGW2\n");
printf("Gold watch with black leather bands - Item ID: MGW3\n");
printf("Gold chronograph solar aviator watch - Item ID: MGW4\n");
printf("Gold sports chronograph with gold and silver band - Item ID MGW5\n");
}
else if(catType == "chains")
{
printf("10k Gold, square link chain, Cost:$1500 - Item ID: MGC1\n");
printf("14k Gold large link, 3 small link chain, Cost:$2500 - Item ID:MGC2\n");
printf("Gold chain with buddah's head pendant, Cost:$2800 - Item ID:MGC3\n");
printf("Gold chain with sideway cross, Cost:$2900 - Item ID:MGC4\n");
printf("Silver chain with blue cat's eye scorpion pendant, Cost:$3000 - Item ID:MSC1");
printf("Silver dogtag with tribal symbol, Cost:$1500 - Item ID:MSC2");
printf("Silver carving chain, Cost:$2000 - Item ID:MSC3");
printf("Silver chain with thick, square links, Cost:$2300 - Item ID:MSC4");
printf("Silver chain with black onyx and diamond cross pendant, Cost:$3500 - Item ID:MSC5");
}
else if(catType == "rings")
{
printf("Gold ring with gold encrusted black onyx, Cost:$3500- Item ID:MGR1");
printf("Gold ring with with phoenix emblem, Cost:$3800- Item ID: MGR2");
printf("10k gold nugget ring with onyx and tiger's eye, Cost:$4300- Item ID: MGR3");
printf("Gold eagle embeded onyx ring with diamonds, Cost:$5200- Item ID: MRG4");
printf("Gold ring with black onyx, Cost:$4000- Item ID: MGR5");
printf("Silver diamond cross ring with black pearl, Cost:$5500- Item ID:MSR1");
printf("Silver watch ring line with gold, Cost:$380- Item ID:MSR2");
printf("Silver ring with rectangular onyx stone, Cost:$2300- Item ID:MSR3");
printf("Four-layer square sterling silver ring, Cost:$5200- Item ID:MSR4");
printf("Celtic weave sterling silver ring, Cost:$2000- Item ID:MSR5");
}
system("pause");
return 0;
}
float calcCost(){
}
(*whenever I run this program, everything works fine except for the fact that the list won't show. How do I correct this please?*)
-
May 15th, 2013, 01:01 AM
#2
Re: Need Help to Solve Errors Please!
First of all, your program does not compile.
Second thing, catType is a pointer, and your intention is evidently to compare not pointers but strings. So you have to use string comparison functions from string.h, like strcmp, stricmp, strncmp, etc.
Code:
if(0 == stricmp(catType, "watches"))
{
Besides, did it occur to you that you output watches category as Men's Watches, and expect watches from user input? Do you think your users smart enough to figure that out themselves?
Best regards,
Igor
-
May 15th, 2013, 04:42 AM
#3
Re: Need Help to Solve Errors Please!
(*whenever I run this program, everything works fine
You need to be more careful when you use scanf() and arrays:
Code:
char FName[100];
//...
printf("Please enter your first name:");
scanf("%s", &FName);
It should be this:
Code:
scanf("%s", FName);
or
Code:
scanf("%s", &FName[0]);
Regards,
Paul McKenzie
-
May 15th, 2013, 05:39 AM
#4
Re: Need Help to Solve Errors Please!
When a function is defined to have a return value, then one needs to be returned of the correct type. You also have main defined as void return. Normally function main would return an int (as indeed your program does).
Code:
char FName[100];
...
printf("Please enter your first name:");
scanf("%s", FName);
What do you think would happen if the user enters a name that is more than 99 characters in length?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|