Thursday, July 30, 2009

I need a very basic C++ program thats ask 1 question and echos the answer?

I need a c++ program that ask


What is Your favorite color?


and then takes the users color and says


(The color goes here) is very nice.


I'm a beginner at programing and i need it for beginners C++.


So Far I Have


#include%26lt;iostream%26gt;


as u can see I'm lost. It has to be very simple cuz it's our 2nd day of class THANK YAHOO ANSWERS

I need a very basic C++ program thats ask 1 question and echos the answer?
Starting programming can be difficult until you learn that ALL programs start with some variation of the "Hello, World!" program.





First start with your basic program:





#include %26lt;iostream%26gt;


using namespace std;


int main(int argc, char *argv[])


{


 cout %26lt;%26lt; "Hello, World!\n";


 return(0);


}





This gets you a working program with output. Now make incremental changes to move to your target behavior. Start with asking the question:





#include %26lt;iostream%26gt;


using namespace std;


int main(int argc, char *argv[])


{


 cout %26lt;%26lt; "What is Your favorite color?\n";


 return(0);


}





Now add in reading in the answer into a string variable:





#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


using namespace std;


int main(int argc, char *argv[])


{


 cout %26lt;%26lt; "What is Your favorite color?\n";


 string color;


 cin %26gt;%26gt; color;


 return(0);


}





Now do something with the user's response:





#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


using namespace std;


int main(int argc, char *argv[])


{


 cout %26lt;%26lt; "What is Your favorite color?\n";


 string color;


 cin %26gt;%26gt; color;


 cout %26lt;%26lt; color %26lt;%26lt; " is very nice.\n";


 return(0);


}





And there you have it. The trick is to start with a working program. Any working program. Then munge it until you have what you want. Don't try and write the whole thing in one shot as it won't compile (at least until you get some experience!) and when a single change breaks the program it is easy to know where to debug it.





Good luck!
Reply:Wonderful idea to print it out. Report It

Reply:this should work





#include %26lt;iostream%26gt;





int main()


{


char* colorchoice;


cout%26lt;%26lt;"Type in you favorite color"%26lt;%26lt;endl;


cin%26gt;%26gt;colorchoice;


cout%26lt;%26lt;colorchoice%26lt;%26lt;"is very nice"%26lt;%26lt;endl;


system("pause");


}
Reply:do your own homework for school :P
Reply:er.. go read your book





#include %26lt;iostream%26gt;


using namespace std;





int main() {


char userInput[81];


cout %26lt;%26lt; "What is your favorite color?


cin.getLine(userInput,80);


cout %26lt;%26lt; userInput " is a very nice."


return 0;


}





====


don't blame me if it doesn't work, writing on the top of my head
Reply:#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


using namespace std;





void main() {


string text;


getline(cin, text);


cout %26lt;%26lt; text.c_str();


}


No comments:

Post a Comment