GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #11  
Old 23-Nov-2005, 16:43
i hate c i hate c is offline
New Member
 
Join Date: Nov 2005
Posts: 16
i hate c is on a distinguished road

Re: runtime error R6002


ok the next step in my assignment is to convert the program to use a linked list, ive been looking at linked lists all day and all i know is that i dont know squat! (and that my head hurts)

ive changed the struct to:
CPP / C++ / C Code:
struct node
	{
	int id;
	double x, y, stress;
	struct node_value *next;
	};

so now im trying to convert this lump of program to use linked lists instead
CPP / C++ / C Code:
	fgets(line, sizeof(line), input_stream);
			while (((line_ptr = fgets(line, sizeof(line), input_stream)) != NULL) &&
			((no_values = sscanf (line, "%d %lf %lf %lf",
							&node_array[no_nodes].id,
							&node_array[no_nodes].x,
							&node_array[no_nodes].y,
							&node_array[no_nodes].stress)) == 4)) no_nodes++;

do you know of any good examples of this or can you set me off in the right direction?

thanks
mick
  #12  
Old 23-Nov-2005, 20:33
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: runtime error R6002


Quote:
Originally Posted by WaltP
And I repeat: READ THIS and THIS
Walt. I tried to be simple.

The OP is already confused with other things.

Mick,
This:
CPP / C++ / C Code:
struct node
    {
    int id;
    double x, y, stress;
    struct node_value *next;
    };
should be replaced with this:
CPP / C++ / C Code:
struct node
    {
    int id;
    double x, y, stress;
    struct node *next;    /*because the structure name is node.*/
    };

Since understanding linked lists is important , read this tutorial about linked lists and then, you will be able to write the code. If you have any doubts, feel free to ask.
Linked List Basics

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
Last edited by Paramesh : 23-Nov-2005 at 21:27.
  #13  
Old 24-Nov-2005, 00:59
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: runtime error R6002


Quote:
Originally Posted by Paramesh
Quote:
Originally Posted by WaltP
And I repeat: READ THIS and THIS
Walt. I tried to be simple.

The OP is already confused with other things.

Sure, I understand that. But to give help using a dangerous function is not a good idea. What happens when a confused poster takes your advice, creates a 2 char buffer for a name (first and last) and enters "Edmund Jackson" and wonders why the program explodes? fgets() is safe, and it seems the poster didn't even know sscanf() in the first place.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #14  
Old 24-Nov-2005, 06:16
i hate c i hate c is offline
New Member
 
Join Date: Nov 2005
Posts: 16
i hate c is on a distinguished road

Re: runtime error R6002


hey, really good link but im running into the same wall that i was banging my head against yesterday
the example it gives is
CPP / C++ / C Code:
struct node
	{
	int id;
	double x, y, stress;
	struct node *next;
	};
struct node *node_ptr
node_ptr = malloc(sizeof(struct node))
node_ptr -> data = data_to_be_stored

but if im reading this right, i want to replace the last line:
node_ptr -> data = data_to_be_stored

with a way to read the line from my file rather than input the values one by one.

am i getting warm, or just talking s*%t?
  #15  
Old 24-Nov-2005, 06:53
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: runtime error R6002


Quote:
Originally Posted by i hate c
but if im reading this right, i want to replace the last line:
node_ptr -> data = data_to_be_stored

with a way to read the line from my file rather than input the values one by one.

am i getting warm, or just talking s*%t?
You have to vary the sscanf arguments.

For example, like this:
CPP / C++ / C Code:
/* code... */
sscanf (line, "%d %lf %lf %lf",
                            &node_ptr->id,
                            &node_ptr->x,
                            &node_ptr->y,
                            &node_ptr->stress);
/* code... */

Here is a simple explanation of linked list:
Building a Linked List in C


Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #16  
Old 24-Nov-2005, 11:35
i hate c i hate c is offline
New Member
 
Join Date: Nov 2005
Posts: 16
i hate c is on a distinguished road

Re: runtime error R6002


