Jump to content

Program exits before finishing calcualtions....

- - - - -

  • Please log in to reply
9 replies to this topic

#1
devilnewt

devilnewt

    Newbie

  • Members
  • Pip
  • 4 posts
Here the code I have so far:

#include <stdio.h>
#include <math.h>

void main()
{
double comprLoad;
double comprStress;
double d,area;
char type[20];
const double PI= 3.14;

printf("Enter material type: ");
scanf("%s",type);
printf("Enter compression load: ");
scanf("%lf",&comprLoad);


comprStress = 25000;
area =comprLoad/comprStress;
d=2*sqrt(area/PI);


printf("\n\nType of the material: %s\n",type);
printf("Compression Load: %.2lf\n",comprLoad);
printf("Allowable stress: %.2lf\n",comprStress);
printf("Diameter: %.2lf",d);


comprStress=15000;
area =comprLoad/comprStress;
d=2*sqrt(area/PI);

printf("\n\nAllowable stress: %.2lf\n",comprStress);
printf("Diameter: %.2lf",d);

comprStress=20000;
area =comprLoad/comprStress;
d=2*sqrt(area/PI);

printf("\n\nAllowable stress: %.2lf\n",comprStress);
printf("Diameter: %.2lf",d);
getchar ();
return;
}

For some reason it exits prior to performing any of the calculations. Not sure why. Any and aall help is greatly appreciated. It will allow me to enter the material type and the compression load but quickly exits after that.

---------- Post added at 10:42 AM ---------- Previous post was at 09:35 AM ----------

What the heck am I doing wrong?

#2
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
I copied your code into a new CodeBlocks console project. It displayed everything as expected:


Enter material type: test
Enter compression load: 20


Type of the material: test
Compression Load: 20.00
Allowable stress: 25000.00
Diameter: 0.03

Allowable stress: 15000.00
Diameter: 0.04

Allowable stress: 20000.00
Diameter: 0.04
Process returned 0 (0x0) execution time : 4.735 s
Press any key to continue.

#3
devilnewt

devilnewt

    Newbie

  • Members
  • Pip
  • 4 posts
Im trying to get this to work in Visio......any idea why it wouldnt work there?

#4
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
Visio? or Visual Studio?

#5
devilnewt

devilnewt

    Newbie

  • Members
  • Pip
  • 4 posts
Oh hell my bad, visual studio.....something doesnt seem right about the program

#6
invokeLater

invokeLater

    Newbie

  • Members
  • PipPip
  • 11 posts
Try either turning precompiled headers off or you can #include <stdafx.h> . I'm just assuming here since lespauled said it ran fine.

#7
DarkLordofthePenguins

DarkLordofthePenguins

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 409 posts
At what point does it exit?

---------- Post added at 02:18 PM ---------- Previous post was at 02:14 PM ----------

invokeLater said:

Try either turning precompiled headers off or you can #include <stdafx.h> . I'm just assuming here since lespauled said it ran fine.

stdafx.h is not a standard header. I've been working with C for years and have become familiar with the C Standard Library, the POSIX API, and to some extent the Windows API, and I have never even heard of stdafx.h. I'm guessing it's a component of some specific framework, and only works for that framework. In any case, you shouldn't use it.
Programming is a journey, not a destination.

#8
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
stdafx.h is a VS thingy, you should include it.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#9
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
If you're using Visual Studio, put a breakpoint on the first line, and hit F10 to go step by step through the program.

But this works in Visual Studio:

#include "stdafx.h"
#include <stdio.h>
#include <math.h>
#include <iostream>

void main()
{
double comprLoad;
double comprStress;
double d,area;
char type[20];
const double PI= 3.14;

printf("Enter material type: ");
scanf("%s",type);
printf("Enter compression load: ");
scanf("%lf",&comprLoad);


comprStress = 25000;
area =comprLoad/comprStress;
d=2*sqrt(area/PI);


printf("\n\nType of the material: %s\n",type);
printf("Compression Load: %.2lf\n",comprLoad);
printf("Allowable stress: %.2lf\n",comprStress);
printf("Diameter: %.2lf",d);


comprStress=15000;
area =comprLoad/comprStress;
d=2*sqrt(area/PI);

printf("\n\nAllowable stress: %.2lf\n",comprStress);
printf("Diameter: %.2lf",d);

comprStress=20000;
area =comprLoad/comprStress;
d=2*sqrt(area/PI);

printf("\n\nAllowable stress: %.2lf\n",comprStress);
printf("Diameter: %.2lf",d);

std::getchar();

return;
}

#10
psepheroth

psepheroth

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
If it exits early, It possibly crashed. Can you enter in debug mode so you can see which line number it crashes?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users