0

I'm currently in a class for C++. I just took a midterm, and I thought all my answers were very good, but some didn't get any points. I just wanted to find out what I did that was wrong about them, not to doubt my teacher, but to just know how I got my answer wrong.

The first one that I got wrong was:

Using three different rules for creating valid variable names, make up an example for each that would NOT compile.

My answer was:

int ex ample
int 2example
char cout

Here I'm assuming its the Char, I feel as though I should have called a different variable.

2. (This ones a longer one)

What is the EXACT output of the following program given that the user enters the following inputs:

P
Maria Lucelli
1000
char status;
string name1;
string name2;

int amount;

cout << "What is the customer’s status ?";
cin >> status;

if (status == 'P')
    cout << "Preferred customer, eligible for gold card."<<endl;
else
    cout << "Regular customer."<< endl;

cout << "Enter full name :";
cin >> name1 >> name2;

cout << "Thank " << name1 << " for his/her business" << endl;

if (status == 'P')
{
    cout << "Ask how much to transfer to 2.9 %";
    cin >> amount;
    if (amount > 1000)
        cout << "This may take a moment to verify.";
    } else
        cout << "Have a nice da y.";

My answer was:

What is the customers status? P
preferred customer, eligible for gold card.
Enter full name: Maria Lucelli
Thank Maria for his/her business
Ask how much to transfer to 2.9% 1000
have a nice day.

I literally copied it letter by letter. I really would like to know what I did wrong for this one.

3.

Write a block of code that demonstrates the concept of overflow.

My answer was:

int a;
a=INT_MAX;
cout<<a+5;

When I ran this, it overflowed to a negative number and yadda yadda. Could I have misunderstood what overflow means?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
BenIsBored
  • 17
  • 2
  • 9
    Your teacher is the best person to have this discussion with. Only they know why they graded you as they did and why. – Retired Ninja Mar 09 '22 at 04:54
  • 3
    1. `char cout;` is valid; 2. There is no space between the first line of output and the first input -- I stopped reading after that; 3. That is signed (usually) integer overflow which is undefined behavior -- they either wanted _unsigned_ integer overflow, or _buffer overflow_ -- or maybe Stack Overflow. Or maybe it's the fact that you did not define a type for `a`. – paddy Mar 09 '22 at 04:54
  • 1
    Since there's no declaration for `status` it's impossible to predict the exact behavior of question 2. – Mark Ransom Mar 09 '22 at 04:56
  • 1
    If I was grading these, I'd also give them all zero. Your claim _"I literally copied it letter by letter."_ is delusional. Just look at the upper/lowercase, whitespace in strings as a basic first pass. Also the fact that you wrote the inputs there, but those are not _output_. And as always, `"Have a nice da y."` (note also that would never be output if status was equal to 'P'). – paddy Mar 09 '22 at 05:01
  • I wouldn't be that harsh on him. Conceptually he was thinking in the right direction. I'd probably ding the teacher for failing to emphasize the distinction between unsigned overflow and signed *implementation defined* behavior. However, if there are mis-copies in the actual code to the answer -- that should get you dinged as programming is all about *Attention to Detail* and I've seen the type of profs that would just go ape on that.... – David C. Rankin Mar 09 '22 at 07:15
  • @paddy I don't know why you have to imply a condescending tone, I really just asked a simple question for some advice. As I'm still new to all of this and just trying to get better, I ask if you could possibly elaborate more with your answer in a decent human way. Also, for the code about inputting the information, the space between "nice da y" was a mistake when copying to the page, the teacher did not actually put that. Just a small simple mistake, I'm only human. – BenIsBored Mar 09 '22 at 08:13
  • @DavidC.Rankin Thank you David. This was not only my midterm exam, he has also never previously asked a question of copying code and answering with the output, so i didn't expect him to be so strict about it. Now i know that minor details are extremely important, and looking back that makes sense. I have no clue what the distinction between unsigned overflow and signed implementation-defined behavior is. I will look into that, thank you. – BenIsBored Mar 09 '22 at 08:19
  • 1
    Attention to detail is the name of the game, and the lack thereof was my main criticism. People in the industry will be far more blunt. I thought I was holding back! I do not wish or intend my feedback be taken to heart. But it was, and that indeed makes you human. Maybe I'm not human. I'll try and your feedback on board. – paddy Mar 09 '22 at 09:51
  • Regarding details: some lines end with `endl`, some do not. In that case the following output would be on the same line. – Raildex Mar 09 '22 at 17:35
  • @BenIsBored the basic distinction is when an unsigned value overflows, it simply wraps around and starts counting from zero again (technical terms: the value is reduced modulo by the max value plus `1`). The standards does not specify what happens when a signed value cannot be represented by the current storage size. So what happens is left to the compiler to implement (*implementation defined*). See [Signed to unsigned conversion in C - is it always safe?](https://stackoverflow.com/questions/50605/signed-to-unsigned-conversion-in-c-is-it-always-safe/50632#50632) – David C. Rankin Mar 09 '22 at 17:49
  • For "What is the EXACT output of the following program given that the user enters the following inputs:" you've copied the *input* as well. An interactive shell will show the input interleaved with the output like that, but if you redirect the output to a file, the file *won't* have the input – Caleth Mar 09 '22 at 17:55
  • For your last answer to 3., the only change needed would be to change `int` to `unsigned` and `INT_MAX` to `UINT_MAX` (or more properly in modern C++,`std::numeric_limits::max()`). Then you would know the output of `std::cout << a + 5;` would be `4`. – David C. Rankin Mar 09 '22 at 18:07

1 Answers1

1

The first one that i got wrong was: Using three different rules for creating valid variable names, make up an example for each that would NOT compile.

My answer was

int ex ample
int 2example
char cout

last one is wrong. Probably you have hoped to have symbol collision with std::cout, but this can only when definition is done in global scope and using namespace std; is used. In other cases new cout will overshadow cout even when using namespace std; is used.

Use of keyword could be a good example: char switch; will not compile. Or redefinition of symbol.

2.(This ones a longer one) What is the EXACT output of the following program given that the user enters the following inputs:

First off all this code contains invalid characters. Fix it then just run it to see the outcome: https://godbolt.org/z/Y51z1Ye75

Your answer is mix of input with output.

3. Write a block of code that demonstrates the concept of overflow.

Answer:

int a;
a=INT_MAX;
cout<<a+5; 

Here problem is a question. There are many kinds of overflows. You presented proper example of "integer overflow", but there is also "stack overflow", or "buffer overflow". It is possible there is also other kind of overflows which just do not pop up in my mind. So it is possible that teacher expected other kind of overflow - in such case problem is imprecise question

Marek R
  • 32,568
  • 6
  • 55
  • 140
  • I think the problem with 3. is the standard does not define signed integer-overflow. (that is left to the compiler writer to implement). You could also be correct, that the intent was some other type of overflow, but if I had to bet, I would wager the OP was thinking correctly about what overflow was wanted, but overlooked the distinction between `unsigned and signed behavior. – David C. Rankin Mar 09 '22 at 18:15