LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-17-2005, 05:14 AM   #1
InvisibleSniper
Member
 
Registered: Jul 2005
Location: Australia
Distribution: Debian
Posts: 113

Rep: Reputation: 15
C Help Needed Please.


Hi again,

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();
}
Thanks In Advance.
 
Old 10-17-2005, 05:16 AM   #2
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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).
 
Old 10-17-2005, 05:43 AM   #3
InvisibleSniper
Member
 
Registered: Jul 2005
Location: Australia
Distribution: Debian
Posts: 113

Original Poster
Rep: Reputation: 15
Quote:
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.
 
Old 10-17-2005, 05:47 AM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Just a comment:

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

My version of the include file:
Code:
/*My Course Project.h*/

struct cd_type{

char title;
char artist;
char album_single ;
int tracks;
float price;

};

void get_user_input();
 
Old 10-17-2005, 05:53 AM   #5
InvisibleSniper
Member
 
Registered: Jul 2005
Location: Australia
Distribution: Debian
Posts: 113

Original Poster
Rep: Reputation: 15
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.
 
Old 10-17-2005, 08:10 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Keep it simple.

Put it all in one .c file to start with before you start
with multiple source.

string buffers should be something like:

#define BUFSZ 1023
char artist[BUFSZ + 1]
char title[BUFSZ + 1]
 
Old 10-17-2005, 08:19 AM   #7
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Quote:
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.
 
Old 10-17-2005, 08:57 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
doh
 
Old 10-17-2005, 09:05 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
help needed sailu_mvn Programming 2 03-24-2005 06:50 AM
help needed with QT sabeel_ansari Programming 1 01-21-2004 02:14 PM
C++ help needed sabeel Programming 6 11-27-2003 12:19 PM
help needed here... c12ayon Programming 2 10-29-2003 10:59 AM
Needed Help rajesh.s Linux - Hardware 0 12-17-2002 02:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration