I need to write a program which reads a list of integers using scanf() (no using arrays). The first number is the representative exchange rate, and all remaining numbers represents cash amounts in dollars. The program should print the cash amount in dollars in "$" column and print in the "IS" column the cash amounts in the other coin (dollar*rate). In addition, program should print the 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 the U.S Dollar Representative Exchange Rate \n ");
scanf(" %f", &rate);
printf(" \nPlease enter the numbers which represents cash amount in Dollars (and then 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 some questions:
Based on the above program:
user enters "rate" -> press enter key -> "enter list of numbers" -> press enter key.
The problem: I want to print the column headers ($ and IS) only AFTER the above flow (i.e only AFTER user entered a list of integers and pressed enter key).
I can`t just do the printing after the while loop, because then the columns headers will be printed below the columns values which is wrong. This is because I am restricted to print the next integer during the while loop because scanf will lose the previous value once the next one is read (correct?!)...
Is there any way to iterate through all the integers stored in scanf buffer? so I can print the headers and then the values in one shot?
In the above program, it seems that after all the column values are printed, the sum of each column is not printed until CTRL+D is entered. How can I make the sum to be printed also with the column values in one shot? I tried to change the while condition to != '\n' but it does not work because I am dealing with float and not char.
you can assume the list of integers are entered in a single line.
Please assist.
Thanks
I did a few changes.
Now I am left only with the 2nd problem. How can I print the sum with the columns headers in 1 shot? I don`t want to press CTRL+D and only then have the sum printed. I want columns Headers + Column Values + Columns Sum to be printed in one shot.
#include <stdio.h>
int main()
{
float rate = 0;
float numDollar = 0;
float numShekel = 0;
float sumDollar = 0;
float sumShekel = 0;
printf(" \nPlease enter the U.S Dollar Representative Exchange Rate, and a list of cash amounts in Dollars in a single line (CTRL+D to 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;
}
numDollar!='\n'
to do, given thatnumDollar
is afloat
? – Paul R(numDollar!='\n')
is wrong but will compile because C will infer a cast fromint
(which is what'\n'
is, like it or not) tofloat
. So it'll be a check to see ifnumDollar
is 10 or 13, depending on platform. – Mike DeSimone