Monday, July 27, 2009

Can someone tell me if I'm doing these correctly? C++ declaring variables?

I am new at this and wanted to see if I am doing these correctly.








1)Declare a variable named var to hold a person's monthly salary , showing dollars and cents, and initialize the variable to one cent less than five thousand dollars





double var=4999.99;








2)declare a variable named var to hold the name of a book, and initialize the variable to My Dear Friend





string var="My Dear Friend";





3)declare a variable named var to hold an array of five integers , and initialize the first three array elements to 7.





int[0] var=7;


int[1] var=7;


int[2] var=7;








4)declare a variable named var to hold a single character, and initialize the variable to the first letter of your last name.





char var='A';





5)declare a constant named var whose value is fixed throughout the program at 6.28





const double var=6.28;





or is it





double var= const 6.28;

Can someone tell me if I'm doing these correctly? C++ declaring variables?
1) OK!


2)OK! but remember to include in your .cpp file the include directive:


//at the beginning of your source file


#include %26lt;string.h%26gt;





3)you have to first declare the array of five elements like this:





//Declare the array


int var[5];





//then you initialize the first three values.





var[0]=7;


var[1]=7;


var[2]=7;





4)OK!


5) const [type of your variable] [variable's name] = [some value];
Reply:at 3) you didn't declare the array!
Reply:1) double var = 4999.99


2) char *var = "My Dear Friend";


3) int var[3] = {7, 7, 7};


4) char var = 'A';


5) const double var = 6.28;


No comments:

Post a Comment