Quest

Hey guys am having an exam on Monday in c++ so i was wondering if i can get some help , because the questions are multiple choices about the output of programs and if the program has any errors , so is there any sites that would help ??
It helps to write code so that you get used to checking for errors and syntax. After looking at code you've written and running into an error over and over, you learn what to look for... How much actually coding have you done? That will help you to recognize what to look for. Try writing some simple things: a for loop that iterates an array, a while loop that pulls from a file and spits to a console, an if statement that changes the variable depending on the condition... You could also look at peoples code that has been posted here and try and figure stuff out, but ultimately you don't really learn until you start typing it in yourself.
Last edited on
http://cplusplus.com/doc/tutorial/

If you have any other questions, I am sure the folks here will be more than willing to answer.
Okay craniumonempty thanks I think am gonna start looking in the codes posted here... and try codes , and hanst99 thank u I already have the tutorial and it's helpful .
Have you tried a simple hello world or something? Start with that to see the basic syntax and work up from there.
Think through everything you remember from class and ask about what you don't understand.
Well I wanted to know what does it mean if we Put a & before the name of the function ? for example : int & addition (int a, int b) {return (a+b);}
and another question actually :
1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;
int main() 
{ 
   int a=10,b;
   b=a++ + ++a;
   cout<<b<<" "<<a++<<" "<<a<<" "<<++a;

}

the output of this program is : 22,13,14,14 ... I know it's about operations but how ??
b=a++ + ++a
is the same as
b=a;
a+1;
b+=a+1

a++ will return a, then increment it, ++a will first increment, then return it.

The & in int& addition(... is part of the return type declaration. It means that the return type is a refernce to an int.
b=a++ + ++a
is the same as
b=a;
a+1;
b+=a+1


This is not true:

Note that the output here is UNDEFINED.

You cannot legally modify and access a variable more than once between sequence points (ie: a semicolon)

for example:

1
2
3
4
5
int a = 0;
b = a++ + ++a;

cout << a;  // this will print 2
cout << b;  // ??  no way to know what this will print 


printing b might get you 1, 2, 3, or something else. The result is not defined by C++, so the compiler is free to do whatever it wants.

General rules of thumb:

- don't needlessly and overly compound statements. Even if the result was defined, it's super confusing. Trying to cram as much code as you can into one line is pointless and makes your code a mess.

- if you write/modify a var, that var should not exist anywhere else in the same line/sequence point. For example:

1
2
3
4
5
6
7
b = a++ /*now that we're modifying 'a', it shouldn't exist anywhere else in
       this line of code */  ;

b = a++;  // OK

b = a++ + a;  // BAD
b = a + a++;  // BAD 
Last edited on
Aha... It was a question by one of my professors about the output of that program... Just help me gettin what happened in the line 7 please... :
1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;
int main() 
{ 
   int a=10,b;
   b=a++ + ++a;
   cout<<b<<" "<<a++<<" "<<a<<" "<<++a;

}
and how did the output came into 22,13,14,14
Line 6 and 7 are undefined behavior. Line 7 is egregious.
Last edited on
rocketboy wrote:
Line 6 and 7 are undefined behavior.


^ This ^ Your professor shouldn't even be showing you code like this.

Take a look at line 6:

b=a++ + ++a;

The compiler is free to do any of these operators in whatever order... as long as it follows rules of operator precedence.

In this case, it seems the compiler is doing this (but again note it doesn't HAVE to do it this way, it could do it another way, producing different results):

1
2
3
4
a += 1;  // ++a
b = a;   // using the post-increment value of ++a
b += a;  // using pre-increment value of a++
a += 1;  // a++ 


This results in a = 12 and b=22.

Line 7 has a similar problem:

cout<<b<<" "<<a++<<" "<<a<<" "<<++a;

The compiler seems to be doing this (but again, it doesn't have to -- it could print something different):

1
2
3
4
5
6
a += 1;           // ++a (a now = 13)
cout << b << " "; // prints 22
cout << a << " "; // prints 13 (a++) -- using pre-increment value of a++
a += 1;           // a++
cout << a << " "; // prints 14 (a)
cout << a;        // prints 14 (++a) -- using post-increment value of ++a 
Last edited on
Okay got it thanks... excuse I am asking a lot but I have an exam... :/ What's the difference between :
1
2
3
int const  *  p     
const int *p
const int *const p

is it that the first one is a const itself and second one should point to a const , and what about the 3rd one ?? really thanks guys for ur help ...
And I got another example for the previous idea...
1
2
3
  int x( 1 );
    cout << x << x++ << ++x << x++ << x++ << x << x++;
    //output: 6563261 

It's really confusing ...
In int const *x; and const int *x; the int is constant. In int * const x; the pointer is constant. In int const * const x both are constant.
Once again, modifying a variable more than once in a statement is undefined behavior. The compiler is allowed to do anything, including send hate mail to all your friends, eat your face off, or burn your processor to a crisp.
Last edited on
closed account (z05DSL3A)
What's the difference between :
1
2
3
int const  *  p     
const int *p
const int *const p

is it that the first one is a const itself and second one should point to a const , and what about the 3rd one ?? really thanks guys for ur help ...

Read them from right to left will help you work them out.
1, p is a pointer to a constant integer.
2, p is a pointer to an integer constant (same as 1)
3, p is a constant pointer to an integer constant.
Thanks guys... one last question , it's about the static functions ... I didn't find anything about them in the tutorial , what do we use them for ?
Last edited on
Topic archived. No new replies allowed.