Google Answers Logo
View Question
 
Q: another simple c question ( No Answer,   7 Comments )
Question  
Subject: another simple c question
Category: Computers > Programming
Asked by: fikal-ga
List Price: $5.00
Posted: 21 Nov 2004 10:10 PST
Expires: 27 Nov 2004 11:08 PST
Question ID: 431914
ok my example program is almost complete except when i add another
object to the array and press '2' (print)  it prints out half the
array (the string part) along with some funky letters.  I thought
since you have helped me a lot I would post this as a new question.

Thank you 
ryan


#include<stdio.h> /* input/output header */
#include<string.h> /* some basic string command */

int restore(char a[][19], int b[], int *number);
int print(char a[][19], int b[], int *number);
int add(char a[][19], int b[], char product[],int howmuch, int *number);
int main(void)
{
	
	/*int numbers, functions, characters, etc */
	char item[100][19], product[19];
	int quantity[100];
	int i;
    int number=0,howmuch=0;    
	
	printf("1. To read chars.\n");
	printf("2. To print chars.\n");
	printf("3. To add chars.\n");
	while (1) {
	scanf("%d",&i);
	
        

	   if (i == 1) {
		   restore(item,quantity,&number);
		   i=0; }
	   if (i == 2) {
		    print(item,quantity,&number);
			i=0; }
	   if (i == 3) {
			scanf("%s%d",product,&howmuch);
		    add(item,quantity, product,howmuch,&number);
			i=0; }
	   }
    
	return 0;
}

int print(char a[][19], int b[], int *number) {
	int j;
	for (j = 0;  j < *number;  j++) 
		printf( "%s  %d ", a[j], b[j]);
	return *number;
}

int restore(char a[][19], int b[], int *number) {
    int j;
	FILE * infile;
	infile = fopen( "ryan.txt","r" );
	fscanf( infile, "%d", number); 
	for (j = 0;  j < *number;  j++) 
		fscanf( infile, "%s %d ", a[j], &b[j]);
	printf("Array Created! YAY!\n");
	return *number;
}

int add(char a[][19], int b[], char product[],int howmuch, int *number) {
	if (*number == 0)
		printf("Error: you must restore an order first\n");
	(*number)++;
	strcpy(a[*number], product);
	b[*number] = howmuch;
	return *number;
}

Clarification of Question by fikal-ga on 22 Nov 2004 06:05 PST
im still lost, sorry.

for my program i run it like this.  1. creates the array.  2. print
(works fine) then i hit 3. followed by <name> <numbers> then i reprint
and my newly created item is weird looking.

Something is not right with my add function.  I changed the print to
an actual number and i still get something printing out like this for
the added array.  |||||||||||<item_name>  -834345335

-Ryan
Answer  
There is no answer at this time.

Comments  
Subject: Re: another simple c question
From: 12345a-ga on 21 Nov 2004 20:10 PST
 
Well not sure but I think you have a few problems with that c.

Possibly a out by one error, in the print function.

If number is the last item , then how can we print it
with  j < number

int print(char a[][19], int b[], int *number) {
	int j;
	for (j = 0;  j < *number;  j++) 
		printf( "%s  %d ", a[j], b[j]);
	return *number;

Also if you don't do a restore first but add items ,
number=1 then if you print it out, you would get what ever
junk was in the array for  a[0] and b[0].
Also your added item a[1] b[1] would exist but not print out because
of  j < number .
Subject: Re: another simple c question
From: fikal-ga on 23 Nov 2004 06:20 PST
 
i agree with that, but how would i fix that i < *number statement ?
Subject: Re: another simple c question
From: evilsaltine-ga on 23 Nov 2004 10:17 PST
 
I think you need to add a space between the %s and %d here:

scanf("%s%d",product,&howmuch); becomes
scanf("%s %d",product,&howmuch);

I tried it without the space and it produced strange results, so that
may be the problem.
Subject: Re: another simple c question
From: evilsaltine-ga on 23 Nov 2004 10:18 PST
 
Hmm.. nevermind, forget I said that. I was doing something else wrong.
Subject: Re: another simple c question
From: evilsaltine-ga on 23 Nov 2004 10:26 PST
 
In your restore function, the last entry created is a[*number - 1],
but in your add function, you add one to *number first then assign to
a[*number], so it skips a row. The *number++; needs to be just above
the return statement.

strcpy(a[*number], product);
b[*number] = howmuch;
(*number)++;
return *number;
Subject: Re: another simple c question
From: fikal-ga on 23 Nov 2004 13:32 PST
 
that is correct!  amazing how putting it in the wrong place has caused
me so much frustration.  This example will now help me finish my large
program.  Thank you again,

Ryan
Subject: Re: another simple c question
From: evilsaltine-ga on 24 Nov 2004 18:59 PST
 
No problem. :-)

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy