Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Complete the following assignments. Copy and paste your finished code into a Wor

ID: 3809139 • Letter: C

Question

Complete the following assignments. Copy and paste your finished code into a Word document, clearly identifying which assignment it is. Also, capture the output of the program and paste that into the Word document. If there are questions to be answered, put the answers after the output. When you complete all three of this week’s assignments, save the document as yourLastName_GSP115_W5_Assignments.docx. Submit it to the Week 5 assignment Dropbox.

1. Compile Time Bugs

Find and fix the eight compile time errors in the code at the end of this section. Compile time bugs show as errors when you compile, but the Visual Studio IDE also gives you visual clues in the form of red squiggly underlines, as shown here. This time, we will talk about the error codes in Visual Studio and how to use them. You will find documentation on using Visual Studio 2012 error codes at http://msdn.microsoft.com/en-us/library/33df3b7a(v=vs.110).aspx.

The error list is included after the code, but only refer to it if the line numbers in your error list don’t match. This could happen if you make a change typing in the code or if you have a copy-paste error. You need the experience of using the error list provided by Visual Studio.

We have taken the first error from the error list and will use it to explain the parts of the error list.

Error 3 error C2660: 'func2' : function does not take 0 arguments C:YOUR_PATHweek5 assignment-1main.cpp 46 Week5 Assignment-1 1

Error 3

These two columns give you the issue type (warning, error, or intelliSense error) and the error number based on when the compiler found the error.

error C2660: 'func2' : function does not take 0 arguments

This is the description column. It includes the generic Microsoft Error ID Number and a brief description. The Error ID number is useful for searching for information on the error online. Start by reading the Microsoft sites first.

C:YOUR_PATHweek5 assignment-1main.cpp

This is the full path to your file. Only the file name shows in the column, but you get the full path if you copy and paste the errors into MS Word.

46

This is the line number in your code where the error occurred. You can also double click on the error and you will be taken to the line with the error.

Week5 Assignment-1

This is the project name.

1

This is the column number where the error occurred. One usually indicates something is wrong with the entire line. You have to right click in the headings, such as description or file, to get a menu that lets you choose which columns to show. The column number column is turned off by default; to show it, you must click in the check box.

Here is a walk-through for fixing a couple of the errors. Remember, you should always start with the first error. Every time you fix an error, the error list can change. IntelliSense will reevaluate the error and may reinterpret it based on the changed code. This is why you start with the first error; some of the later errors may not actually be errors or may not be the errors IntelliSense interpreted them to be.

Let’s look at the first error, the one listed as Error 3. (It is 3 because 1 and 2 are warnings.)

Assuming you have already entered the code, here is one set of steps you can follow. First, look at the line number of the error (you can jump to that line by double clicking the error—handy if you don’t have line numbering turned on). That would be line 46, which is this.

       cout << "Flipping a coin. The result is " << func2()?"Heads.":"Tails."; << endl;

The error description says error C2660: 'func2' : function does not take 0 arguments. It turns out that we actually have two errors in this line of code, but this error has nothing to do with either of them. The error is telling us that func2() doesn’t take 0 arguments, but looking at the func2 definition, we see it does take 0 arguments. What’s going on here? Wait a minute; the compiler gets its information on the number of parameters from the prototype. What does the prototype say? Oops, it says bool func2(int);. This could be either a simple typo or it could be we changed our function when we were entering in the code for the definition and forgot to change the prototype. The latter is a common mistake. So let’s change the prototype to read boolfunc2(); and build again to see if the error goes away.

It did, but it has been replaced with another error on the same line. This one says error C2143: syntax error : missing ';' before '<<', which was the next one on our list. This is a little more unclear. Why would we want a semicolon before <<? If we check online using error C2143, the first entry is a MSDN page. It doesn’t have our specific example, but it does tell us—in a roundabout way—that it wasn’t expecting to see <<. Looking back at line 46, we notice there is a semicolon before the <<, and it shouldn’t be there. We mistakenly typed a semicolon after the conditional operator by force of habit. It is instructional to notice that the error definition is all the more confusing because we actually did have a ; before <<. The lesson here is to not take the error definition too literally. Let’s remove the offending semicolon and rebuild.

Now we have two new errors, both on line 46. The error definitions, once again, don’t seem too helpful. However, both errors seem to indicate that the << is having trouble deciding how to print something we gave it. Because we didn’t get the error until we fixed func2(), that is probably our culprit. It could be that it doesn’t know whether to print the bool that func2() returns or the string the conditional operator returns. It’s the latter that we want, so—because parentheses are the duct tape of C++ programming—let’s put parentheses around the conditional operator. Now line 46 looks like this.

cout << "Flipping a coin. The result is " << (func2()?"Heads.":"Tails.") << endl;

Rebuilding, we see both errors are gone and our first error now is on line 50. Parentheses save the day again. We’re going to let you work on the remaining errors. They all have to do with issues with the functions—except for error C2143 on line 82. This one is an example of where the error definition tells you exactly what’s wrong.

Here is the code.

// Week Assignment-

// Description:

//----------------------------------

//**begin #include files************

#include <iostream> // provides access to cin and cout

#include <time.h> // provides access to time() for srand()

//--end of #include files-----------

//----------------------------------

using namespace std;

//----------------------------------

//**begin function prototypes*******

double func1( int[]);

bool func2(int);

//--end of function prototypes------

//----------------------------------

//**begin global constants**********

const int ArraySize = 10;

//--end of global constants---------

//----------------------------------

//**begin main program**************

int main()

