Jump to content

Program not working.

Kosios
Go to solution Solved by madknight3,
17 minutes ago, Kosios said:

This is a C program does in Visual basic 2016

I'm going to assume you mean Visual Studio. Visual Basic is a programming language, Visual Studio is an IDE to write and run code (among other things).

 

Also, there is no Visual Studio 2016. There's a VS 2015 and a VS 2017 currently approaching release. I haven't used 2017 yet but I'll assume it's similar to 2015 when running command line apps. So the below information will probably apply either way.

 

If my assumptions are incorrect, then sorry my mistake, and the below may not apply to you.

 

17 minutes ago, Kosios said:

The program seems to work, however when it displays the average of all 3 numbers it immediately closes on its own.  Is there something wrong?

If you're running the application from Visual Studio, and if running a C command line app is anything like running a .NET one, then the process when you run with a debugger is

  • Open command line window
  • Run application
  • Close command line window if the program finishes

You can stop it from closing in a couple ways

  1. If you're running in debug mode, you can set a breakpoint at the end of your main function
  2. You can also run your code without the debugger. There's a "Start Without Debugging" option. It'll keep the command line open with a "Press any key to continue..." to close it.
  3. You can also get user input at the end of your main method. This will keep the command line open until it receives input.
  4. You can also run the compiled executable directly from the command line

Hey all, so I am working on my first programming assignment for my introductory programming class.  This is a C program does in Visual basic 2016.  The program seems to work, however when it displays the average of all 3 numbers it immediately closes on its own.  Is there something wrong?

 

#include<stdio.h> /*C standard library header file.*/

int main(void) /* Main begin execution*/
{

    int a, b, c;
    int sum;
    double average;

    printf("Please enter three numbers: ");
    scanf_s("%d%d%d", &a, &b, &c);

    printf("\n");

    sum = a + b + c;

    printf("Sum is: %d", sum);

    printf("\n");

    average = (a + b + c) / 3;

    printf("The average of these three numbers is: %.2lf", average);
    return(0);
}

Link to comment
Share on other sites

Link to post
Share on other sites

17 minutes ago, Kosios said:

This is a C program does in Visual basic 2016

I'm going to assume you mean Visual Studio. Visual Basic is a programming language, Visual Studio is an IDE to write and run code (among other things).

 

Also, there is no Visual Studio 2016. There's a VS 2015 and a VS 2017 currently approaching release. I haven't used 2017 yet but I'll assume it's similar to 2015 when running command line apps. So the below information will probably apply either way.

 

If my assumptions are incorrect, then sorry my mistake, and the below may not apply to you.

 

17 minutes ago, Kosios said:

The program seems to work, however when it displays the average of all 3 numbers it immediately closes on its own.  Is there something wrong?

If you're running the application from Visual Studio, and if running a C command line app is anything like running a .NET one, then the process when you run with a debugger is

  • Open command line window
  • Run application
  • Close command line window if the program finishes

You can stop it from closing in a couple ways

  1. If you're running in debug mode, you can set a breakpoint at the end of your main function
  2. You can also run your code without the debugger. There's a "Start Without Debugging" option. It'll keep the command line open with a "Press any key to continue..." to close it.
  3. You can also get user input at the end of your main method. This will keep the command line open until it receives input.
  4. You can also run the compiled executable directly from the command line
Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Kosios said:

The program seems to work...

But has a problem...

int a, b, c;
double average;

average = (a + b + c) / 3; //<== will not result in what you think it will...

Since a, b and c are all of type int, the expression "a + b + c" will also be of type int.

3 is also a int, so the expression becomes "int / int".
That means the result will be a integer, rounded down.

 

So a = 2, b = 4, c = 5 will result in average being 3, not 3.6666...

Fix is simple:

int a, b, c;
double average;

average = a + b + c; //store sum in average first
average /= 3; //shorthand for average = average / 3

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×