I have been used to programming in the DOS Box software, and yesterday I got a book titled 'Absolute Beginner's Guide for C++' by Greg Perry. So in the third chapter, it has a code, which I copied along, since the codes I was typing were familiar with that of the DOS software, and not of the C++ 11.
/
/* Filename Chap3 A.c*/
/* Totals how much money will be spent on holiday gifts. */
#include<stdio.h>
main()
{ float gift1, gift2, gift3, gift4, gift5, total; /*initialization*/
printf("How much do you want to spend on your mom?");
scanf ("%f", gift1);
printf("\nHow much do you want to spend on your dad?");
scanf("%f", gift2);
printf("\nHow much do you want to spend on your brother?");
scanf("%f", gift3);
printf("\nHow much do you want to spend on your sister?");
scanf("%f", gift4);
printf("\nHow much do you want to spend on yourself?");
scanf("%f", gift5);
total=gift1+gift2+gift3+gift4+gift5;
printf("\nThe total ammount you will be spending on your gifts is $%.2f", total);
return 0;
}
Now when I compile, it shows no errors. As I run it, the first question appears, which goes like, "How much do you want to spend on your mom?"
As I type in something, it displays a pop up with the message 'filename.exe has stopped working.' Could someone help me on where I am going wrong?
That's the problem with scanf. One tiny error can crash your program. scanf expects an pointer to store the input so you need to supply the address of the variable - like so scanf ("%f", &gift1);
/* Totals how much money will be spent on holiday gifts. */
#include <stdio.h>
int main()
{
float gift1, gift2, gift3, gift4, gift5, total; /*initialization*/
printf("How much do you want to spend on your mom?");
scanf ("%f", &gift1);
printf("\nHow much do you want to spend on your dad?");
scanf("%f", &gift2);
printf("\nHow much do you want to spend on your brother?");
scanf("%f", &gift3);
printf("\nHow much do you want to spend on your sister?");
scanf("%f", &gift4);
printf("\nHow much do you want to spend on yourself?");
scanf("%f", &gift5);
total = gift1 + gift2 + gift3 + gift4 + gift5;
printf("\nThe total ammount you will be spending on your gifts is $%.2f", total);
return 0;
}
// Totals how much money will be spent on holiday gifts.
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
float gift1, gift2, gift3, gift4, gift5, total; // initialization
cout << "How much do you want to spend on your mom?";
cin >> gift1;
cout << "\nHow much do you want to spend on your dad?";
cin >> gift2;
cout << "\nHow much do you want to spend on your brother?";
cin >> gift3;
cout << "\nHow much do you want to spend on your sister?";
cin >> gift4;
cout << "\nHow much do you want to spend on yourself?";
cin >> gift5;
total = gift1 + gift2 + gift3 + gift4 + gift5;
cout << fixed << setprecision(2);
cout << "The total ammount you will be spending on your gifts is $" << total;
return 0;
}
Notice the usage of scanf() to take in an integer. It is passed by reference (i.e., using the & operator).
I usually encounter that error message when I neglect to allocate memory for variables for functions that read input from stdin into those variables.
Not relevant but worth mentioning:
float gift1, gift2, gift3, gift4, gift5, total; /*initialization*/
This is a declaration, not an initialization since each float is a local variable and has not been assigned an initial value. Being a local variable (a.k.a. automatic variable in C), they contain garbage. It's good practice to initialize it to some appropriate value (in your case, 0).
Also, if you're going to run this in windows console, use gets() or scanf() to prevent the console from prematurely closing so that the terminal window remains open after the final printf() statement.
Lastly, please use the code,/code tags to format your code.
/* Filename Chap3 A.c*/
/* Totals how much money will be spent on holiday gifts. */
#include<stdio.h>
int main()
{
float gift1=0, gift2=0, gift3=0, gift4=0, gift5=0, total=0; /*initialization*/
printf("How much do you want to spend on your mom?");
scanf ("%f", &gift1);
printf("\nHow much do you want to spend on your dad?");
scanf("%f", &gift2);
printf("\nHow much do you want to spend on your brother?");
scanf("%f", &gift3);
printf("\nHow much do you want to spend on your sister?");
scanf("%f", &gift4);
printf("\nHow much do you want to spend on yourself?");
scanf("%f", &gift5);
total=gift1+gift2+gift3+gift4+gift5;
printf("\nThe total ammount you will be spending on your gifts is $%.2f", total);
fflush(stdin); //Flush the stream buffer so gets() doesn't just take garbage from
//stream and prematurely terminate.
//Pause console: Use this over system("pause");
char a[10];
gets(a);
return 0;
}