{

        // seed random number generator

        srand(time(NULL));

        // create and initialize variables

        string myString;

        int myInt;

        bool myBool;

        int myIntArray[ArraySize];

        // initialize the array with random numbers 1-100

        for (int &i:myIntArray)

        {

                i = (rand()%100) + 1;

        }

        // calling the functions

        for (int i = 0; i < 4; i++)

        {

                myInt = func1( myIntArray);

                cout << "Value returned by func1 was " << myInt << endl;

        }

        cout << "Flipping a coin. The result is " << func2()?"Heads.":"Tails."; << endl;

        for (int i = 0; i < 4; i++)

        {

                int ranNum = rand()%3;

                cout << "The number " << ranNum << " is " << func3(ranNum) << endl;

        }

        // Wait for user input to close program when debugging.

        cin.get();

        return 0;

}

//--end of main program-------------

//----------------------------------

//**begin function definitions******

int func1( int [])

{

        return int [rand()%ArraySize];

}

bool func2()

{

        return rand()%2?true:false;

}

string func3( int anInt)

{

        switch (anInt)

        {

        case 0:

                return "ZERO.";

                break;

        case 1:

                return "ONE.";

                break;

        case 2:

                return "TWO."

                break;

        default:

                break;

        }

        return "Not Found.";

}

//--end of function definitions------

//----------------------------------

Here is the error list.

Issue Type and number

Description

File

Line

Project

Error

3

error C2660: func2 : function does not take 0 arguments

C:YOUR PATH week5 assignment-1main.cpp

46

Week5 Assignment-1

1

Error

4

error C2143: syntax error : missing ; before <<

C:YOUR PATH week5 assignment-1main.cpp

46

Week5 Assignment-1

1

Error

5

error C3861: func3: identifier not found

C:YOUR PATH week5 assignment-1main.cpp

50

Week5 Assignment-1

1

Error

6

error C2556: int func1(int []) : overloaded function differs only by return type from double func1(int [])

C:YOUR PATH week5 assignment-1main.cpp

61

Week5 Assignment-1

1

Error

7

error C2371: func1 : redefinition; different basic types

C:YOUR PATH week5 assignment-1main.cpp

61

Week5 Assignment-1

1

Error

8

error C2062: type int unexpected

C:YOUR PATH week5 assignment-1main.cpp

62

Week5 Assignment-1

1

Error

9

error C2143: syntax error : missing ; before break

C:YOUR PATH week5 assignment-1main.cpp

82

Week5 Assignment-1

1

IntelliSense Error

10

IntelliSense: cannot overload functions distinguished by return type alone

C:YOUR PATH week5 assignment-1main.cpp

15

Week5 Assignment-1

8

IntelliSense Error

11

IntelliSense: expected an expression

C:YOUR PATH week5 assignment-1main.cpp

46

Week5 Assignment-1

74

IntelliSense Error

12

IntelliSense: no operator << matches these operands (operand types are: std::basic_ostream<char, std::char_traits<char>> << std::string)

C:YOUR PATH week5 assignment-1main.cpp

50

Week5 Assignment-1

45

IntelliSense Error

13

IntelliSense: type name is not allowed

C:YOUR PATH week5 assignment-1main.cpp

62

Week5 Assignment-1

9

IntelliSense Error

14

IntelliSense: expected a ;

C:YOUR PATH week5 assignment-1main.cpp

82

Week5 Assignment-1

3

Error 3

These two columns give you the issue type (warning, error, or intelliSense error) and the error number based on when the compiler found the error.

error C2660: 'func2' : function does not take 0 arguments

This is the description column. It includes the generic Microsoft Error ID Number and a brief description. The Error ID number is useful for searching for information on the error online. Start by reading the Microsoft sites first.

C:YOUR_PATHweek5 assignment-1main.cpp

This is the full path to your file. Only the file name shows in the column, but you get the full path if you copy and paste the errors into MS Word.

46

This is the line number in your code where the error occurred. You can also double click on the error and you will be taken to the line with the error.

Week5 Assignment-1

This is the project name.

1

This is the column number where the error occurred. One usually indicates something is wrong with the entire line. You have to right click in the headings, such as description or file, to get a menu that lets you choose which columns to show. The column number column is turned off by default; to show it, you must click in the check box.

Explanation / Answer

Error 3:
cout << "Flipping a coin. The result is " << (func2(2)?"Heads.":"Tails.")<< endl;
-> func2 takes int argument as decalred above, so calling method without passing argument makes ambiguity
-> short form of if then else works correclty but should be placed within braces.
Semicolon is not needed inside cout statement, remove the semicolon.

Error 4:
cout << "Flipping a coin. The result is " << func2()?"Heads.":"Tails."; << endl;
-> The semicolon is not needed inside cout statement.
It is only used at end of statement when function call is needed.
cout is itself an object of ostream class and while calling "<<" method, we do not passing semicolon until end of statement is reached

Error 5:
func3 definition is not found. Some compilers expects to either declare functions before calling
them or defining them above the calling functions. So giving forward declarations
like func1 and func2 solves the problem
string func3( int anInt);

Error 6:
Ambigous function declarations, functions should return int instead of double as modulus returns full int value and not double
Correct by changing double from double func1( int []) to int func1( int [])

Error 7:
above change in error 6 resolves this issue as same type is passed, so same basic types

Error 8:
int is not needed, returning value from a function just need to be specified in starting of function declaration and definition.

Error 9:
Compile error due to missing semicolon after return statement.

Error 10: NOT needed to solve as intellisense error are not same as compile error
undocumented solution: just turn them off!
Simply right-click anywhere on the Visual Studio error list (the click must be inside the list, not outside), then uncheck “Show IntelliSense Errors”.

Error 11: Error solved by making round brackets and removing semicolon in
previous error at same line

Error 12: Ignore, already solved by above done changes for func3 declaration.

Error 13: Ignore, we can return integer value.

Error 14: Ignore, same as problem already solved in same line number above.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote