can't make it work

Aug 2 2009 3:07 AM

Hi guys,
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.
 
 Purchase_details:
 printf("Details of the purchase...");
 if(qty1>0){
  gotoxy(6,4);
  printf("%s",prdnme1);
  gotoxy(26,4);
  printf("%f",unit_price1);
  gotoxy(43,4);
  printf("%d",qty1);
  gotoxy(60,4);
  subtotal1=unit_price1*qty1;
  printf("%f",subtotal1);
  }
 if(qty2>0){
  gotoxy(6,6);
  printf("%s",prdnme2);
  gotoxy(26,6);
  printf("%f",unit_price2);
  gotoxy(43,6);
  printf("%d",qty2);
  gotoxy(60,6);
  subtotal2=unit_price2*qty2;
  printf("%f",subtotal2);
  }
 if(qty3>0){
  gotoxy(6,8);
  printf("%s",prdnme3);
  gotoxy(26,8);
  printf("%f",unit_price3);
  gotoxy(43,8);
  printf("%d",qty3);
  gotoxy(60,8);
  printf("%f",unit_price3*qty3);
  }

 
THanks..

Answers (14)

0
jhaysonn jhaysonn

jhaysonn jhaysonn

  • 0
  • 13
  • 0
Aug 8 2009 12:30 AM
thanks, friend. I have figured it out.  ^_^
0
Danatas Gervi

Danatas Gervi

  • 0
  • 910
  • 0
Aug 5 2009 10:08 AM
this

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.....
 




0
Danatas Gervi

Danatas Gervi

  • 0
  • 910
  • 0
Aug 5 2009 9:55 AM
this

  if ((argc < 2) ||
            (sscanf(argv[1],"%d",&n) != 1) ||
            (n < 1) || (n > 9)) {
        printf("usage : %s <n>\n",argv[0]);
        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
0
jhaysonn jhaysonn

jhaysonn jhaysonn

  • 0
  • 13
  • 0
Aug 5 2009 8:14 AM
hi.. I've corrected it. Added a formula on the line to terminate the adding value on the qty parts. Anyways, I have another concern.. can you help me with this? here is the link: http://answers.yahoo.com/question/index;_ylt=ArgkNV.2Q1UpFsomYZ3GIK3sy6IX;_ylv=3?qid=20090805044719AA5XEqb thanks..
0
Danatas Gervi

Danatas Gervi

  • 0
  • 910
  • 0
Aug 5 2009 4:22 AM
>>I also don't get what is stdafx.h

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 .
0
Danatas Gervi

Danatas Gervi

  • 0
  • 910
  • 0
Aug 5 2009 4:18 AM
The idea is:

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.
0
jhaysonn jhaysonn

jhaysonn jhaysonn

  • 0
  • 13
  • 0
Aug 4 2009 7:03 PM

hi,
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.
0
Danatas Gervi

Danatas Gervi

  • 0
  • 910
  • 0
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):

 #include "stdafx.h"
#include"conio.h"
#include"dos.h"

struct ProductInfo
{
    char Name[10];
    int Qtty;
    float Prise;
};
int ShowCurrentPurchaseDetails(ProductInfo   onePurchase[]);

int _tmain(int argc, _TCHAR* argv[])
{

 ProductInfo onePurchase[10] = {0,0,0,0,0,0,0,0,0,0};
 cprintf("Welcome to D129 Store");
 getch();
 
 cprintf("\nYou're One-Stop Store..");
 getch();
 
char ContinuePurchase = 'y';
int ii = 0;
while (ContinuePurchase == 'y' && ii < 10)
{
    printf("\nPlease encode the product you want to purchase : ");
    scanf("%s", &onePurchase[ii].Name);
    printf("Enter it's unit price : ");

    scanf("%f",&onePurchase[ii].Prise);
    printf("\nPlease encode the quantity you require : ");
 
    scanf("%d",&onePurchase[ii++].Qtty);
    ShowCurrentPurchaseDetails(onePurchase);

    printf("\nDo you want to continue? (Y/N)\n");
    ContinuePurchase = getch();
}

 printf("\nThank You for shopping...");
 printf("\nSee you again next time....");

 getch();
}
int ShowCurrentPurchaseDetails(ProductInfo  onePurchase[])
{
    printf("\nDetails of the purchase...");
    int ii = 0;
    float sum = 0;
    for (ii = 0; ii < 10; ii++)
    {
       if (onePurchase[ii].Qtty >0)
       {
        printf("\nName %s  prise %f  qtty %d",onePurchase[ii].Name,  onePurchase[ii].Prise, onePurchase[ii].Qtty);
        sum += onePurchase[ii].Prise * onePurchase[ii].Qtty;
       }
     }
      printf("\ntotal sum now = ");
      printf("%f",sum);
     return 0;
}


you can use yours gotoxy() and clrscr() instead  "\n"  (new line) 

Thanks for this C coding practice. It was so nostalgically for me.

:-)


0
jhaysonn jhaysonn

jhaysonn jhaysonn

  • 0
  • 13
  • 0
Aug 2 2009 7:41 AM

thanks anyway..
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.
0
Danatas Gervi

Danatas Gervi

  • 0
  • 910
  • 0
Aug 2 2009 7:32 AM
I see now, that your program is not for windows console...

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)...
:-(
0
jhaysonn jhaysonn

jhaysonn jhaysonn

  • 0
  • 13
  • 0
Aug 2 2009 7:01 AM

hmm.. you have ym account? its easier to communicate there.. add me denn_c10
 
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..
 ---deleted---

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....
0
Danatas Gervi

Danatas Gervi

  • 0
  • 910
  • 0
Aug 2 2009 6:15 AM
This is only output code of your program:
(I do not know, what is in gotoxy(...) function, and how it affects on global variables qtty1 ets.)
 
int _tmain(int argc, _TCHAR* argv[])
{
printf("Details of the purchase...");
printf("\r\n");
 
printf("%s","erer"); 
printf("%f",6); 
printf("%d",6);
printf("%f",6);
 
printf("\r\n");
 
printf("%s","erer"); 
printf("%f",8); 
printf("%d",8);
printf("%f",8);
 
printf("\r\n");
 
printf("%s","erer"); 
printf("%f",9);
printf("%d",9); 
printf("%f",9);
 
 printf("\r\n");
 getchar();

    return 0;
}

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.
:-)

0
jhaysonn jhaysonn

jhaysonn jhaysonn

  • 0
  • 13
  • 0
Aug 2 2009 5:27 AM

hmm thanks, but it didn't work.
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?
0
Danatas Gervi

Danatas Gervi

  • 0
  • 910
  • 0
Aug 2 2009 3:58 AM
Try it

 printf("\r\n");
this is  "carridge return, line feed"
:)