• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

help with standard C

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Squadfer

Member
Joined
Apr 1, 2010
Hey there. I've been working on a specific lab for a week now and I cant seem to figure out how to get the output to do what I need it to do. In this lab I'm having to enter some employee data from the keyboard, do some calculations to the data and print the output to a file before new employee data is added to the program. The problem i'm running into is I need to be able to put a "$" infront of some numbers that are being generated by the calculations.

I'm using fprintf to print the output to a file.

for instance this is what i need it to look like:
emp1 (other data) (other data) -----$65.09
emp2 (other data) (other data) ----$102.09
emp3 (other data) (other data) --$1104.04 (the decimal place has to line up vertically. disregard the ---'s, they are suppose to be whitespaces)

However, I personally have only been able to come up with two forms of output and neither are what I need them to be.

$"%.2lf" gives me:
emp1 (other data) (other data) $65.09
emp2 (other data) (other data) $102.09
emp3 (other data) (other data) $1104.04

and $"%8.2lf" gives me:
emp1 (other data) (other data) $-----65.09
emp2 (other data) (other data) $---102.09
emp3 (other data) (other data) $-1104.04 (again disregard the ---'s, they are suppose to be whitespaces)

I've also tried $"%-8.2lf" but that gives me the same output as $"%.2lf". Everything else for the lab is completed expect for this problem I'm having. If anyone could chime in and give me some pointers on how to accomplish this task I would greatly appreciate it.
 
Last edited:
What does this output? "$%11.2lf"

That does the same thing as "$%8.2lf" that I posted, only difference is that there are three more padded spaces reserved for the calculated number which in turn causes more space between the number and the $.
 
Last edited:
Code:
#include <stdio.h>
#include <ctype.h>

#define SIZE 12

int main(void) {
   int i,j,spaces=0;
   double n=10.0;
   char s[SIZE]={""};;
   
   for(i=0;i<5;i++) {
      sprintf(s,"%11.2f",n); //put the digits into a string

      j=0;
      while(!(isdigit(s[j])))  //find the first digit
         ++j; 
      s[--j]='$';               //add the $ char
      for(spaces=0;spaces<SIZE;spaces++) //remove spaces from string
         s[spaces]=s[spaces+j];
      fprintf(stdout, "%11s\n",s);   //print out the string with field width 
      n*=10.05;                          //just to show it works
   }
   return 0;
}
 
Does the assigment specifically tell you to put the $ next to the number? In any real printout, whoever reads it will hate having them in the middle of the numbers. I like problems that make people think to solve problems, but teaching people to do something the wrong way is a wrong way to go about teaching.
 
The position of the dollar sign is different than in your examples, relative to the "

Oh good eye, didnt see that. However that is an error on my part, the first " is not suppose to be there as that code is part of a string of code to be output. My examples were ment to be $%.2lf" and $%10.2lf" as this is my actuall piece of code: fprintf(outFile, "\n%-4d\t %6d %-10s%-11s%8.2lf %8.2lf $%8.2lf") with the one in question being the last entry. My code does have the variable names added in to finish off the syntax for fprintf, I just didnt copy them to here.

Does the assigment specifically tell you to put the $ next to the number? In any real printout, whoever reads it will hate having them in the middle of the numbers. I like problems that make people think to solve problems, but teaching people to do something the wrong way is a wrong way to go about teaching.

It doesnt specifically say it inside the assignment. However in the example printout that is attached to the assignment, it shows the $ right next to the numbers.


Code:
#include <stdio.h>
#include <ctype.h>

#define SIZE 12

int main(void) {
   int i,j,spaces=0;
   double n=10.0;
   char s[SIZE]={""};;
   
   for(i=0;i<5;i++) {
      sprintf(s,"%11.2f",n); //put the digits into a string

      j=0;
      while(!(isdigit(s[j])))  //find the first digit
         ++j; 
      s[--j]='$';               //add the $ char
      for(spaces=0;spaces<SIZE;spaces++) //remove spaces from string
         s[spaces]=s[spaces+j];
      fprintf(stdout, "%11s\n",s);   //print out the string with field width 
      n*=10.05;                          //just to show it works
   }
   return 0;
}

Ok, first time seeing sprintf(). Correct me if im wrong. This is taking n converting it to a char array (string). it is then traversing the char array till it finds no more digits, once it finds a non digit, it then places the $ in that spot. If so I can easily implement this. Thanks
 
Much better:

Code:
#include <stdio.h>
#include <ctype.h>

#define SIZE 12

int main(void) {
   int i;
   double n=10.0;
   char s[SIZE]={"$"};;
   
   for(i=0;i<5;i++) {
      sprintf(s+1,"%.2f",n);
      fprintf(stdout, "%11s\n",s);
      n*=10.05;
   }
   return 0;
}
 
Back