Sunday, July 12, 2009

C++ Help Please.?

1. Write a program that declares two constants (A and B).


2. Initialize A=1 and B=2.2


3. Declare an int named C and a float named D.


4. Initialize C=A and D=B.


5. Write statements to print C and D to the screen.


6. Compile and Run.





This is what I did but everytime I run the program it doesn't work! Help please.








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


int main()


{


// declare a constant for A and B


int a; // declare a as an integer


int b; // declare b as an integer


int c; // declare c as an integer


float d; // d as a floating-point number





a = 1; // intialize a to 1


b = 2.2; // initialize b to 2.2


c = a; // initialize c to a


d = b; // initialize d to b





// output constant and variables to screen


cout %26lt;%26lt; c and d %26lt;%26lt; '\n';


cout %26lt;%26lt; c %26lt;%26lt; '\n';


cout %26lt;%26lt; d %26lt;%26lt; '\n';


return 0;


}

C++ Help Please.?
You declaring B as an integer instead of a float. So the way your program is written B will be 2 and then d will also be 2.





Hope this helps
Reply:You declared variables for a start, not constants.





const int a=1


const float b=2.2





You should also initialise your variables as you declare them


int c=a;


float d=b;
Reply:Like this:








const int a=1;


const float b=2.2;


int c=a;


float d=b;





_________





Also, put the line "using namespace std;" before int main(). Probably that's why you are not able to compile..





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


using namespace std;


int main()


{





//code





}
Reply:This seems to be an error:


cout %26lt;%26lt; c and d %26lt;%26lt; '\n';





Try:


cout %26lt;%26lt; a %26lt;%26lt; " and " %26lt;%26lt; b %26lt;%26lt; '\n';





Secondly:





const int a = 1;


const int b = 2.2; // actually it should be const float b = 2.2 but may be it is was intentional in the question





Edit -----------------------------





int main()


{


// constants must be initialized as soon as they are declared


const int a = 1;


const float b = (float) 2.2; // this might give warning


int c; // declare c as an integer


float d; // d as a floating-point number


c = a; // initialize c to a


d = b; // initialize d to b


/*


you are not asked to print a and b but anyway


cout %26lt;%26lt; a %26lt;%26lt; " and " %26lt;%26lt; b %26lt;%26lt; '\n';


*/


cout %26lt;%26lt; c %26lt;%26lt; '\n';


cout %26lt;%26lt; d %26lt;%26lt; '\n';


return 0;


}
Reply:What do mean it doesn't work? Does it compile? What compiler are you using?





I think you need to use double quotes (") instead of single quotes. Also, put the text c and d (the first cout) in double quotes.
Reply:Might help if you tell us the output you get compared to the output you expect? It's not wise to simply convert types by using =, your compiler will warn you.





Change:


cout %26lt;%26lt; c and d %26lt;%26lt; '\n';





to:


cout %26lt;%26lt; c %26lt;%26lt; d %26lt;%26lt; '\n';


No comments:

Post a Comment