I am creating a program. Now i have to get the user to input the value in the format "f1.5", "f6", "f5.6", etc etc...
What type can i use for those?
Thanks
I am creating a program. Now i have to get the user to input the value in the format "f1.5", "f6", "f5.6", etc etc...
What type can i use for those?
Thanks
hmmm u can use string? ok cool, i just started C. Thanks
What about if i want to display an answer as 1/200. Is it possible to be displayed like that and not in decimal?
Erm, well, you can't use std::string in C, you'll have to use char arrays.
Within a char array, you can have "1/200", but you won't be able to do calculations with it.
There's also (to my knowledge) no library function which will take a decimal number and find it's fraction representation (Now, *that* would be a good homework task!)
QuantumPete
haha very true pete.
Does the strcmp look ok?Code:#include <stdio.h> #include <string.h> int main(int argc, char ** argv) { FILE * output = stdout; FILE * input = stdin; float exposureTime; char aperture[5]; fprintf(output,"Please enter exposure time: "); fflush(output); fscanf(input,"%f",&exposureTime); fprintf(output,"Please enter aperture value: "); fflush(output); fscanf(input,"%s",&aperture); if(strcmp (aperture == "f2" || "f3" || "f6") { printf("ERROR\n"); } else { fprintf(output,"\n"); fprintf(output," Camera Test Code\n"); fprintf(output,"~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); fprintf(output,"The exposure time is %.2f\n",exposureTime); fprintf(output,"The aperture value is %s\n",aperture); fprintf(output,"~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); } }
umm...no.
strcmp takes two string and returns 0 if they're equal.
So :
strcmp("foo", "foo") will return 0.
strcmp("foo","bar") will not return 0.
Therefore, you'd probably want something like :
strcmp(aperture,"f2") == 0 || strcmp(aperture,"f3") == 0 || ...
Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction
perhaps you should change those fprintf functions to standard output function and input function for i/o operation as show below
ssharish2005Code:printf("Please enter exposure time: "); //fprintf(output,"Please enter exposure time: "); fflush(output); fscanf(input,"%f",&exposureTime); printf("Please enter aperture value: "); //fprintf(output,"Please enter aperture value: "); fflush(output); fgets(aperture, sizeof(aperture), stdin); //fscanf(input,"%s",&aperture);
alright am just gonna have a read up on the fgets, never seen it before
could someone briefly explain what the fflush(output); means?
thanks
It is a call that "outputs" any pending data from the buffer that is used to optimize the output to files - because it's "expensive" to call the low-level write routines for one or a few characters at a time, it is better to store the data in a buffer (essentially an array), so that when there's little bits of data printed out, it's stored in this buffer, rather than calling the OS low-level write routine for each one of those. A bit like using a bag to carry potatos from your the shop, rather than carrying one or two potato(s) at a time to get them to your car.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
briefly: When you printf, the output isn't send to your screen immediatly, but is buffered (to save time and resources). To force this buffer to be emptied (flushed) you run fflush(). This will print any outstanding output onto your screen.
QuantumPete
"No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
"Have you tried turning it off and on again?" - The IT Crowd