thanks paramesh,
ive built the program as best i can but i think i might have messed up the loops a bit

CPP / C++ / C Code:
#include <stdio.h>
int main(void)
{
char line[101], filename[101];
char *line_ptr;
double dummy = 9;

struct node
	{
	int id;
	double x, y, stress;
	struct node *next;
	};

struct node *node_ptr, *first_ptr = NULL, *new_structure_ptr, *current_ptr = NULL;			
int no_nodes = 0, no_values;
FILE *input_stream, *output_stream;

do
	{
	fprintf(stdout, "Enter the file name you wish to read from: ");
	fscanf(stdin, "%s", filename);
	fprintf(stdout, "\nReading %s", filename);
	    if ((input_stream = fopen(filename, "r")) != NULL)
				{
				new_structure_ptr = (struct node*)malloc(sizeof(struct node));
				if (first_ptr != NULL)
					current_ptr ->next = new_structure_ptr;
				else
					first_ptr = new_structure_ptr;
					current_ptr = new_structure_ptr;
	
					fgets(line, sizeof(line), input_stream);
					while (((line_ptr = fgets(line, sizeof(line), input_stream)) != NULL) &&
					((no_values = sscanf (line, "%d %lf %lf %lf",
								&node_ptr ->id,
								&node_ptr ->x,
								&node_ptr ->y,
								&node_ptr ->stress)) == 4)) no_nodes++;
					
					
							
					fclose(input_stream);

				if ((line_ptr != NULL) && (no_values != 4))
				
					fprintf(stdout, "Error reading line %s\n", line);
				else
					if (line_ptr == NULL)
					fprintf(stdout, "\nEnd of file found\n\n");
					fprintf(stdout, "%s\n\n", filename);
					fprintf(stdout, "node x y stress\n\n");	        //colomn headers//	
				current_ptr ->next = NULL;
				}
		else
			fprintf(stdout, "\nFile cannot be found\n");
	
	}

while (current_ptr != NULL)
		{
		fprintf(stdout, "%d %.1lf %.1lf %.1lf\n",
								&node_ptr ->id,
								&node_ptr ->x,
								&node_ptr ->y,
								&node_ptr ->stress );
		}

		
	fprintf(stdout, "\nEnter the file name you wish to write to: ");
	fscanf(stdin, "%s", filename);

	((output_stream = fopen(filename, "w")) != NULL);
	fprintf(output_stream, "node x y stress\n");

	current_ptr = first_ptr;	

while (current_ptr != NULL)
			{
			fprintf(output_stream, "%d %.1lf %.1lf %.1lf\n",
								&node_ptr ->id,
								&node_ptr ->x,
								&node_ptr ->y,
								&node_ptr ->stress );
			}

fclose(input_stream);
fprintf(stdout, "\nData has been written to %s\n\n", filename);
return(0);
}


