Thread: What type to use

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    270

    Question What type to use

    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

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    What's wrong with strings?

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    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?

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    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

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    haha very true pete.

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    270

    umm

    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,"&#37;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");
    	}
    }
    Does the strcmp look ok?

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    No, it doesn't.

  8. #8
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    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

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    perhaps you should change those fprintf functions to standard output function and input function for i/o operation as show below

    Code:
    printf("Please enter exposure time: "); 
    //fprintf(output,"Please enter exposure time: "); fflush(output);
    fscanf(input,"&#37;f",&exposureTime);  
    
    printf("Please enter aperture value: ");
    //fprintf(output,"Please enter aperture value: "); fflush(output);
    fgets(aperture, sizeof(aperture), stdin);
    //fscanf(input,"%s",&aperture);
    ssharish2005

  10. #10
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    alright am just gonna have a read up on the fgets, never seen it before

  11. #11
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    could someone briefly explain what the fflush(output); means?
    thanks

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by taurus View Post
    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.

  13. #13
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM
  4. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM