473,473 Members | 1,563 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Debug program errors

momotaro
357 Contributor
am supposed to have the same data that i have entred but its not the case!!!
can you help me????
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. typedef struct
  4. {
  5.     char name[20];
  6.     double salary;
  7.     int rank;
  8. }employee;
  9.  
  10. void fill_up(employee arr[]);
  11. void display(employee arr[]);
  12.  
  13. void main()
  14. {
  15.     employee arr[2];
  16.     fill_up(arr);
  17.     display(arr);
  18.     while(1);
  19. }
  20.  
  21. void fill_up(employee arr[])
  22. {
  23.     int i;
  24.     for(i = 0; i < 2; i++)
  25.     {
  26.         printf("name --> ");
  27.         scanf("%s", &arr[i].name);
  28.         printf("salary --> ");
  29.         scanf("%lf", &arr[i].salary);
  30.         printf("rank --> ");
  31.         scanf("%d", &arr[i].rank);
  32.     }
  33. }
  34.  
  35. void display(employee arr[])
  36. {
  37.     int i;
  38.     for(i = 0; i < 2; i++)
  39.     {
  40.         printf("\n\n\n\nname --> %s", &arr[i].name);
  41.         printf("\nsalary --> %.2f", &arr[i].salary);
  42.         printf("\nrank --> %d", &arr[i].rank);
  43.     }
  44. }
Feb 11 '07 #1
3 1573
AdrianH
1,251 Recognized Expert Top Contributor
Hi,

The problem is your scanf("%s", &arr[i].name); If you are going to get the address of the first char of name, you have to specify the array index. Otherwise you have a char** instead of a char*, which I think would point at a temporary location on the stack in the frame of the function. You have the same problem with your printf's.

Basically, &arr[i].name is the equivalent of saying "Given the char pointer arr[i].name, give me back the address of that pointer." because arr[i].name is a pointer to the first element in the array.

So either say arr[i].name or &arr[i].name[0].


Adrian

P.S. If you use gcc or g++, use the -Wall option as it will give you a whack of warnings that will inform you when you are doing something fishy. If you are using another compiler, tell it to turn warnings on. Warnings are your friend, not your nemesis.
Feb 11 '07 #2
kvbreddy
23 New Member
One more issue in the above code is -
If the input of the name field is more than a single word, the scanf("%s", arr[i].name); function takes the first word as the name and left the remaining input to the following fields.

So use the following to avoid the above problem

scanf("%[^\n]", arr[i].name);

It reads a string until it encounters neline character
Feb 11 '07 #3
kvbreddy
23 New Member
One more bug in ur code is ..

Don't use & for printing fileds..Replacements for ur error statements

printf("\nsalary --> %.2f", arr[i].salary);
printf("\nrank --> %d", arr[i].rank);

Now ur code works perfecltly.

Vijay
Feb 11 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: lkrubner | last post by:
When I go to groups.google.com and run a search against the comp.text.xml newsgroup, using the words "debug XML" as my search term, the results that come up are either irrelevant are surprising...
7
by: Techno Learner | last post by:
Sorry for the lame question but, what's the difference between Debug and Release versions?
5
by: Dave | last post by:
I am trying to compile my C++ programs in debug mode in Visual NET 2003 and keep getting the following message fatal error LNK1104: cannot open file 'mfc70d.lib I cannot find this program anywhere...
2
by: Eagle | last post by:
I created a project that runs fine on my local machine, using http://localhost etc. I copied the program up to another web server and am getting errors -- sometimes. How do I run my program in...
5
by: SiD` | last post by:
when starting a windows service writte in vb.net, a messagebox appears: cannot start service from the command line or a debugger. A windows service must first be installed using installutil.exe ...
2
by: jocobunshin | last post by:
Hi there! I'm new in Turbo C... uhmm our teacher gave us a project to make a program in Turbo C. Im from the Philippines, and our project is due on tuesday, februrary 13, 2007... can anyone check my...
4
by: mickey22 | last post by:
Hi all, I am getting linking errors when I run my program in debug mode and the same thing I run in release mode it doesnt show errors..Am I linking wrong libraries in the debug mode?Can they be...
19
by: desktop | last post by:
When I write code I use a lot of: std::cout << "TEST1\n"; .... .... <some code> .... ....
7
by: Khookie | last post by:
Hi everyone The program I'm writing is getting bigger, hence not as easy to debug. So I have this on one of my core header files, which I include in just about every .c file, and I use it...
2
by: =?Utf-8?B?V2lsbGlhbSBNY0lscm95?= | last post by:
Here's the problem. I've been developing a C++ application since 2003 using the default settings. Comes time to find all the memory leaks. I downloaded a nifty package that won't work unless I...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.