Issue with some simple logic and getting something to print C++ HouseWindowsLab

how to print in c
how to print backslash in c
printf function in c source code
printf and scanf in c
how to print double quotes in c
printf in c definition
printf java
printf right justify c

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 */

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.

One Issue with some simple logic and getting something to print C++ , One Issue with some simple logic and getting something to print C++ HouseWindowsLab? One Issue with some simple logic and getting  Most college students feel stiff struggle learning programming logic in college days. Below is list 101 C Programs, which will help you build basic concepts of control structures, conditional statements and so on. Programs are written for basic to advance logic building. These programs have been personally written by me 8-9 years back in my

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

[PDF] Secrets of “printf”, printf is the C language function to do format- ted printing. this problem. Some require the special character to be entered twice. C As I mentioned above, simply printing numbers was not enough. To make things line up nice and pretty, it is common to write a date Using the format specifier %+05d or %0+5d we get. Any thing printed in console starts from top left and flows to bottom right, it will fill top row first until a new line is encountered or row is completely filled, then it will proceed with next row and continue in that fashion.

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.

Return values of printf() and scanf() in C/C++, What values do the printf() and scanf() functions return ? printf() : It returns total number of Characters Printed, Or negative value if an output error or an encoding​  10 Logic Puzzles You Won't Be Able To Solve These logic puzzles will ruin your weekend, distract you from your loved ones, and make you realize you aren't as smart as you think you are. Sorry.

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.

How to print % using printf()?, Here is the standard prototype of printf function in C. int printf(const char *format, .​..); The format string is composed of zero or more directives: ordinary characters (​  Logic gates have one or more input terminals, and the voltage at these terminals are translated into Boolean inputs of 1 or 0. Typically a high voltage is read as a 1 and a low voltage as a 0. These inputs can come from anything, be it a battery, a sensor, some IC or even another logic gate.

Your Guide to the Python print() Function – Real Python, The simplest example of using Python print() requires just a few keystrokes: >>> C++, std::cout << "hello world" << std::endl; For simple objects without any logic, whose purpose is to carry data, you'll typically take When you go and get things out of the refrigerator for yourself, you can cause problems. Some users have reported issues with activating their copies of Windows 10, which could have been down to a number of different reasons. In some cases, the easiest way to get around the problem is

print function, logical, indicating whether or not strings should be printed with surrounding quotes. Use methods("print") to get all the methods for the print generic. print.​factor allows some customization and is used for printing ordered factors as well. For a simple prototypical print method, see .print.via.format in package tools. Even though Microsoft is making the upgrade to Windows 10 an easy process, many users are experiencing a number of issues. Today we look at some of these issues and what you can do to fix them!

10+ Tips for Troubleshooting Common Printer Problems, Few things lead to frustration quicker than when you need to print through your most troubling printer issues and we've got some basic tips covered right here! Unplug your printer to reset the sensors if you are still getting a paper Explorer browser to search: C:\Windows\System32\Spool\PRINTERS  Additionally, some scammers may try to identify themselves as a Microsoft MVP. You can help protect yourself from scammers by verifying that the contact is a Microsoft Agent or Microsoft Employee and that the phone number is an official Microsoft global customer service number .

Comments
  • Take another look at the format specifier string for spare windows.
  • Welcome to Stack Overflow. Please read the About and How to Ask pages soon. Please do not tag a question with both c and c++ — they are vastly different languages. Your title mentions C++; your code is C. Be cautious about using fflush(stdin) — it is not good practice on most platforms because it leads to undefined behaviour.
  • 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 >_>
  • 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.'
  • 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.
  • 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!
  • 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.
  • 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.
  • Exactly why I decided to use integer on this one.
  • I think that would be very helpful, however I can't even get prinf("The total amount of windows... show up when I run it.
  • Yes the problem was with your &d on printf.
  • Yeah I removed all the fflush and cleaned it tf up. If its a floating point window wouldn't you have a weird percentage of a window instead of just one?
  • @MatthewNickles: How weird ?