C++ Homework

I have been working on my homework assignment this weekend and have gotten about half way through it...very slowing of course. I have a very confusing teacher and the way she words things can make things very hard to understand. Not to mention many of assignment she gives us are filled with spelling errors...

However, putting that aside I wondered if someone with better knowledge could "translate" the following question & statements. I have included some of my answers which I am not sure are correct.

Her wording confuses me and I am not sure what she is asking for.

4. Write C++ statements to accomplish the following:
a) Declare int variables numb1 and numb2. Initialize numb1 to 25 and numb2 to 18.
int numb1=25;
int numb2=18;

b) Declare and initialize an int variable temp to 10 and a char variable letter to ‘A’.

int temp = 10;
const char let = 'A';


c) Update the value of an int variable amt by adding 5 to it.
int amt=5;

d) Declare and initialize a double variable payRate to 12.50.
double payRate=12.50;
e) Copy the value of an int variable firstNum into an int variable tempNum.


The last one leaves me out in the cold..I don't have a clue what she means or if I have answered the other questions right...What does she mean in the last one? reword anyone?
she means "assign" the value held by firstNum to tempNum, that is, quite simply,
tempNum = firstNum
that indeed will copy the value on the right hand side to the variable on the left hand side

BTW
b) watch out here, you want a char variable, not a const char . If you declare it "const" it is a constant and cannot be changed.
char letter = 'A'

c) you are declaring and initialising a variable here, you aren't adding anything. Your task is to add a value to an existing (i.e. already declared and initialised) variable. So:
amt = amt + 5;
or, if you want to be smarter
amt += 5;
does the same thing.
Thank you for taking the time to answer my post. I used char letter = 'A' in the compiler and it gave me errors. So I went with the const char thinking that was the way to go (still very new here). I am not sure if she wants the output to show the ' on either side of the A.

On another note, would it be considered bad form for me to post the given assignment with my answers for others to tell me if I am doing things correctly?
My teacher really has no extra time to help me or others in the class. I hope to find a tutor this week through the school though that will be well after this assignment will be handed in and I want to do well...even it takes me all weekend.
I used char letter = 'A' in the compiler and it gave me errors.


If that's true, your errors were completely unrelated to that line of code, because char letter = 'A'; compiles just fine with no error.
Yes, your char declaration should have compiled just fine. As for the last part, all your teacher is asking you to do is copy firstNum into into tempNum. A simple assignment statement ( = ) will take care of this.
Thank you again for clearing up the confusion. I am down the last part of my assignment. Question 6 seemed easy enough, however I was confused by the cout << fixed << setprecision(1); should I assume that things will show up one space out from the left of the screen? When I put it all into the compiler I got errors until I removed the cout << fixed << setprecision(1); line. I have posted my answers for question 6 (The _ is for space.)
Question 5 leaves me at a loss. I put what I think the answer is but it just seems wrong, any thoughts on this?

5. Suppose numb and value are int variables and letter is a char variable. Consider the following input:
5 28 36
What value, if any, is assigned to numb, value and letter after each of the following statements executes? (use the same input for each statement)
a) cin>>numb >> value>>letter; 5 28 36
b) cin >> letter >>numb >> value; 36 5 28
c) cin >> numb >> letter >> value; 5 36 28

6. Given the following declarations:
int numb =5;
double amt=15.68;
cout << fixed << setprecision(1);

What is output from the following?

a) cout<< setw(5) << numb << endl; _ _ _ _5
b) cout << setw(3)<< amt << endl; 15.7
c) cout << numb <<amt<< endl; 515.7
d) cout << left << setw(5) << numb << setw(5) << amt; 5_ _ _ _15.7
Topic archived. No new replies allowed.