Q1. | Why does the output window disappear before I can see it? |
A1. |
Depending on the development system, the output window disappears when
the program is finished running. There are two solutions: |
Q1. | How do I get Visual Studio to build a C-language program instead of C++? |
A1. |
The precompiled headers option must be turned off and the file extension must be .c The 'precompile headers' option can be turned off either when the project is first being created or it can be turned off after the project is has already been created. The steps are different.
|
Q2. | You receive the following error when trying to build a C-language program on Microsoft Visual Studio: Precompiled header file is from a previous version of the compiler, or the precompiled header is C++ and you are using it from C |
A2. |
Turn off the precompiled headers for your project Select Not Using Precompiled Headers. Click OK
|
Q1. | FAQ - My compiler gives an error message concerning scanf_s |
A1. |
In the beginning The C-language used scanf, but many C compilers consider the original version of scanf to be 'deprecated' or old-fashioned and have replaced scanf with scanf_s. The _s indicates this is a secure version of scanf. Some compilers still use scanf. If this is a case with your compiler, just replace scanf_s with scanf in the Rock-Paper-Scissors program. Why use scanf_s? Buffer overrun can happen when using the original scanf. For example, if the programmer wants the user to input a name and creates an array that can hold 10 characters to receive the name, and the user inputs 20 characters, scanf would fill the array with the first 10 characters and then keep putting the rest of the characters on top of other data or program code and wipe out those memory locations. The scanf_s version requires the programmer to provide one more parameter when inputting a character string, the size of the array (buffer) that will be receiving input data. The scanf_s routine limits the number of characters input. This extra parameter is not needed when inputting a single character. In this case scanf and scanf_s are written with the same set of parameters. Parameters are the things inside the parentheses ( ). |
Q1. | Why does a C++ program report an error when typing end1? |
A1. |
The cout statement in C++ uses endl to end a line of text and move the cursor down to the next line. The word endl stands for End-Line. A common mistake for programmers new to the C++ language is to type end1 with the number-1 instead of endl, with the small-L. Compare the difference in how the small-L and number-1 look with different fonts. Depending on the font you are looking at, the small-L and the number-1 look very similar. Actually, many of the older typewriters did not even have keys for the number-1 or number-0. Typists were instructed to use the small-L and capital-O instead. In the days of computers, the small-L and number-1 as well as the capital-O and number-0 are very different characters. |
Q1. | How do I fix a COFF Error in Microsoft Visual Studio? |
A1. |
|
Q1. | FAQ – How do I download Xcode for macOS? |
A1. |
NOTE: As of Feb 2020, the latest version of Xcode requires macOS 10.14.4 or later 1) Use Safari to go to https://developer.apple.com/xcode/ |
Q1. | How do I build a C++ program using Xcode? |
A1. |
Do not select 'Playground'. |
Q1. | How do I build a C program using Xcode? |
A1. |
Do not select 'Playground'. |
Q1. | Why does the program ignore inputs when using scanf with %c? |
A1. |
Whitespace includes the space, tab, Enter and Return characters. Normally, scanf ignores leading whitespace characters, inputs the requested data and then stops when encountering a whitespace character leaving the whitespace character still in the input buffer. When reading a single character using scanf or scanf_s, place a space before the % character in "% c". However, if scanf(" %c", ... does not ignore leading whitespace characters. If it follows a previous call to scanf, scanf will receive the whitespace character left over from the previous scanf and does not input the desired character. Solve this problem by calling flushall( ); before using scanf("
%c", ... |
Q1. | Why is so hard to input a single character from the keyboard |
A1. |
Inputting a single character can be rather a problem with the more modern operating systems. They like to buffer the input and hold it until the Enter key is pressed. Different pieces of software have different ways of dealing with this when trying to input a single character. getch and getchar worked great with the old MS/DOS because our programs could bypass the operating system and work directly with the hardware. This is no longer the case. The operating systems place a barrier between application programs and the hardware. Solution in C++
cin will ignore multiple space or tab keys and accept the next non-whitespace character and place it into player1, ignore multiple whitespace characters and then place the next character in player2. The cin routine only finishes executing when the Enter key is pressed AFTER a valid non-whitespace character is processed. Solution in C
We will read the desired character for player1, but when we try to read player2 both getchar or scanf will place the code for the Enter key in player2 and not even bother to read the character we were hoping for into player2. What we need to do is 'flush' the whitespace characters from the operating system's input buffer before using either getchar or scanf. this can be accomplished as:
The " %c" in scanf has the space character before the %c to indicate to scanf to accept and ignore any leading whitespace characters. If you write a C program in a project you created for C++ using either
MSVC, Xcode, cpp.sh, etc. you will may also need |
Q1. | Are there extra credit points available? |
A1. |
Extra credit points count towards the number of points earned, but are not included in the total number of points for the class. For example, if the class has a total of 100 points for all assignments, quizzes and tests and you earned 85 points + 10 points for extra credit, your grade percentage would be (85+10)/100 = 95%. If you did not have the extra credit, your percentage would be (85)/100 = 85%. Extra credit is available in two ways: |
Q1. | How do I loop until a non-negative integer is input with cin? |
A1. |
A return statement inside of int main will cause the program to
end. The value after the return (0 or 1) is only useful if the program
is part of a chain of programs that are run one after another. A return
value of 0 typically indicates that the program did not have a problem
and other values are used to indicate an error or some other non-standard
completion. If you want to have the program ask again if a negative number is entered,
change the code to: So far, the code works if you enter a negative number, but still fails
if you enter something illegal. Because the cin is expecting to input
into an integer, anything that is left in the keyboard input buffer is
left there and the next time in the loop that cin is called, the
bad value is still there without you even getting a chance to type anything
new. Here is the code updated to clear the input if it was illegal. |
Q1. | How do I reject non-numeric or negative numbers with cin? |
A1. |
|
Q1. | Why are structured records used to store the abbreviation and full name of each state? |
A1. |
A structured record can collect one or more data types into a single
unit. Some languages refer to this as a Record, other languages
such as C and C++ refer to it by the keyword struct. For example
a record for a student might have several fields such as name, IDnumber,
address, phone, e-mail, unitsTaken, GPA, etc. Since the record contains
several data items grouped together, it is possible to pass the record
from one part of the program to another part, or even have an array of
many records, each having the same fields. An example definition for a
student record might be:
The process could also be done in two steps. First, create the structured
record, then create a new data type based on the structured record:
|
Q1. | Why are there different functions that do string compare for C-strings |
A1. | The original version of the C-language had functions that could compare
the contents of two strings. As the C and C++ languages evolved,
it was determined that run-time errors could be caused when users input
strings that were much longer than the program was expecting. Many of the
compilers have deprecated (too old fashioned to be used) the strcmp
function and have upgraded and renamed it to _strcmp. Some compilers
upgraded strcmp but left its name to still be strcmp. Some
compilers also upgraded the stricmp function and renamed it _stricmp
while other compilers totally renamed it to strcasecmp.
strcmp compares two strings and returns a negative value if the strings do not match and string1 has a lower value than string2. A positive value is returned if string1 has a greater value than string 2. A zero is retuned if both strings are equal. The stricmp function compares strings similar to strcmp but the case of the characters in the two strings are ignored. For example. big-A is treated the same as little-a. The way these routines work is by comparing the strings character by character and subtracting the numeric value of a character in string1 from the numeric value character in string2. If there is a mismatch along the way, the routine returns a positive or negative number based on the difference of the first mismatch of the two strings. For example, 'C'-'F' results in -3, but 'F'-'C' is a +3. If they are the same, the result is zero and the function moves to the next character. If all of the characters match then the function returns a zero. In this way, these routines can be used to determine if the strings are equal or one of the strings has a higher or lower value. For example, these routines can be used when strings need to be sorted. C++ now has a string data type. C++ strings can be compared using the >, the < and the == operators. |
Q1. | Why is gets( ) deprecated? |
A1. | The gets( ) function is being deprecated by several compilers because it does not have a limit on the number of characters that can be input from the keyboard which can create problems if the user enters more characters that the size of the receiving array could hold. It is best to use fgets which was originally created to get data from a file. Since the keyboard is being treated as a file named stdin by the operating system, fgets (buff, Buff_Size, stdin); is better and works in all cases instead of gets (buff, Buff_Size); |
Q1. | C++ What is ----using namespace std; |
A1. | Originally, C++ did not use namespaces. As the language grew, it
started being used for very large projects with several hundreds of programmers
contributing to a single program. Programmers in completely different departments,
sometimes in different buildings, started using completely unrelated variables
and constants that happened to have the same names that were used by other
people they didn't even know or work with. The idea of a namespace
was created. Everything one group did was placed in one namespace, everything
a different group did was placed in their own namespace.
The standard C++ functions are placed in the standard namespace, called
std. Actually, you can get by without using namespace std; Some textbooks start having students use the std::cout
in the first few chapters. It is just easier to place using
namespace std; at the top of the program. |
Q1. | Why change the sign on the numerator and denominator if the denominator is negative? |
A1. |
It just looks better when displaying a rational number with a positive
denominator |
Q2. | Can the remainder be negative when dividing two integers? |
A2. | Yes. The remainder when dividing two numbers can be negative. For example
-5 % 3 gives a remainder of -2 |
Q3. | How is the equality operator == supposed to work? |
A3. | The return data type for the equality operator == should be bool (true or false). If the numerators of both numbers are the same, AND the denominators of both numbers are the same, then the two numbers are equal, so the == operator should return a true. Otherwise, return false. |
Q1. | How should the Movie Ratings assignment work? | |||||||||||
A1. |
Without giving away all of the code for the Movie Ratings project, below
is a sample of the main program.
With the above code, the output should be:
|
Q1. | How do I implement error handling in C when using scanf in the Electric Bill? |
A1. |
The Electric Bill assignment states that exception handling is to be used to reject non-numeric and negative inputs. scanf attempts to read the input made of characters, convert and
store them in the variables passed to scanf. For example See below to implement the solution. double
kWh; /*
check for negative inputs */ |
Q1. | How do I implement Exception Handling in C++ (Try...Throw...Catch) in the Electric Bill? |
A1. |
The Electric Bill assignment states that exception handling is to be used to reject non-numeric and negative inputs. Use Try...Throw...Catch blocks as shown below to implement the solution. double
kWh; |
Q1. | Can a constructor return an error? |
A1. |
A constructor has no return value, but if an error is detected, use the
throw command so that the error can be processed by the main program.
For sample code see: |
Q1. | How do you select a file named Balances on the Desktop? |
A1. |
To open a file, you must specify the full pathname and filename. This
is called the fully-qualified filename. For example, when in C or C++,
to open a file named Balances.txt that is located on the desktop, the
fully-qualified filename on my system would be: The pathname consists of your HOME directory and /Desktop which is a
folder on the operating system that Mac - To display the location of the HOME directory on a Mac running
OS/X Windows - To display the location of the HOME directory on a PC
running Windows SPECIAL NOTE FOR PROGRAMMERS ON WINDOWS SYSTEMS C and C++ use the backslash \ in character strings as an escape to give
a new meaning to the next character. For example, \n is new line, \b is
backspace, \t is tab, etc. It is necessary to type two backslashes \\
inside of a quoted string to get one backslash \ in memory. For example
to get this: Since web servers were originally developed on Unix systems, the forward
slash / is used in web addresses. To make Windows compatible with the
rest of the world, Windows now accepts forward slashes / as well as backslashes
\ as part of a filename. In C or C++ you can now use |
Q1. | What is XAML or Microsoft 'Blend'? |
A1. |
If you see an error message that contains XAML when trying to write a C++ program using Microsoft Visual Studio, it appears that you are running "Microsoft Blend for Visual Studio" instead of Visual Studio itself. I suggest that you get out of this project and reopen Visual Studio. Make sure that you do not select the "Blend" option. When you get back into Visual Studio, Select New Project, then Visual C++, then Win32 Console Application. "Extensible Application Markup Language, or XAML (pronounced "zammel"), is an XML-based markup language developed by Microsoft. XAML is the language behind the visual presentation of an application that you develop in Microsoft Expression Blend, just as HTML is the language behind the visual presentation of a Web page. Creating an application in Expression Blend means writing XAML code, either by hand or visually by working in the Design view of Expression Blend." (the quoted text is from the Microsoft Developer Network MSDN) For more on XAML and Microsoft Expression Blend, see |
Q1. | What are argc and argv used for on the int main line? |
A1. |
Console programs like we are creating do not exist in a vacuum. They are launched by some other program. The other program can pass data to our application program, and can also receive an integer status code back. There is a copy file command in the terminal mode for Windows named COPY.
In macOS it is called CP. If our program is not expecting to obtain any parameters when it executes,
you can use Some versions of the Microsoft C/C++ compiler like to be special and uses _tmain and _TCHAR* which it then redefines to main and char * in case they want to change things for themselves in the future. |
Q1. | What is return 0; used for in a the Payckeck program? |
A1. |
Our application program can also return an integer indicating the status of the program using the return statement. It is commin to use return 0; to indicate that the program was successful. Or another value to indicate an error such as the program was not able to successfully open a file. The value returned can be different for each application program and needs to be documented in writing if another programmer might be using this return value. C and C++ programs do not necessarily need to have a return statement followed by an integer. Change int main(. . .) to void main(. . .) to indicate that a return code will not be used. You code can still have a return; statement that ends the program, but can't pass a return code such as return 0; |
Q1. | What is fgets used for in the C-language Paycheck program? |
A1. |
The C version of the program is using fgets which stands for 'get a string of text characters from a file'. The syntax of fgets is fgets(char[ ], int count, File infile) count is an integer that identifies the size of the character array.
In the Paycheck program, the character variable name is declared as char
name[100]; to hold 100 characters. The literal value 100 could have been
placed in fgets as: I chose to use fgets (name, sizeof(name), stdin); I am using scanf in the Paycheck program to input hours and payRate. scanf works great here because it starts reading data from the keyboard and stops when it sees whitespace (space, tab, new-line, return-key, etc.) I am using fgets instead of scanf because there might be a space entered while typing the name. For example: "Dan McElroy". If scanf were used, then the word "Dan" would be placed in the name variable, but when scanf tried to read the hours worked into the hours variable, it would read "McElroy" which is not a collection of digits 0-9 and decimal point. The program would crash. When Unix was being created, Doug McIlroy (no relation) wanted a common interface for all devices. Accessing files on a disk drive was the most complex, so he decided that all input/output devices would have an interface similar to disk files. The stdin is the name of the standard-input device which by default is the console keyboard. |
Q1. | How do you convert the row and seat selection to an array index? |
A1. |
The row and seat selection has two parts, the numeric row selection (1
to maxRow) and the alpha seat selection (A to Z). The program can input
the row number either as a character into a char data type, or as an integer
into an int data type. The seat selection must be input into a character
data type. Arrays start their index at 0 and must be integers. Therefore row and
seat values input from the keyboard need to be converted to integers,
starting at 0. However, if the row was input into a character data type, then subtract a character literal '1'. Refer to the ASCII chart at http://program-info.net/C++/ascii.htm and you will see that a character '1' has a decimal value of 49. Therefore, if the user typed a '5' and the program read it as a character, the row index would be converted to an integer 4 for the array ('5' - '1' ===> 4). When looking at the character code decimal values for '5' and '1', (53 - 49 ===> 4). A similar procedure happens with the seat. But since it must be input as a character, we just need to convert it to an integer value for the array index, starting at 0. The seats start a 'A' which needs to be converted to starting from 0 for the array index. Therefore, just subtract the character literal 'A'. For example, if 'D' was input for the seat, then 'D' - 'A' would be 3, or looking at the ASCII table for the decimal values of 'D' and 'A' (68 - 65 ===> 3). |
Q1. | How do you identify a seat being taken using '-'? |
A1. |
Assuming that you have a two dimensional array named ArrayOfSeats, you can use the code shown below. You still need to verify that a valid row and seat were entered by the user.
cout << "Enter
a seat selection (example 5F): "; //
Verify that a valid row and seat were entered |
Q1. | Do you have any hints on finding the mean and median of a disk file? |
A1. |
while (!infile.eof() && recordsToSkip--)
// <===== while not end of file, count down to find the median |
Q1. | How do I start implementing the standard deductions? |
A1. |
Let's start with selecting the choice between itemized and standard. cin >> choice; // where choice is of type char if (choice == 'I') // compare to the character capital-I, not the variable I You need to compare to a 'I', not a variable named I. It would be even better to accept both big and small i if (choice == 'I' || choice == 'i') // compare to the character capital or small I For standard deductions, we need to look up a value in the array, based on the tax status, which is passed into the function as int s. Because an array index (position) starts at 0, we do need to subtract 1 from the taxpayer status in order to select the correct location in the array. NOTE: we need to subtract 1, not the code for the printable character '1', which would actually subtract 49. Refer to the numeric values of character codes at http://program-info.net/C++/ascii.htm Here is the code that would get the correct value for standard deductions. |
Q1. | Hints for the Roll Dice Histogram Using In? |
A1. |
Two functions are needed when using the random number generator in C or C++. The rand( ) function produces random numbers based on its own internal formula. The srand() function is only used one time, typically at the beginning of the program. It 'seeds' the random number generator so that it does not produce the exact same sequence of numbers every time the program is run. Start the program with int main, then call the srand( ) function. To make sure it is only called one time, do not put the srand( ) function in a loop. The code for the roll( ) function also shows another way to seed the random number generator, and only do it one time.
You can either use a collection of separate counters or an array to keep track of each time a pair of numbers occurs when rolling the dice. A seprate FAQ describes how to use an array for the counters.
Use a loop to roll a pair of dice 1000 times to get the value of 2-12 for each roll
Now you can use the switch and case statements to increment the counters
// |
Q1. | Hints for the Roll Dice Histogram Using an Array? |
A1. |
Two functions are needed when using the random number generator in C or C++. The rand( ) function produces random numbers based on its own internal formula. The srand() function is only used one time, typically at the beginning of the program. It 'seeds' the random number generator so that it does not produce the exact same sequence of numbers every time the program is run. Start the program with int main, then call the srand( ) function. To make sure it is only called one time, do not put the srand( ) function in a loop. The code for the roll( ) function also shows another way to seed the random number generator, and only do it one time.
You can either use a collection of separate counters or an array to keep track of each time a pair of numbers occurs when rolling the dice. A separate FAQ describes how to use separate counters.
Use a loop to roll a pair of dice 1000 times to get the value of 2-12 for each roll
Now you can use the switch and case statements to increment the counters }
// |
Q1. | How do I use the screen magnifier on Windows or macOS |
A1. |
The Windows Magnifier tool can be used on any part of the screen. To activate the magnifier
On a Mac and running macOS, the magnifier/Zoom tool is activated by
|