Friday, July 31, 2009

C++ simplification with main code (is there anyway to simplify my main function because there is repetition?

#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


using namespace std;


void head()


{


string a = "Alouette, gentille Alouette";


cout %26lt;%26lt;a %26lt;%26lt;endl %26lt;%26lt;"(" %26lt;%26lt;a %26lt;%26lt;")" %26lt;%26lt;endl;


}


void tail()


{cout %26lt;%26lt;"Alouette" %26lt;%26lt;endl %26lt;%26lt;"(Alouette)" %26lt;%26lt;endl %26lt;%26lt;"O-o-o-oh" %26lt;%26lt;endl;


}


void chorus1()


{


string a = "Et la tete";


cout %26lt;%26lt;a %26lt;%26lt;endl %26lt;%26lt;"(" %26lt;%26lt;a %26lt;%26lt;")" %26lt;%26lt;endl;


}


void chorus2()


{


string a = "Et le bec";


cout %26lt;%26lt;a %26lt;%26lt;endl %26lt;%26lt;"(" %26lt;%26lt;a %26lt;%26lt;")" %26lt;%26lt;endl;


}


void chorus3()


{


string a = "Et le cou";


cout %26lt;%26lt;a %26lt;%26lt;endl %26lt;%26lt;"(" %26lt;%26lt;a %26lt;%26lt;")" %26lt;%26lt;endl;


}


int main()


{


head(); cout %26lt;%26lt;"Je te plumerai la tete" %26lt;%26lt;endl %26lt;%26lt;"(Je te plumerai la tete)" %26lt;%26lt;endl; chorus1(); tail();


cout %26lt;%26lt;endl;


head(); cout %26lt;%26lt;"Je te plumerai le bec" %26lt;%26lt;endl %26lt;%26lt;"(Je te plumerai le bec)" %26lt;%26lt;endl; chorus2(); chorus1(); tail();


cout %26lt;%26lt;endl;


head(); cout %26lt;%26lt;"Je te plumerai le cou" %26lt;%26lt;endl %26lt;%26lt;"(Je te plumerai le cou)" %26lt;%26lt;endl; chorus3(); chorus2(); chorus1(); tail() %26lt;%26lt;endl;

C++ simplification with main code (is there anyway to simplify my main function because there is repetition?
I think you are looking for something more like this.





#include %26lt;iostream%26gt;


#include %26lt;string%26gt;





using namespace std;





void head()


{


string a = "Alouette, gentille Alouette";


cout %26lt;%26lt;a %26lt;%26lt;endl %26lt;%26lt;"(" %26lt;%26lt;a %26lt;%26lt;")" %26lt;%26lt;endl;


}





void tail()


{


cout %26lt;%26lt;"Alouette" %26lt;%26lt;endl %26lt;%26lt;"(Alouette)" %26lt;%26lt;endl %26lt;%26lt;"O-o-o-oh" %26lt;%26lt;endl;


}








void chorus(string str)


{


cout %26lt;%26lt; str %26lt;%26lt; endl %26lt;%26lt; "(" %26lt;%26lt; str %26lt;%26lt;")" %26lt;%26lt;endl;


}











int main()


{


const int ROUNDS = 3;





string strArr1[ROUNDS] = {"Et la tete", "Et le bec", "Et le cou"};


string strArr2[ROUNDS] = {"la tete", "le bec", "le cou"};





for (int i = 0; i %26lt; ROUNDS; ++i)


{


head();





cout %26lt;%26lt; "Je te plumerai " %26lt;%26lt; strArr2[i] %26lt;%26lt; endl


%26lt;%26lt; "(Je te plumerai " %26lt;%26lt; strArr2[i] %26lt;%26lt; ")" %26lt;%26lt; endl;





for (int j = i; j %26gt;= 0; --j)


{


chorus(strArr1[j]);


}





tail();





cout %26lt;%26lt;endl;


}





cout %26lt;%26lt; endl;


}
Reply:you r making alot of function why you didn't replace it in the main and arrange your main and add comments to indicate what you want


that will be useful than very small functions like this which only make confusion


No comments:

Post a Comment