ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have some code that has errors, and I do not know how to use a debugger to fix it myself. I have looked and looked and changed and changed heaps and heaps trying to fix it, and no success. I was hoping someone could help, I will comment it again.
This is my header file
Code:
/*My Course Project.h*/
/*
*Structure of 100 CD's
*/
int count;
char input;
struct cd_type{
char title;
char artist;
char album_single ;
int tracks;
float price;
};
struct cd_type cd_t[100];/*Declare an array of 100 structures of cd_t*/
/*
*Definition of get_user_input() function
*/
void get_user_input()
{
*/
*Start a for loop to enter data into each element of the
*array.
*/
for (count = 0; count < 5; count++)
{
printf("Please enter enter the title of the album: ");
scanf("%[\n]", cd_t[count].title);
fflush(stdin);
printf("Please enter enter the artist of the album: ");
scanf("%[\n]", cd_t[count].artist);
fflush(stdin);
printf("Please enter enter if the album is a single or a double 's' for single and\n 'd' for double: ");
scanf("%c", &cd_t[count].album_single);
fflush(stdin);
printf("Please enter enter the number of tracks the album has: ");
scanf("%d", &cd_t[count].tracks);
fflush(stdin);
printf("Please enter the retail price of the album: ");
scanf("%f", &cd_t[count].price);
fflush(stdin);
printf("Do you have any more CD's to enter, 'y' for yes or 'n' for no: ");
scanf("%c", &input);
if(input == 'n' || 'N')
break;
if(input == 'y' || 'Y')
continue;
else
break;
}
for (count = 0; count < 5; count++)
{
printf("%s", cd_t[count].title);
printf("\n%s", cd_t[count].artist);
}
}
This is my source file
Code:
/*My Course Project*/
#include <stdio.h>
#include "My Course Project.h"
void get_user_input();
int main()
{
get_user_input();
fflush(stdin);
getchar();
}
Well, these can only hold a single character each:
Code:
char title;
char artist;
char album_single ;
Surely the names of artists and albums will be longer than that? And drop the fflush(stdin), it doesn't empty the input stream. If you have stuff in the input stream it must be read (and possibly ignored).
Originally posted by Hivemind Well, these can only hold a single character each:
Code:
char title;
char artist;
char album_single ;
Surely the names of artists and albums will be longer than that? And drop the fflush(stdin), it doesn't empty the input stream. If you have stuff in the input stream it must be read (and possibly ignored).
Oh yea I fogot about that, thanks . I will give it a try.
::EDIT::
It still has a run time error even after I changed it. This time the run time error shows nothing, before it showed NULL for everything.
Some more help would be appreciated further,
Thanks Again.
Last edited by InvisibleSniper; 10-17-2005 at 05:48 AM.
Usually include files don't contain whole functions, but only the prototype. The problem that will occur is that if you include it in multiple files, there will also be multiple instances of the function.
So put your definitions and prototypes in the include file and the get_user_input function in a seperate c-file.
Compile with something like gcc -Wall main.c getuserinput.c -o cddb
So in other words I should put my function definition inside of the source file with main in it... or at least another C file. And the function declarations should go inside the header files?
I still have the run-time error anyway, I don't know why it's doing what it is because this is the first time I have used structures with arrays.
I still have the run-time error anyway, I don't know why it's doing what it is because this is the first time I have used structures with arrays.
So does it work if the programs operates on one only one instance of the struct? You need to condense your code, keep removing stuff (while still keeping it compilable, of course) till the error goes away. This will be a good learning exercise for you and you have a good chance of finding the error yourself. You can't expect us to do this condensing for you. Also, learn how to use gdb. If you're using emacs as your editor it has a gdb mode that's probably easier than using gdb itself, there are also pure graphical frontends to the debugger. gdb is powerful, but start with the basics as stepping through your code one statement at a time, printing the values of the variables involved.
here is an on-purpose stack dump.
To show you how to find the failing line of code....
call it dump.c
Code:
#include <stdio.h>
int main (int argc, char ** argv, char ** envp)
{
int n;
char *p;
printf("Goodbye");
puts(p);
return 0;
}
now do:
Code:
export CFLAGS=' -g'
make dump
gcc -g dump.c -o dump
now run the program:
Code:
dump
Segmentation Fault(coredump)
which should give you a file called 'core'
right, now call gdb dump core
and type where
and lo and behold! the line where it failed:
Code:
#0 0xff2c2e58 in memccpy () from /usr/lib/libc.so.1
#1 0xff314074 in puts () from /usr/lib/libc.so.1
#2 0x000106b4 in main (argc=1, argv=0xffbef47c, envp=0xffbef484) at dump.c:10
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.