c - Is there any way to keep all scanf -


i need write program reads list of integers using scanf() (no using arrays). first number representative exchange rate, , remaining numbers represents cash amounts in dollars. program should print cash amount in dollars in "$" column , print in "is" column cash amounts in other coin (dollar*rate). in addition, program should print sum of each column.

#include <stdio.h>   int main() {     float rate = 0;     float numdollar = 0;     float numshekel = 0;     float sumdollar = 0;     float sumshekel = 0;       printf(" \nplease enter u.s dollar representative exchange rate \n ");     scanf(" %f", &rate);      printf(" \nplease enter numbers represents cash amount in dollars (and press enter) \n ");      printf("\n$\t\t\t\t\tis\n");      while (scanf(" %f", &numdollar) !=eof && (numdollar!='\n'))     {       sumdollar = sumdollar + numdollar;       numshekel = numdollar * rate;       sumshekel = sumshekel + numshekel;        printf("%3.2f\t\t\t\t\t%3.2f\n",numdollar,numshekel);     }       printf("%3.2f\t\t\t\t\t%3.2f\n",sumdollar,sumshekel);      return 0; } 

i have questions:

  1. based on above program:

    user enters "rate" -> press enter key -> "enter list of numbers" -> press enter key.

    the problem: want print column headers ($ , is) after above flow (i.e after user entered list of integers , pressed enter key).

    i can`t printing after while loop, because columns headers printed below columns values wrong. because restricted print next integer during while loop because scanf lose previous value once next 1 read (correct?!)...

    is there way iterate through integers stored in scanf buffer? can print headers , values in 1 shot?

  2. in above program, seems after column values printed, sum of each column not printed until ctrl+d entered. how can make sum printed column values in 1 shot? tried change while condition != '\n' not work because dealing float , not char.

you can assume list of integers entered in single line.

please assist.

thanks


i did few changes.

now left 2nd problem. how can print sum columns headers in 1 shot? don`t want press ctrl+d , have sum printed. want columns headers + column values + columns sum printed in 1 shot.

#include <stdio.h>   int main() {     float rate = 0;     float numdollar = 0;     float numshekel = 0;     float sumdollar = 0;     float sumshekel = 0;       printf(" \nplease enter u.s dollar representative exchange rate, , list of cash amounts in dollars in single line (ctrl+d finish) \n ");     scanf(" %f", &rate);      printf("\n$\t\t\t\t\tis\n");      while (scanf("%f", &numdollar) != eof)     {       sumdollar = sumdollar + numdollar;       numshekel = numdollar * rate;       sumshekel = sumshekel + numshekel;        printf("%3.2f\t\t\t\t\t%3.2f\n",numdollar,numshekel);     }       printf("%3.2f\t\t\t\t\t%3.2f\n",sumdollar,sumshekel);      return 0; } 

what want isn't possible scanf , printf , without using arrays. you'd need take on character-by-character input system, using readline library or terminfo library.

the fundamental problem scanf(' %f', ...) scan until reads floating point value, discarding whitespace until then. "whitespace" here not means spaces , tabs line ends well! user can type return few times, number, return few more, second number, , return before code gets it. code need obliterate malformatted input, work way proper line, may have scrolled away then, , print formatted values.

thus need better scanf able block return (and enter , long lines) scrolling screen, , need terminfo or equivalent control sequences needed things "erase line" , "move cursor point".

scanf no substitute robust parser, unfortunately. it's designed around reading input files , isn't smart terminal input.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -