Thread: new to C. need basic help

  1. #16
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Code:
    char aperturef;
    ...
    fscanf(input,"%s",&aperturef);
    You're trying to put a string into a char! That won't work, either declare a char array that is big enough to hold your string or malloc some memory for it.

    QuantumPete

    P.S. What matsp said...
    Last edited by QuantumPete; 08-30-2007 at 08:00 AM.
    "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

  2. #17
    Registered User
    Join Date
    Aug 2007
    Posts
    46
    i dont really understand.

    gah. ive never been able to do programming.

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your variable aperturef is a char. That can hold ONE character. %s when used in scanf will absolutely read in AT LEAST two characters, one being what you typed, the second being a zero to indicate the end of the string. So the next location in memory after aperturef will be set to zero. If there's some meaningfull data in that location, it will be overwritten by this zero.

    The point about printing it is related to the same - printf() will see %s as an indication that the argument is a pointer to char, where there is zero or more characters followed by a zero-character. E.g. "Hello" would be (( 'H', 'e', 'l', 'l', 'o', 0 )). If you try to do this with a char only, printf will continue along until it finds a zero - whcih if you are not lucky, could be severa dozen or hundreds of bytes of memory! If you are "really unlucky", it runs to a point where there is no valid memory any longer, and the application crashes!

    --
    Mats

  4. #19
    Registered User
    Join Date
    Aug 2007
    Posts
    46
    ok.

    thankyou. that actually makes sense.
    so in this case. do i need to change my input method of %s.
    or should i change my char to something that can hold more characters.

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by k4k45hi View Post
    ok.

    thankyou. that actually makes sense.
    so in this case. do i need to change my input method of %s.
    or should i change my char to something that can hold more characters.
    Yes, one of those two would work. You can choose either.

    By the way, it looks like your code expects 1.4f rather than f1.4 - not sure that's what you meant to do.

    --
    Mats

  6. #21
    Registered User
    Join Date
    Aug 2007
    Posts
    46
    i think i will go with the changing %s.

    now what can i change this to to make it work.
    and ive changed the inputs around so that aperturef is first.

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by k4k45hi View Post
    i think i will go with the changing %s.

    now what can i change this to to make it work.
    and ive changed the inputs around so that aperturef is first.
    Try googling something like "format specifiers scanf" perhaps?

    One of the important parts of learning to program is "how to find information about what you are doing" - there won't always be someone else to ask.

    --
    Mats

  8. #23
    Registered User
    Join Date
    Aug 2007
    Posts
    46
    ok well,
    i had done that while waiting for a reply and had replaced the %s with a %c.

    so the program no longer comes up with a segmentation fault but instead reads back 0.000000. i will give it a shot and see if i can get it on my own.

    and let you know how i go.

    thanks for all your help mate.

  9. #24
    Registered User
    Join Date
    Aug 2007
    Posts
    46
    why is this code returning 0.000000.
    ive tried everything i could see, but nothing fixed it.



    Code:
      float aperture;
      char aperturef;
    
      fprintf(output,"please enter aperture value: ");
      fscanf(input,"%c%f",&aperturef,&aperture);
    
      fprintf(output,"the aperture value is %c%f\n ",aperturef,aperture);

  10. #25
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    works for me...

    What exactly is the problem? I type in:
    f1.2
    and that's what I get out as well, that's what you want, no?

    QuantumPete
    Last edited by QuantumPete; 08-30-2007 at 09:29 AM.
    "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

  11. #26
    Registered User
    Join Date
    Aug 2007
    Posts
    46
    must mean the rest of the code must be interfering with it.

    heres the whole thing.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char ** argv)
    
    
    {
      // Mainline Variable Declarations
      FILE * output = stdout;
      FILE * input = stdin;
    
      float  exposuretime;
      float aperture;
      char  aperturef;
    
      fprintf(output,"please enter exposure time: ");
      fscanf(input,"%f",&exposuretime);
    
    
      fprintf(output,"please enter aperture value: ");
      fscanf(input,"%c%f",&aperturef,&aperture);
    
      fprintf(output,"the exposure time is: %f\n ",exposuretime);
      fprintf(output,"the aperture value is %c%f\n ",aperture,aperturef);
    
    }

  12. #27
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Code:
    fprintf(output,"the aperture value is &#37;c%f\n ",aperture,aperturef);
    You have aperture and aperturef the wrong way round!
    "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

  13. #28
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You may want to check the return result from fscanf(), e.g.
    Code:
      int res;
      .... 
      res = fscanf(...);
      if (res != number_of_things_wanted) {
          printf("error...");
      }
    For example, fscanf(stdin, "%d %d", &i1, &i2) woould return 2 on successfull read of two integers.

    --
    Mats

  14. #29
    Registered User
    Join Date
    Aug 2007
    Posts
    46
    hmm.

    ive never used return result

  15. #30
    Registered User
    Join Date
    Aug 2007
    Posts
    46
    well ive changed aperture with aperturef. but didnt make a difference

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [ANN] New script engine (Basic sintax)
    By MKTMK in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2005, 10:28 AM
  2. what are your thoughts on visual basic?
    By orion- in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-22-2005, 04:28 AM
  3. visual basic vs C or C++
    By FOOTOO in forum Windows Programming
    Replies: 5
    Last Post: 02-06-2005, 08:41 PM
  4. Basic Graphics programming in C, please help
    By AdRock in forum Game Programming
    Replies: 3
    Last Post: 03-24-2004, 11:38 PM