1

Please Help. This simple program is suppose to calculate users inputs for how many of each house they want. Multiply it by the windows each house adds. And apply 1% more windows to the total for spares. Why isn't the spare part working?

/*************************************
* PROGRAM: WindowCalculator
* AUTHOR: Matthew Nickles
* DATE: 9/6/2018
* NOTES: This is for educational purposes, the program calculates
* how many windows for each house plan a city builder would get.
**************************************/

/* PREPROCESSOR COMMANDS */
#include <stdio.h>


/* MAIN PROCESSING CONTROL */
int main()
{
/* VARIABLE DECLARATIONS */
int MontgomeryHouses; /*20 Windows per a house*/
int KetteringHouses; /*15 Windows per a house*/
int SaxonHouses; /*12 Windows per a house*/
int TotalWindows; /*Total calculation of all windows*/
int SpareWindows; /*How many spare windows if you wanted 1% of total*/ 

/* WINDOWS PER HOUSE ALGORITHM */
printf("\n How many Montgomery Houses do you wish to build? They have 20             windows. ");
scanf("\n%d", &MontgomeryHouses);
fflush(stdin);

printf("\n How many Kettering Houses do you wish to build? They have 15 windows. ");
scanf("\n%d", &KetteringHouses);
fflush(stdin);

printf("\n How many Saxon Houses do you wish to build? They have 12 windows. ");
scanf("\n%d", &SaxonHouses);
fflush(stdin);

/* CALCULATE TOTAL WINDOWS*/
TotalWindows = (MontgomeryHouses * 20) + (KetteringHouses * 15)
        + (SaxonHouses * 12);
SpareWindows = TotalWindows * 0.01;
/* DISPLAY OUTPUT*/
printf("The spare windows needed &d are windows.\n",SpareWindows);
fflush(stdin);  
printf("\nThe total amount of windows needed for all houses are %d windows.\n",TotalWindows);
fflush(stdin);



return 0;
}
/* END OF PROGRAM */
Adil Saju
  • 771
  • 2
  • 7
  • 22
  • 4
    Take another look at the format specifier string for spare windows. – dgnuff Sep 07 '18 at 04:51
  • 2
    Welcome to Stack Overflow. Please read the [About] and [Ask] pages soon. Please do not tag a question with both [tag:c] and [tag:c++] — they are vastly different languages. Your title mentions C++; your code is C. Be cautious about [using `fflush(stdin)`](https://stackoverflow.com/questions/2979209/) — it is not good practice on most platforms because it leads to undefined behaviour. – Jonathan Leffler Sep 07 '18 at 04:52

4 Answers4

5

Your logic is totally fine. It is just a tiny syntax error. In this line:

printf("The spare windows needed &d are windows.\n",SpareWindows);

&d should be replaced by %d.

I think you just overlooked it, because the other parts are perfect.

Harshith Rai
  • 2,531
  • 6
  • 18
  • 31
  • Tried running it, here is what happened when I ran it. --- `How many Montgomery Houses do you wish to build? 1 How many Kettering Houses do you wish to build? 1 How many Saxon Houses do you wish to build? 1 The total amount of windows for all houses are 47 windows. RUN SUCCESSFUL (total time: 1s)` It didn't even acknowledge one of the print statements lol. Also im new so sorry about the formating >_> – Matthew Nickles Sep 07 '18 at 05:07
  • No problems about the formatting buddy. But are you sure you didn't comment out the `printf` or something? Because this is what I got as the output: `gcc version 4.6.3 How many Montgomery Houses do you wish to build? They have 20 windows. 1 How many Kettering Houses do you wish to build? They have 15 windows. 1 How many Saxon Houses do you wish to build? They have 12 windows. 1 The spare windows needed 0 are windows. The total amount of windows needed for all houses are 47 windows.' – Harshith Rai Sep 07 '18 at 05:13
  • and yes another thing, try working without the use of `fflush(stdin)`. It isn't a must for smaller programs. It also does cause problems at times. – Harshith Rai Sep 07 '18 at 05:17
  • 1
    I copied and pasted my source code, changed one thing and it worked again... it was that &d I have been working on that far too long. Completely new to programing, slowly but surely I work my way. Its actually getting quite fun. Thanks you for the help! – Matthew Nickles Sep 07 '18 at 05:25
  • You're welcome. And could you tell me what caused that particular `printf` to not display even after making the change to the `&d`? Just curious to know. – Harshith Rai Sep 07 '18 at 05:29
  • @Rai: use of `fflush(stdin)` must be avoided. It is an undefined behavior to use `fflush()` function for input stream`(stdin)`. This library function should only be used for output stream `(stdout)` – Destructor Sep 07 '18 at 05:32
  • I actually can't. There were no error messages, or anything showing when I ran it. It drove me mad. Also it wasn't a comment and no syntax error (well has to be but none I found) as well. Maybe it was the IDE? It might come clear to me on the morrow. – Matthew Nickles Sep 07 '18 at 05:34
  • Yes @Destructor. So that is what caused his `printf` to not display then? I myself have witnessed some unusual behavior while using the `fflush`, didn't know this though. Thanks. – Harshith Rai Sep 07 '18 at 05:34
  • Yes Matthew, maybe it was the `fflush` as Destructor said. – Harshith Rai Sep 07 '18 at 05:35
  • @Rai: See my answer. I've provided links to gain more information about this issue. OP has used '\n' in `scanf()` which isn't necessary here and type of `SpareWindows` variable should be `float` instead of `int` – Destructor Sep 07 '18 at 05:36
  • @MatthewNickles: Which compiler you are using ? – Destructor Sep 07 '18 at 05:40
  • Yes @Destructor. But I feel he is right to use the integer type for the spare windows, because logically, one can't really think of buying a fraction(float type) of windows. Not from a coding POV, but just a practical thought. You do have a point though, regarding floats and percentages. – Harshith Rai Sep 07 '18 at 05:40
0

Your Problem is that ints cannot have numbers after a point. E.g: SpareWindows = 50.43, but because you declared it as int, it get truncated to 50. You might want to use floats instead, like:

/* VARIABLE DECLARATIONS */
int MontgomeryHouses; /*20 Windows per a house*/
int KetteringHouses; /*15 Windows per a house*/
int SaxonHouses; /*12 Windows per a house*/
int TotalWindows; /*Total calculation of all windows*/
float SpareWindows; /*How many spare windows if you wanted 1% of total*/ 

Since you total number of windows can always be just a "full" number, its perfectly fine to declare them as ints. But when moving to a percentage, like SpareWindows, you have to use floats. You might want to take a look here

Doctor
  • 59
  • 11
  • A very valid point syntactically Doctor. But practically, wouldn't be I guess. Not from a coding point of view, but you can't really have a fraction of windows, can you? Like, 0.53 spare windows for a total of 53 windows. So, logically, I feel he is better off using the Integers. Hope you don't take any offence, just sharing my opinion. – Harshith Rai Sep 07 '18 at 05:04
  • Exactly why I decided to use integer on this one. – Matthew Nickles Sep 07 '18 at 05:26
0

Try this code:

#include <stdio.h>


/* MAIN PROCESSING CONTROL */
int main()
{
/* VARIABLE DECLARATIONS */
int MontgomeryHouses; /*20 Windows per a house*/
int KetteringHouses; /*15 Windows per a house*/
int SaxonHouses; /*12 Windows per a house*/
int TotalWindows; /*Total calculation of all windows*/
float SpareWindows; /*How many spare windows if you wanted 1% of total*/ 

/* WINDOWS PER HOUSE ALGORITHM */
printf("\n How many Montgomery Houses do you wish to build? They have 20             windows. ");
scanf("\n%d", &MontgomeryHouses);
fflush(stdin);

printf("\n How many Kettering Houses do you wish to build? They have 15 windows. ");
scanf("\n%d", &KetteringHouses);
fflush(stdin);

printf("\n How many Saxon Houses do you wish to build? They have 12 windows. ");
scanf("\n%d", &SaxonHouses);
fflush(stdin);

/* CALCULATE TOTAL WINDOWS*/
TotalWindows = (MontgomeryHouses * 20) + (KetteringHouses * 15)
        + (SaxonHouses * 12);
SpareWindows = (float)TotalWindows * 0.01; //Used Explicit type conversion to float
/* DISPLAY OUTPUT*/
printf("The spare windows needed %f are windows.\n",SpareWindows);
fflush(stdin);  
printf("\nThe total amount of windows needed for all houses are %d windows.\n",TotalWindows);
fflush(stdin);



return 0;
}
/* END OF PROGRAM */

Here actually the problem is when you try to multiply an integer by a decimal, output get converted to integer, so we must explicitly specify that value should be a float. Hope that worked.

Adil Saju
  • 771
  • 2
  • 7
  • 22
0

There are multiple issues in your program.

Let me start to point out them one by one.

Consider: scanf("\n%d", &MontgomeryHouses);

Why you've used '\n' in the scanf() ? There is no need to use '\n' here when you are taking an input.

I see that you've used fflush(stdin); 5 times in your program. Stop using fflush(stdin) in your C programs. fflush() function should only be used for output stream. Read Standard streams to know more information about this. Use of fflush(stdin) is an Undefined behavior in both C & C++. Read the answers of these questions What does fflush(stdin) do in C programing? & Using fflush(stdin) for the more information.

Another issue is that you've written SpareWindows = TotalWindows * 0.01;. You want to use floating point value in SpareWindows variable but the data type of this variable is defined to be of type int. It should be defined as float SpareWindows; So you'll have to use '%f' format specifier to print the value of this variable.

Destructor
  • 13,235
  • 8
  • 48
  • 108