Thread: Simple program closing at end, no time to read...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    47

    Simple program closing at end, no time to read...

    Hi,

    I've started programming in C a few weeks ago and have decided to start writing a calculator for every topic/subject I get in maths at school.

    When I was writing my most recent, I came across a problem that it would not give the user time to read the final answer at the end. I must be doing something slightly wrong (and probably very stupidly simple aswell lol!). Does anybody know what's wrong with my source code below?

    Code:
    /* A Program by Jake C to work out the gradient of a line by entering
       2 coordinates */
       
    #include <stdio.h>
    #include <stdlib.h>
    
    main()
    {
          char xone[20];
          char xtwo[20];
          float xintone;
          float xinttwo;
          char yone[20];
          char ytwo[20];
          float yintone;
          float yinttwo;
          float answer;
          
          
          printf("Please enter the first set of coordinates seperated by a space:\n");
          scanf("%s",xone);
          scanf("%s",xtwo);
          xintone=atof(xone);
          xinttwo=atof(xtwo);
          
          printf("Please enter the second set of coordiantes seperated by a space:\n");
          scanf("%s",yone);
          scanf("%s",ytwo);
          yintone=atof(yone);
          yinttwo=atof(ytwo);
          
          answer=(yinttwo-yintone)/(xinttwo-xintone);
          
          printf("The gradient of the line is:\n\n%f",answer);
          getchar();
          
          return(0);
    }
    Thanks - I'm new to the forum and it seems like a really informative place! I'm looking forward to being here.

    Jake

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    scanf leaves the \n char in the input buffer
    getchar(); reads it and continues without pause

    add another - or (better IMHO) just run the program from the command prompt
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    47
    Thanks! that seems to have worked - but now there's another problem, when it prints the "answer" float, it comes up with 1.#INF...

    Don't suppose anyone knows why that is?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Could be a couple of things:
    1. xinttwo and yintone are not what they should be. (Look at your atof statements again.)
    2. Maybe it really is a line of infinite slope (i.e., vertical) -- again, modulo your x/y backwardness above.
    3. Double check atof if all fails (print out values of xintone etc.)

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You have two other flaws in your program, though.
    First is implicit main.
    Second is your scanf reading. Check this for proper reading strings.
    C is not a kind and safe language which puts that burden on YOU to do it right.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    47
    Quote Originally Posted by Elysia View Post
    You have two other flaws in your program, though.
    First is implicit main.
    Second is your scanf reading. Check this for proper reading strings.
    C is not a kind and safe language which puts that burden on YOU to do it right.
    So I should have "int main(void)" instead of "main()"?

    I've also corrected the buffer overflows.

    Quote Originally Posted by tabstop View Post
    Could be a couple of things:
    1. xinttwo and yintone are not what they should be. (Look at your atof statements again.)
    2. Maybe it really is a line of infinite slope (i.e., vertical) -- again, modulo your x/y backwardness above.
    3. Double check atof if all fails (print out values of xintone etc.)
    I can't see anything wrong with xinttwo and yintone...

    And it does it for all coordinates, not just vertical.

    I printed out all of the float variables and they were all fine...

    Thanks anyway though.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You read the value y1 into a variable named "xtwo" (and consequently xinttwo) and you read the value x2 into a variable named "yone" (and consequently yintone). If xintone, etc. print fine, then answer should be computed fine.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Jake.c View Post
    So I should have "int main(void)" instead of "main()"?
    Yeah.

    I've also corrected the buffer overflows.
    Terrific! You're a great student! ;)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    47
    Quote Originally Posted by tabstop View Post
    You read the value y1 into a variable named "xtwo" (and consequently xinttwo) and you read the value x2 into a variable named "yone" (and consequently yintone). If xintone, etc. print fine, then answer should be computed fine.
    Ah of course - how did I miss that?!?! I said it would be something simple haha!

    Thanks everyone!

    Jake

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding Program Running Time
    By pf732k3 in forum C Programming
    Replies: 6
    Last Post: 03-18-2008, 01:56 PM
  2. help with basic program
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 02-01-2006, 04:19 PM
  3. program not working...please look at this
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 01-30-2006, 10:33 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Replies: 5
    Last Post: 02-01-2003, 10:58 AM

Tags for this Thread