Menu

Please could someone take a look at this :)

Alex
2008-10-29
2012-09-26
  • Alex

    Alex - 2008-10-29

    Hey everyone, i've just joined up to source forge and would appreciate a little help the following simple C programme created using bloodshed should work as a 2 number calculator. However every time i execute the programme and try and enter the required numbers when prompted windows vista complains that it is no longer responding and jams the application!

    is there something in my code causing this or is it just vista!
    cheers for any help
    kind regards
    Alex


    include <windows.h>

    include <stdio.h>

    include <stdlib.h>

    main()

    / Ex5.c Harder Selection Statements - 2 Number Calculator /

    {
    char reply;
    float reply1, reply2, answer1, answer2, answer3, answer4;

    printf("This programme works as a two number calculator\n");
    printf("Please enter the first number in the calculation\n");
    scanf("%f", reply1);
    fflush (stdin);

    printf("Please enter the second number in the calculation\n");
    scanf("%f", reply2);
    fflush (stdin);

    printf("Now please type the following depending on the action required\n");
    printf("+, -, x, /\n");
    scanf("%c", reply1);
    fflush (stdin);

    if (reply == '+')
    {
    answer1 = reply1 + reply2;
    printf(" The answer of %f + %f = %f\n", reply1, reply2, answer1);
    system("PAUSE");
    }

    else

    if (reply == '-')
    {
    answer2 = reply1 - reply2;
    printf(" The answer of %f - %f = %f\n", reply1, reply2, answer2);
    system("PAUSE");
    }

    else

    if (reply == '/')
    {
    answer3 = reply1 / reply2;
    printf(" The answer of %f / %f = %f\n", reply1, reply2, answer3);
    system("PAUSE");
    }

    else

    if (reply == 'x')
    {
    answer4 = reply1 * reply2;
    printf(" The answer of %f * %f = %f\n", reply1, reply2, answer4);
    system("PAUSE");
    }
    }

     
    • cpns

      cpns - 2008-10-31

      Ubuntu is user friendly not perhaps developer friendly. With a VM you could easily try out several distributions, or in fact have more than one! One cool thing about VMs is that they can communicate with other VMs or real machines over your network, which makes a lot of network application testing scenarios far simpler (and less expensive).

      > [Bloodshed] is much more user friendly

      More user friendly than what? There are IDEs for use with Linux as well - you don't have to use the command line tools directly. Dev-C++ is not a compiler, it is an IDE. Under the hood it uses variants of the same command-line GNU tools you would have used on Linux - in fact you can use them from teh command line if you wish.

      Linus IDE examples: http://www.yolinux.com/TUTORIALS/LinuxTutorialSoftwareDevelopment.html#IDE

      I would suggest using EasyEclipse for C/C++: http://www.easyeclipse.org/site/distributions/cplusplus.html

       
    • Alex

      Alex - 2008-10-29

      P.S I am using Dev-C++ 4.9.9.2

      Cheers

       
    • Alex

      Alex - 2008-10-29

      P.P.S

      I have since noticed an error with the code which i have rectified, however it still jams :(

      printf("Now please type the following depending on the action required\n");
      printf("+, -, x, /\n");
      scanf("%c", reply);
      fflush (stdin);

       
    • Alex

      Alex - 2008-10-29

      PROBLEM Solved, my debuger wasn't picking up the problem, - segmentation, silly mistake :O

      thanks anyways :D

       
    • cpns

      cpns - 2008-10-29

      It is always helpful if you could explain teh solution when you solve it yourself. Someone else may learn from your experience. No doubt you added the & address of operator to your scanf() parameters?

      Note that adding the -Wformat compiler option will produce a warning for that error. If you use -Werror as well, it will transmute warnings to errors - always a good idea. For good measure use -Wall also. Let teh compiler do your debugging for you where it can!

      Other issues:

      > fflush (stdin);

      The ISO standard does not define fflush() for input streams. It happens to work in Microsoft's C library, but beware, it is not portable, and will fail in other libraries.

      Consider:

      scanf("%f", &reply1);
      while( getchar() != '\n' ) {} ;

      scanf("%c", &reply);
      while( reply != '\n' && getchar() != '\n' ) {} ;

      Note the difference when using %c because the character might legitimately be a '\n', otherwise the getchar() call would block waiting for new input.

      Because the if/else if/else construct test the same integer variable each time, you could better use a switch/case construct

      switch( reply )
      {
      case '+' :
      ...
      break ;

      case '-' :
      ... etc
      }

      Also consider a default case (or final else) to trap invalid input.

      Clifford

       
    • Alex

      Alex - 2008-10-30

      Thank you for the advice, I appreciate it. I have only just started playing with C as I am beginning a 5 year engineering course. I've been trying to get to grips with the compiler software at home. It has taken me a while, i started by partitioning my HDD and installing Linux, however after falling flat on my face with all the patches necessary to make it work with the libraries and directories I went in search of a windows version and found Bloodshed. Seems to be a sound piece of software. And this forum is really useful, been reading over various posts.

      I have touched on the switch construct before, however up until now I did not consider using it in this way, a great suggestion, I shall have a play with it later on.

      Kind Regars

      Alex

       
      • cpns

        cpns - 2008-10-30

        > i started by partitioning my HDD and installing Linux

        There is an easier and safer way. Use a virtual machine. http://www.vmware.com/products/server/

        > falling flat on my face with all the patches necessary
        > to make it work with the libraries and directories

        Strange, Linux is normally the environment of choice for using GCC. What distribution did you use? Not all Linux distributions are as well configured for software development as others - at least not out of the box.

        > I went in search of a windows version and found Bloodshed.

        Note that the MinGW flavour of GCC differes from say Linux or Cygwin versions in that it does not use the GNU C library (which required a POSIX compliant OS). It uses Microsoft's C library, so they differ in respect to some OS and platform specific functions.

        If it were merely C/C++ you needed and not specifically GCC then there are other (frankly better) alternatives for Windows development. The primary issue is Dev-C++'s abysmal debugger integration, and lack of GUI development tools. Even if you stick with MinGW/GCC, there are other IDEs with better debug capability - that said they all suck in other ways! ;-)

        Clifford

         
    • Alex

      Alex - 2008-10-30

      I was using the Ubuntu version of Linux, that lacked the necessary libraries, I found them in the end, but to be frankly honest it was a lot more long winded than bloodshed. And I like the bloodshed interface its is much more user friendly :) The virtual machine is an interesting idea, i will bare that in mind next time!

      Thanks again

      Alex

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.