Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5430
Number of posts: 16951

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
all printf before scanf ??? Posted by richardbautist on 2 Feb 2007 at 4:20 AM
To anyone,

Hello I need to make a program like this:
when you run the program it will dispaly like this:

COM_FEE:
INTERNET_FEE:
ID:
OTHER FEES:
TOTAL:


then the user will input the amount for each item...

how to do that...
???

pls. any kindhearted person...

P.S. (I only use TurboC and C Language only)


THANKS in ADVANCE

Report
Re: all printf before scanf ??? Posted by AsmGuru62 on 2 Feb 2007 at 5:03 AM
: To anyone,
:
: Hello I need to make a program like this:
: when you run the program it will dispaly like this:
:
: COM_FEE:
: INTERNET_FEE:
: ID:
: OTHER FEES:
: TOTAL:
:
:
: then the user will input the amount for each item...
:
: how to do that...
: ???
:
: pls. any kindhearted person...
:
: P.S. (I only use TurboC and C Language only)
:
:
: THANKS in ADVANCE
:
:
Why not do it one by one:

printf ();
scanf ();

printf ();
scanf ();

//...

Also, do you need to input TOTAL also?
Should it be all added and made into TOTAL?


Report
Re: all printf before scanf ??? Posted by richardbautist on 2 Feb 2007 at 5:07 AM
That's what i made but my teacher don't accept it. he wants all the things to be answered (example: com_fee) must be displayed first... and when i entered all the amounts the sum will print on the right side of the "TOTAL:"
Report
Re: all printf before scanf ??? Posted by MT2002 on 2 Feb 2007 at 5:59 AM
This message was edited by MT2002 at 2007-2-2 5:59:58

: That's what i made but my teacher don't accept it. he wants all the things to be answered (example: com_fee) must be displayed first... and when i entered all the amounts the sum will print on the right side of the "TOTAL:"
:
AsmGuru62's response can do just that. Unless he wants you to use puts()?



Report
Re: all printf before scanf ??? Posted by richardbautist on 2 Feb 2007 at 6:10 AM
EXAMPLE OUTPUT (underscores represent the cursor positions)

----------------
FIRST NUMBER: _
SECOND NUMBER:
SUM:
---------------
FIRST NUMBER: 3 (type 3 then press "ENTER" the cursor will now go down)
SECOND NUMBER : _
SUM:
---------------
FIRST NUMBER: 3
SECOND NUMBER: 5 (type 5 then press "ENTER" then the sum will print)
SUM: 8
---------------


That would the sequence of the program my teacher wants us to do

pls help me>...
Report
Re: all printf before scanf ??? Posted by MT2002 on 2 Feb 2007 at 11:24 AM
This message was edited by MT2002 at 2007-2-2 11:26:56

: EXAMPLE OUTPUT (underscores represent the cursor positions)
:
: ----------------
: FIRST NUMBER: _
: SECOND NUMBER:
: SUM:
: ---------------
: FIRST NUMBER: 3 (type 3 then press "ENTER" the cursor will now go down)
: SECOND NUMBER : _
: SUM:
: ---------------
: FIRST NUMBER: 3
: SECOND NUMBER: 5 (type 5 then press "ENTER" then the sum will print)
: SUM: 8
: ---------------
:
:
: That would the sequence of the program my teacher wants us to do

One possible way is getting a far pointer (using dos.h MK_FP macro) to 0xb8000 (counsol memory), and minipulating the counsole directly.

Or, you can try using the conio.h gotoxy() method to jump to a specific x/y character within the counsole. It only seems to work when jumping ahead of the insertion point though.

Probably the easiest method is display the output in a loop, and gather the numbers with kbhit() and getch(). The problem with this method is chance of possible (yet alot) of flicker because of constant redrawing.

Possible the best method is waiting for input, in which you can use one of the first two methods to modify the required locations.

----------
On a side note, this instructor does not seem to be that good. It is not clear to me why he is teaching out of date coding styles, and is using a non-ANSI compilient compiler to do so. Not to mention, conio.h varaies from different Borland compiliers. ie, alot of routines from TurboC 2.1 are excluded and/or modified in TurboC 3. --Huge portability problem.

This instructor is not teaching things that you can actually use in the field. Granted, I like TurboC 2.1, I am just wondering what the instructor was thinking when he chose it.

I guess what Im saying is: If a C/C++ compilier does not recognize "bool" as a data type--update your compilier asap!!
//end rant




Report
Re: all printf before scanf ??? Posted by AsmGuru62 on 3 Feb 2007 at 5:24 AM
I see now what you want to do. In Turbo C it is easy - no idea how in other systems. TC has functions to get the cursor location, so here is what you do - you print every line and get the X,Y of cursor for all lines. Then you just use gotoxy() to set a cursor into a positions you remembered and enter the data.

This is flexible way, meaning that if strings change - you do not have to modify the code for locations.

However, if the text will not change - then simply use gotoxy() to constant coordinates and you will be done:

gotoxy (20,  8); printf ("VALUE1: "); // X,Y here is 20,16
gotoxy (20,  9); printf ("SOMETHING: "); // X,Y here is 20,19
gotoxy (20, 10); printf ("MORE: "); // X,Y here is 20,14
// and so on...

// now use the locations to set a cursor and get data
gotoxy (20, 16); cscanf (...); // for VALUE1
gotoxy (20, 19); cscanf (...); // for SOMETHING
gotoxy (20, 14); cscanf (...); // for MORE

Report
Re: all printf before scanf ??? Posted by richardbautist on 3 Feb 2007 at 6:16 AM
TNX ASMGURU !!! You RULE !!!
BELOW is my own code i based it on what you helped me... tnx a lot.. try my code in Turbo C 2.01 pls and give a comment if it works !!! i'll wait tnx again

#include<stdio.h>
main()
{

int num1,num2,sum;

clrscr();

gotoxy(1,2);
printf("Enter no.1:");
gotoxy(1,3);
printf("Enter no.2:");
gotoxy(1,4);
printf("Their sum is:");


gotoxy(13,2);
scanf("%d",&num1);
gotoxy(13,3);
scanf("%d",&num2);
gotoxy(15,4);
sum=num1+num2;
printf("%d",sum);

getche();

}

Report
Re: all printf before scanf ??? Posted by Lundin on 5 Feb 2007 at 12:41 AM
This is how you do it in Windows:

#include <windows.h>
   
void gotoxy(int x, int y)
{
  static HANDLE hStdout = NULL;
  COORD coord;

  coord.X = x;
  coord.Y = y;

  if(!hStdout)
  {
    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  }
  
  SetConsoleCursorPosition(hStdout,coord);
}


Needless to say, you need a Windows compiler for that.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.