Im currently getting a syntax error missing ;before { at this point:
CPP / C++ / C Code:
{
		fprintf(stdout, "%d %.1lf %.1lf %.1lf\n",
								&node_ptr ->id,
								&node_ptr ->x,
								&node_ptr ->y,
								&node_ptr ->stress );

and also
'malloc' undefined; assuming extern returning int
  #17  
Old 24-Nov-2005, 11:58
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: runtime error R6002


I already told you that you should use printf instead of fprintf(stdout.
and you should include your stdlib.h to use malloc.

Your code is totally confusing.
Just try a simple code first, and then once you satisfy with your simple one, switch to your main code.

Here are some few changes you should make:
In the printf statement, you should not put the & symbol. (I wonder you know printf statement)
CPP / C++ / C Code:
printf( "%d %.1lf %.1lf %.1lf\n",
        node_ptr ->id,
        node_ptr ->x,
        node_ptr ->y,
        node_ptr ->stress 
       );

The syntax error came because:
You use do while loop.
You should end the while statement with a semicolon.
There is no need for printing the output there.
Just remove those lines.
So, this:
CPP / C++ / C Code:
}

while (current_ptr != NULL)
        {
        fprintf(stdout, "%d %.1lf %.1lf %.1lf\n",
                                &node_ptr ->id,
                                &node_ptr ->x,
                                &node_ptr ->y,
                                &node_ptr ->stress );
        }

        
    fprintf(stdout, "\nEnter the file name you wish to write to: ");
    fscanf(stdin, "%s", filename);

should be changed to :
CPP / C++ / C Code:
}while (current_ptr != NULL);

        fprintf(stdout, "\nEnter the file name you wish to write to: ");
    fscanf(stdin, "%s", filename);

Now you must've understood what a linked list is!
So, as already said, visit this simple linked list example:
Linked List using C
and proceed.

Take a look at this program and you may find some hints:
CPP / C++ / C Code:
#include<stdio.h>
#include<stdlib.h>

typedef struct node
    {
    int id;
    double x, y, stress;
    struct node *next;
    }nodes;

int main()
{
    int i;
    nodes *node_ptr, *head;
    head = NULL;    

   for(i=1;i<=10;i++) 
   {
      node_ptr = (nodes*)malloc(sizeof(nodes));
      node_ptr->id = i;
      node_ptr->x = i+10.2;
      node_ptr->y = i+1.5;
      node_ptr->stress = i+12.5;
      node_ptr->next  = head;
      head = node_ptr;
   }  
   
    node_ptr = head;
    char s[] = "20 20.5 20.6 22.3";

   while(node_ptr) {
      printf("%d\n", node_ptr->id);
      node_ptr = node_ptr->next ;
   }

    return 0;
}




Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #18  
Old 19-Jun-2009, 14:35
Iamcrysis Iamcrysis is offline
New Member
 
Join Date: Jun 2009
Posts: 1
Iamcrysis is on a distinguished road

Re: runtime error R6002


Please any1 help me.Where can I put the codes you tell.I really don't know very well to work with this stuff.I have the same problem as some of you guys do.Please help.
  #19  
Old 20-Jun-2009, 08:36
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: runtime error R6002


Quote:
Originally Posted by Iamcrysis
....Please help...
1. When you first get to gidforums.com, click the "C Programming Language" link or the "C++ Forum link"

2. At the top of the list of threads, there is a link highlighted in red. The link is called "Read This >>>Guidelines for posting requests for help" Click on that one. Maybe there are enough specifics to help. If so, then quit reading this and create your own new thread.

If you are still not sure...

3. Now, back at the "C Programming Language" site, just above the list of threads there is a button called "New Thread" Click on that one and an edit window opens.

4. Put your questions or other narrative in the edit window. Just type the stuff into the window..

5. If you have code that is part of your post, then paste it from your text editor into the edit window.

6. Put [c] before the first line of the code. (The code, not the narrative.)

7. Put [/c] after the last line of the code.

8. Click on the "Preview Post" button below the edit window to see what it will like.

9. If it doesn't look the way you want it to look, then make changes in the edit window and click the "Preview Post" button again.

10. Repeat step 9 as many times as you need in order to make it look OK. Then, and only then, click the "Submit Reply" button.

11. Now go back and read the "Guidelines for posting requests for help" stuff again as you are waiting to see if anyone responds.

Sometimes you get immediate help, sometimes it takes a while.
It's an open forum and anyone can post. Maybe you will see a helpful response and maybe not.

If your questions are specific and they are appropriate for a programming help forum, you will probably see some answer(s).


Regards,

Dave
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
runtime, compile time... kai85 C Programming Language 2 19-May-2005 23:05
Cannot find a runtime error on a fltk app running on windows Frangar FLTK Forum 2 12-May-2005 12:38
how to increase the font size at runtime banur22 MS Visual C++ / MFC Forum 1 21-Apr-2005 07:37
Free copy of Runtime Revolution crystalattice Open Discussion Forum 0 29-Dec-2004 16:52
Problem with php/mysql script during runtime norok MySQL / PHP Forum 3 25-Jun-2003 06:35

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:41.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.