1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

C++ Assigning Values

Discussion in 'Scripting & Programming' started by Mof, Aug 2, 2008.

  1. Mof

    Mof Megabyte Poster

    526
    2
    49
    [/CODE][/CODE]Here we go again at least the threads should be smaller.

    [
    ]
    would you like to test me on this see how i get along.
     
    WIP: C++ and A+
  2. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    Okay! :biggrin

    As with all code there are several ways of writing a program that will produce identical output. Find as many ways as possible of writing this program so that the output is identical. :)
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  3. Mof

    Mof Megabyte Poster

    526
    2
    49
    Hi Mathmatix Ive been practicing with assigning values and putting into pratice the endl function simple as it is. have you got an example what you would use assigning values for;
     
    WIP: C++ and A+
  4. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    Hi Mof

    This is the reason why I asked you to find as many different ways as possible to write the program. This will allow you to experiment with assignments. :)
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  5. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,623
    115
    224
    As an aside - if you wrap your code in [ CODE] [ /CODE] tags (use the button marked '#' in the posting box) then it will format a bit better, and you won't be plagued with smileys in odd places!

    Edit - To get the tags to display I needed extra spaces - the tags normaly don't have spaces.

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  6. Mof

    Mof Megabyte Poster

    526
    2
    49
    Have I got this completely wrong
    as when I try to compile it it says c++ score.cpp no such file or directory

    # include <iostream>
    Code:
    using namespace std;
    
    int main()
    {
    int a,b,c,d;
    Cout<<"A="<<(a=1)<<" ";
    cout<<"B="<<(b=2)<<" ";
    cout<<"c="<<(c=4)<<" ";
    cout<<"D="<<(d=8)<< endl;
    Cout<<" Assigning D the Interger Value of 15:"<<endl;
    cout<<"D=(a+b+c+d)="<<d=(a+b+c+d)<<endl;
    cout<<"Assign A the Interger value of 58:"<< endl;
    cout<<"A= (D*C)-(b)="<< a=(d*c)-(2)<<endl;
    cout<<"A="<<a <<" ";
    cout<<"D="<< d <<endl;
    return 0;
    }
    so what have i done
     
    WIP: C++ and A+
  7. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    Here's what you have did wrong:

    1. C++ is case sensitive, so 'Cout' is different from 'cout'.
    2. In these two lines of code:
    Code:
    cout<<"D=(a+b+c+d)="<<d=(a+b+c+d)<<endl;
    ...
    cout<<"A= (D*C)-(b)="<< a=(d*c)-(2)<<endl;
    
    you are trying to use an overloaded definition of cout (enforcing more than one definition of cout) by performing an assignment within its call. You do have the option of fixing it with:
    Code:
    cout<<"D=(a+b+c+d)="<< a+b+c+d <<endl;
    ...
    cout<<"A= (D*C)-(b)="<< d*c-2 <<endl;
    
    making variables 'a' and 'd' redundant. The definition of cout that you're trying to use doesn't exist, hence the errors.

    Lastly, you do not need to put brackets around your calculations as I've shown above. Google 'rules of precedence' to see what I mean.
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  8. Mof

    Mof Megabyte Poster

    526
    2
    49
    Hi Mathmatix. Ok I understand 2 points you have made first I did know c++ was case sensitive so Cout,cout and Cout would all be seen differently I just hit the shift key whilest on auto pilot,made a similar mistake the other day when I dropped the "E" from namespace had me going for a good half hour.the other one was arithmetic I can see what you mean about the brackets its just the book says its to make it clearer when reading. the bit I cant seem to get my head aroud is making the A and D redundant.


    # include <iostream>
    using namespace std;

    Code:
    int main()
    {
    int a,b,c,d;                               // here 
    cout<<"A="<<(a=1)<<" ";            // hear
    cout<<"B="<<(b=2)<<" ";
    cout<<"c="<<(c=4)<<" ";
    cout<<"D="<<(d=8)<< endl;         //hear
    cout<<" Assigning D the Interger Value of 15:"<<endl;
    cout<<"D=(a+b+c+d)="<<d=(a+b+c+d)<<endl;
    cout<<"Assign A the Interger value of 58:"<< end;
    cout<<"A= (D*C)-(b)="<< a=(d*c)-(2)<<endl; // I was trying to assign integer values to a
    cout<<"A="<<a <<" ";                // hear
    cout<<"D="<< d <<endl;             // hear
    return 0;
    }
    I must admit it is a problem not having a compiler at work. I know its proberley staring me in the eyes.
     
    WIP: C++ and A+
  9. Mof

    Mof Megabyte Poster

    526
    2
    49
    Cheers Harry for that they were begining to bug me.
     
    WIP: C++ and A+
  10. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    These two lines were causing problems:

    Code:
    cout<<"D=(a+b+c+d)="<<d=(a+b+c+d)<<endl;
    ...
    cout<<"A= (D*C)-(b)="<< a=(d*c)-(2)<<endl;
    
    To remove the problem you had to remove the attempt at assignment ('d=' and 'a='). Since you removed them they were made redundant as they were no longer in use even though they were declared. This raises the question of where the result of '(a+b+c+d)' and '(d*c)-(2)' are stored if you remove the variables to which you normally would assign the calculations? At run time the results of the calculations are swtored in 'temporary variables' that are not explicitly declared by the programmer. Once the result is referenced and finished with, they are destroyed. This is why they are redundant.

    If you wish to reference the values later it would be necessary to store them as before. But to do this and get around the error you should revise your code, thus:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int a,b,c,d;                            
    	cout<<"A="<<(a=1)<<" ";          
    	cout<<"B="<<(b=2)<<" ";
    	cout<<"c="<<(c=4)<<" ";
    	cout<<"D="<<(d=8)<< endl;      
    	cout<<" Assigning D the Interger Value of 15:"<<endl;
    	d=a+b+c+d;						   // Moved calculation here.
    	cout<<"D=(a+b+c+d)="<< d <<endl;   // Only variable 'd' is referenced by 'cout'.
    	cout<<"Assign A the Interger value of 58:"<< endl;
    	a=d*c-2;						   // Likewise here.
    	cout<<"A= (D*C)-(b)="<< a <<endl;  // And here.
    	cout<<"A="<< a <<" ";              
    	cout<<"D="<< d <<endl;        
    	return 0;
    
    }
    
    and that's how you would get this code to work and retain your values in variables.

    This is only a valid reason when you have very long calculations. Brackets (or more correctly 'parenthesis') are normally used to enforce an order of calculation that otherwise breaks the rules of precedence. Example:

    3*4+2 = 14 (3*4 is calculated first because multiplication has a higher precedence over addition, then 2 is added.)

    3*(4+2) = 18 (4+2 is calculated first since we've broken the precedence order by using the parenthesis to force this to be calculated first - then the result multiplied by 3.)

    This is exactly the same as for elementary algebra. This is the reason why we have them. :)
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  11. Mof

    Mof Megabyte Poster

    526
    2
    49
    Code:
    a=d*c-2;
    of coarse this to is a mistake on my behalf,as it should read

    Code:
    a=d*c-b
    in my defence I have had a few interupted sleeps:tune:baby:sleeps,
    ill have a look when I get home later, but as for assigning i think Ive got the idear
    just need to write more code to see what works.

    I know I forgot the ;
     
    WIP: C++ and A+

Share This Page

Loading...