Crashing Visual Studio

I'm writing a program, however, a calculation I put in a function just won't work. I took it out and ran it be it's but it still crashes. I've tried changing the variable type and names, but it keeps crashing. I'm new to programming so I'm not sure what I'm doing wrong. It seems like a simple calculation, but I'm still messing it up. I've done research and perhaps I need to use fmod. Any ideas?
1
2
3
4
5
6
7
8
9
10
11
  double savAcct;
	float t = 0;
	double balance;

	printf("Transfer funds from savings to checking.\n");
	printf("\nEnter the current Savings Account Balance: \n");
	scanf_s("%lf", &savAcct);
	printf("\nEnter the amount to be transferred: \n");
	scanf_s("%lf", t);
	savAcct = savAcct - t;
	printf("\n%lefThe new savings account balance is: \n", savAcct);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
double savAcct;
float t = 0;
// double balance;

printf( "Transfer funds from savings to checking.\n" );
printf( "\nEnter the current Savings Account Balance: \n" );

scanf( "%lf", &savAcct ); // scanf double, ergo %lf

printf( "\nEnter the amount to be transferred: \n" );
// scanf_s( "%lf", t );
scanf( "%f", &t ); // scanf float, ergo %f, and pass the address

savAcct = savAcct - t;
//::printf( "\n%lefThe new savings account balance is: \n", savAcct );
printf( "\nThe new savings account balance is: %.2f\n", savAcct );
// note: unlike scanf, for printf double, the format specifier is %f

// tip: programmers who use the C++ library don't get into this kind of mess 
I just forgot the '&' when I scanned t. Which it didn't register and causing it to crash. Works fine now. Just didn't realize it was something so simple until I was comparing mine to yours. So thanks for that.
Compile with warnings enabled and pay attention to the warnings that are generated.

With -W4 (to set it from the IDE, Menu Project=>Properties=>C/C++=>General=>Warning Level : Level4)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>

int main()
{
    double savAcct;
    float t = 0;
    
    double balance; // *** warning C4101: 'balance': unreferenced local variable

    printf( "Transfer funds from savings to checking.\n" );
    printf( "\nEnter the current Savings Account Balance: \n" );
   
    scanf_s( "%lf", &savAcct );
    
    
    printf( "\nEnter the amount to be transferred: \n" );

    scanf_s( "%lf", t ); 
    // *** warning C4477: 'scanf_s' : format string '%lf' requires an argument of type 'double *', but variadic argument 1 has type 'double'
    // *** warning C6066: Non-pointer passed as _Param_(2) when pointer is required in call to 'scanf_s' Actual type: 'float'.         
    
    
    savAcct = savAcct - t;
    printf( "\n%lefThe new savings account balance is: \n", savAcct );
}

http://rextester.com/SKXHL37310
Topic archived. No new replies allowed.