Now I'm confused

This is a discussion on Now I'm confused within the C Programming forums, part of the General Programming Boards category; Here is my question, where is the issue? I run my compiler and get nothing for warnings or errors, but ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    TX
    Posts
    19

    Now I'm confused

    Here is my question, where is the issue? I run my compiler and get nothing for warnings or errors, but after entering the first two inputs command prompt gets shutdown. What would cause that?


    Code:
    #include <stdio.h>
    
    int combine (int, float, char);
    
    int main ()
    {
    int a;
    float b;
    char c;
    
    printf ("Enter an interger:");
    scanf ("%d",&a);
    printf ("Enter a floating interger:");
    scanf ("%f",&b);
    printf ("Enter a character:");
    scanf ("%c",&c);
    puts (combine (a,b,c));
    
    system ("Pause");
    
    return 0;
    }
    int combine (int ger,float oat,char har)
    {
    int d;
    float e;
    char f;
    int string[81];
    
    sprintf(string, "%d %f %c",d,e,f);
    
    return (string [81]);
    }
    I find this more agrivating than getting errors all day long.

  2. #2
    csd@auth
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    71
    Are you sure that there are no errors? Because I tested it and it has a few.(in Dev-C++)
    1First of all how do you use
    the system function without including stdlib?
    2How do you use sprintf when d,e,f are not initiallized and
    3why in main you use
    Code:
    puts (combine (a,b,c));
    when puts() must have a string as an argument?
    Last edited by kantze; 12-19-2006 at 02:35 PM.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    I use C++, but in general, adding

    Code:
    getch();
    before return 0 may solve this problem. If not, try adding

    Code:
    gechar();
    getch() is non-standard though and avalible in the conio.h header file.
    Other people may be able to help you better.
    I'm just trying to be a better person - My Name Is Earl

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by al_engler
    Here is my question, where is the issue? I run my compiler and get nothing for warnings or errors, but after entering the first two inputs command prompt gets shutdown. What would cause that?
    scanf SOP.

    Generally, take all input as a string using fgets, if you need to convert to a number, try sscanf. If you intended to input a string, be wary of the newline that may be left in the input stream. Visit the FAQ for further details.

    other
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,459
    return (string [81]);
    you are returning out of bounds member of the string

    Don't think about using return string;
    It will be bad in another way

    sprintf(string
    sprintf awaits array of characters to fill, not array of ints


    If you want to fill a buffer in your function to output it in the calling function - declare the buffer in the calling function and provide it as a parameter to your function.
    Also don't forget to provide a buffer length to check for buffer overruns
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused about Memory
    By gL_nEwB in forum C++ Programming
    Replies: 22
    Last Post: 06-20-2006, 07:32 PM
  2. why wont this compile?!? :confused:
    By jdude in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2004, 12:13 AM
  3. So Now Im getting confused?!?!?
    By zergdeath1 in forum C++ Programming
    Replies: 11
    Last Post: 03-06-2004, 04:41 PM
  4. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM
  5. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 10:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21