Can anyone help me.. I can't make this code work.
The third entry overwrites the second entry.. how can I make it go to the next line.
|
THanks..
|
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
jhaysonn jhaysonnPosted Aug 8, 2009, 12:30 AM
Danatas GerviPosted Aug 5, 2009, 10:08 AM
str = calloc(n+1,sizeof(char));
- getting the memory in heap (not in stack)
for string (array of char) +1 byte more for 0 array terminator
this
for (i = 0, j = n; i < n; i++, j--)
{
str[i] = j + '0';
}
inserting to string (do not forget this is the array of chars) 654321
this
for (i = 1; i <= n; i++) {
printf("%s\n",str);
memset(str,' ',i);
}
two things - printing,
and modifying the string directly in memory - by inserting to char 1, char 2 ets the char ' ' (space)
free(str); // freeing the memory (no memory leakadge! :-) )
return 0;
This exaple seems optimal to you demend (if you want only output, and do not interesting to save this tabulated array.....
Danatas GerviPosted Aug 5, 2009, 9:55 AM
if ((argc < 2) ||
(sscanf(argv[1],"%d",&n) != 1) ||
(n < 1) || (n > 9)) {
printf("usage : %s
puts(" where 0 < n < 10");
return -1;
}
is command line argument parameters validation.
you can remove it - it is not affect on code - you can set this n haddcoded:
int n = 6; // for example
jhaysonn jhaysonnPosted Aug 5, 2009, 8:14 AM
Danatas GerviPosted Aug 5, 2009, 4:22 AM
This is header file that needs to compile the code in my enviroment (VS2008)
you may use all your function with your header files. Remove it .
Danatas GerviPosted Aug 5, 2009, 4:18 AM
To use array of struct for grouping linked data - not Qtt1, Qtt2 ... Qtty10 .... Qtty13456. and ProdName500!
To use cicle (wile, for ...) instead spagetty-like code goto Qtyy1Calculate goto back....
To use fuction call - for every same algorithm parth.
You problem - this is not "output hides previos data" - the problem is that all algorithm is not developed.
jhaysonn jhaysonnPosted Aug 4, 2009, 7:03 PM
Thanks for that, but I find it hard to understand that source code, sorry just a beginner here.. hehe
I also don't get what is stdafx.h
thanks again.
Danatas GerviPosted Aug 4, 2009, 12:14 PM
It's me again.
I wanted to say, that your code was terrible. But it is not good to do so and do not explain, why.
So, your code was terrible. ;-)
Have a look, how it may be designed (use it as template):
you can use yours gotoxy() and clrscr() instead "\n" (new line)
Thanks for this C coding practice. It was so nostalgically for me.
:-)
jhaysonn jhaysonnPosted Aug 2, 2009, 7:41 AM
I am using the TurboC++IDE (as shown on it's window).
I can't really make it work... *sigh*
can somebody help me? appreciate so much.
thanks.
Danatas GerviPosted Aug 2, 2009, 7:32 AM
I have no gotoxy() and clrscr() function - probably they are existing in additional dll for your devise...
So, try to use gotoxy() for setting the next line for output, and/or clear previos string by prining to same place an "space" string like printf(" ");
Sorry, that i cannot help you more - i have different programming enviroment (i mean - i cannot see the problem in runtime)...
:-(
jhaysonn jhaysonnPosted Aug 2, 2009, 7:01 AM
actually the situation is this: I am asked to create a program on C that will do the purchase, list and compute of all the products bought on me. With a max of ten items, then display all items bought before showing the total amount to be paid and the change.
here is my complete code..
my problem is: on the third entry, after encoding the data for the second entry there is already a display on the third entry, when i encode the data on the third it overwrites the previous data on the third. How can I eliminate an entry to appear before I encode a data?
thanks....
Danatas GerviPosted Aug 2, 2009, 6:15 AM
(I do not know, what is in gotoxy(...) function, and how it affects on global variables qtty1 ets.)
this is console output :
Details of the purchase...
erer0.00000060.000000
erer0.00000080.000000
erer0.00000090.000000
Print the output that you need - may be it help me to understand your problem.
:-)
jhaysonn jhaysonnPosted Aug 2, 2009, 5:27 AM
actually on the first entry there's already an output for the third entry, when third entry is encoded, it overwrites the second entry.
did i miss something on my program?
Danatas GerviPosted Aug 2, 2009, 3:58 AM
:)