Friday, July 31, 2009

Am i doing this c++ program right?

can someone plz tell me what i'm doign wrong plzzz?





#include %26lt;conio.h%26gt;// needed for getch


#include %26lt;iostream.h%26gt;//needed for cout and cin messages


#include %26lt;string.h%26gt;//needed for string copy


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


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


void startup();


void method();


int main()


{


startup();


method();


getch();


return 0;


}





void startup()


{


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


cout%26lt;%26lt;"jan 14, 2008"%26lt;%26lt;'\n';


cout%26lt;%26lt;"pg pro"%26lt;%26lt;'\n';


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


}


void method()


{


char stock[20];


int open;


int close;


int shares;


int final;


cout%26lt;%26lt;"Enter name ";


cin.get(stock, 20);


cin.ignore(80, '\n');





cout%26lt;%26lt;"enter open value ";


cin%26gt;%26gt;open;


cout%26lt;%26lt;"enter closing value ";


cin%26gt;%26gt;close;





cout%26lt;%26lt;"Enter number of stock you own";


cin%26gt;%26gt;shares;





final = close - open;





cout%26lt;%26lt;stock;


cout%26lt;%26lt;open;


cout%26lt;%26lt;close;


cout%26lt;%26lt;final;


}

Am i doing this c++ program right?
i took the time to debug it my self, heres the clean code....








#include %26lt;conio.h%26gt;// needed for getch


#include %26lt;iostream.h%26gt;//needed for cout and cin messages


#include %26lt;string.h%26gt;//needed for string copy


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


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


using namespace std;





void startup();


void method();


int main()


{


startup();


method();


getch();


return 0;


}





void startup()


{


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


cout %26lt;%26lt; "jan 14, 2008"%26lt;%26lt;'\n';


cout %26lt;%26lt; "pg pro"%26lt;%26lt;'\n';


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


}


void method()


{


char stock[20];


int open;


int close;


int shares;


int final;


cout %26lt;%26lt; "Enter name ";


cin.get(stock, 20);


cin.ignore(80, '\n');





cout %26lt;%26lt; "enter open value ";


cin %26gt;%26gt; open;


cout %26lt;%26lt; "enter closing value ";


cin %26gt;%26gt; close;





cout %26lt;%26lt; "Enter number of stock you own";


cin %26gt;%26gt; shares;





final = close - open;





cout %26lt;%26lt; stock;


cout %26lt;%26lt; open;


cout %26lt;%26lt; close;


cout %26lt;%26lt; final;


return 0;


}








DONT FORGET TO USE PROPER TABBING.
Reply:The program runs fine on my computer. There are a few problems, though:


-- No error checking when the user inputs values (what if "abc" is inputted for the open value?)


-- The output at the end is all on the same line without any spaces, so it's not really readable


-- You can use "endl" instead of '\n' when printing a newline with cout


-- You're not doing anything with the number of shares. Is this on purpose?


-- Why do you need "math.h"?
Reply:instead of


cout%26lt;%26lt;"blah blah"


try


cout%26gt;%26gt; "blah blah


but i dont know much about c++


No comments:

Post a Comment