Tuesday, July 28, 2009

C++ help! can please me with C++! i am just learning!?

this program should be an IF statement that displays the string "Firebird" when the user enters the letter F (in any case).





this is my code, but it's not working! can someone tell me what am i doing wrong?





#include %26lt;iostream%26gt;


#include %26lt;algorithm%26gt;


#include %26lt;iomanip%26gt;





using std::cout;


using std::cin;


using std::endl;


using std::transform;


using std::fixed;


using std::setprecision;





int main()


{


//declare variable


char letter = 'F';


string word = "";








//enter input


cout %26lt;%26lt; "Enter a letter: ";


cin %26gt;%26gt; letter;





//convert letter to uppercase


transform(letter.begin(), letter.end(), letter.begin(), toupper);





//display output


if (letter == 'F')


word = Firebird;


//end if





cout %26lt;%26lt; "Word: " %26lt;%26lt; word %26lt;%26lt; endl;


return 0;


} //end of main function

C++ help! can please me with C++! i am just learning!?
why are you making it so complicated?


it's a very simple program:





#include %26lt;iostream%26gt;


#include %26lt;string%26gt;





using namespace std;





int main()


{


char letter='f';


string word="";





cout %26lt;%26lt; "Enter a letter: ";


cin %26gt;%26gt; letter;





//display output


if ((letter == 'F') || (letter =='f'))


word="Firebird";


else


word="N/A";





cout %26lt;%26lt; "Word: " %26lt;%26lt; word %26lt;%26lt; endl;





return 0;


}





Replace N/A with whatever is appropriate. Some other #include-s may be necessary to successfully compile this program.





Good luck!
Reply:int main()


{


char letter;


cout %26lt;%26lt;"Enter Letter: ";


cin %26gt;%26gt; letter;


if (letter == 'F' || letter == 'f')


cout %26lt;%26lt;"\nWord: Firebird"%26lt;%26lt;endl;


return (0);


}





you only need to include iostream


and


using namespace std;
Reply:First off you don't need


"#include %26lt;algorithm%26gt;


#include %26lt;iomanip%26gt;"


You can just delete those





"using std::cout;


using std::cin;


using std::endl;


using std::transform;


using std::fixed;


using std::setprecision;"


All these things can be replaced with "Using namespace std;"





Since the progarm is asking you for "letter f" you don't need a string, you can use char f = 'f';





Now you can ask for user input





cout %26lt;%26lt; "Enter a letter: ";


cin %26gt;%26gt; letter;





You don't really need "transform(letter.begin(), letter.end(), letter.begin(), toupper);"





Now write an if statement, to decide if it is a F or f





if (letter == 'F' || letter == 'f')





"word = Firebird;"


This is not valid, you didn't create any variable for "word"


instead, you can use "cout %26lt;%26lt; "Firebird";





Now you need an else statement


If letter isnt F or f then do this... %26lt;something%26gt;





Now if you were to put everything together, it will look like this





#include %26lt;iostream%26gt;


using namespace std;





int main()


{





char letter = 'F';


// declare variables





cout %26lt;%26lt; "Enter a letter: ";


// ask for user input





cin %26gt;%26gt; letter;


// user inputs something





if (letter == 'F' || letter == 'f')


cout %26lt;%26lt; "Firebird";


else


cout %26lt;%26lt; "Word: " %26lt;%26lt; letter %26lt;%26lt; endl;


// an if statement, IF letter is F or f then display Firebird, if it is not, then display the word





return 0;


}
Reply:Firebird should be quoted thus it is a string.


No comments:

Post a Comment