Jump to content
  • Advertisement
Sign in to follow this  
Tradone

How Can I Avoid Repetitive Programming

This topic is 4548 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

is there like an alternative for this?
cin >> yes;
if ( yes == "yes" ){
  for(int i=0; i<=1000; i++){
    many many commands, including changing variables.
  }
}
else{
  for(int i=1000; i>=0; i--){
    exactly the same commands as above, but only in descending order.
  }
}


i want to only write it once! and I am having syntax error on the following code.
for (multimap<string, int, case_insensitive_test>::const_reverse_iterator const_it = multimap_order.rbegin() const; const_it != multimap_order.rend() const; ++const_it ){

or

for (multimap<string, int, case_insensitive_test>::const_reverse_iterator const_it = multimap_order.rbegin(); const_it != multimap_order.rend(); ++const_it ){

both end up as errors.

Share this post


Link to post
Share on other sites
Advertisement
I guess you can either do something like this:


char cBuffer = 'y';

for(int iIndex = cBuffer == 'y' ? 0 : 1000 ; cBuffer == 'y' ? iIndex <= 1000 : iIndex >= 0 ; cBuffer == 'y' ? iIndex ++ : iIndex --)
{
//slow though...
}







or write a function that does your stuff, and call it in the two for-loops...

Share this post


Link to post
Share on other sites
In order to help us help you... 1) What are you attempting to do with the "for" statements? (i.e. what are you trying to accomplish) 2) could you post the errors you're getting at those lines?

Share this post


Link to post
Share on other sites
You can do:

for (int i = 0; i <= 1000; i++)
{
j = i;
if (yes != "yes")
j = 1000 - i;

// ... use j instead of i in your code inside the for loop ...
}



You do suffer the comparison and possible branch penalty inside your loop for doing it this way, but you do only have to write your code once.

Maybe it might be better to use a #define?

Share this post


Link to post
Share on other sites

Here is a quick cheesy way to do the first:
cin >> yes;
int direction, start, end;
if ( yes == "yes" ){
direction = 1;
start = 0;
end = 1000;
} else {
direction = -1;
start = 1000;
end = 0;
}

for (int i = start; i != end; i += direction) {
// code
}


You could manage it with just direction, but that would be annoying.

Second question. First, please use typedefs and keep your lines shorter! Long lines are very much a write-only-code style.

Rewritten:
Quote:
typedef multimap<string, int, case_insensitive_test> my_map;
typedef my_map::const_reverse_iterator my_it;
for (my_it const_it = multimap_order.rbegin() const;
const_it != multimap_order.rend() const;
++const_it ){


or
typedef multimap<string, int, case_insensitive_test> my_map;
typedef my_map::const_reverse_iterator my_it;
for ( my_it const_it = multimap_order.rbegin();
const_it != multimap_order.rend();
++const_it ){


What is the type of multimap_order?

Share this post


Link to post
Share on other sites
Quote:

i want to only write it once!


Isn't that the reason functions we're invented in the 1950s?

Seriously though, what in the world are you talking about. Use a function, an inline function, a functor, a macro ... whatever. That's what a programming langauge is for, organizing reused code into blocks.

Share this post


Link to post
Share on other sites

multimap<string, int, case_insensitive_test> multimap_order;


this is the type.


This is line 3638:

for (multimap<string, int, case_insensitive_test>::const_reverse_iterator const_it = multimap_order.rbegin(); const_it != multimap_order.rend(); ++const_it ){


[2], int, std::string (*)[2], int, std::string)':
shenuadmin.cpp:3638: error: no match for 'operator!=' in 'const_it != std::multimap<_Key, _Tp, _Compare, _Alloc>::rend() [with _Key = std::string, _Tp = int, _Compare = case_insensitive_test, _Alloc = std::allocator<std::pair<const std::string, int> >]()'


and my question #1.

cin >> yes;
if ( yes == "yes" ){
for(int i=0; i<=1000; i++){
many many commands, including changing variables.
}
}
else{
for(int i=1000; i>=0; i--){
exactly the same commands as above, but only in descending order.
}
}


I thought writing a sample simple example would simplify the answer, but I guess not.


if ( sort_type == "descending" ){
for (multimap<string, int, case_insensitive_test>::const_iterator const_it = multimap_order.begin(); const_it != multimap_order.end(); ++const_it ){
//BLA BLA BLA
}
}
else{
for (multimap<string, int, case_insensitive_test>::const_reverse_iterator const_it = multimap_order.rbegin(); const_it != multimap_order.rend(); ++const_it ){
/// BLA BLA BLA
}
}



and in the BLABLABLA area,
I would like to put this in

Menu_Cfg="system/users/orders/";
Menu_Cfg+=ToString((*const_it).second);
Menu_Cfg+=".cgi";
count_order_flag++;
cout << " <TR BORDERCOLORDARK=\"WHITE\" BORDERCOLORLIGHT=\"#C6C3C6\" BORDERCOLOR=\"WHITE\" onClick='changeRow_order(this); change_OptionIconsOrder(\"" << (*const_it).second << "\", \"" << Temp_Select_All_Order_Number_Holder << "\", \"" << Temp_Select_All_Order_Holder << "\", \"type1\");' onDblClick='edit_order(\"" << (*const_it).second << "\");' id='changeme_order";
cout << (*const_it).second;
cout << "' style='cursor:default' onselectstart=\"return false\">";
cout << " <TD WIDTH=1 title=\"Marked Deletion\" NOWRAP>";
cout << " <INPUT TYPE=checkbox NAME='order_multidelete" << (*const_it).second << "' VALUE=1>";
cout << " </TD>";

cout << " <TD>";
if ( ReadPosition("status", Menu_Cfg) != "" ){
cout << "<IMG name=orderStatusIcon src=\"system/image/admin/menu/" << ReadPosition("status", Menu_Cfg) << ".gif\" width=16 height=16 align=absmiddle border=0>";
}
else{
cout << "&nbsp;";
}
cout << " </TD>";

cout << " <TD>";
cout << " &nbsp;" << ReadPosition("order_number", Menu_Cfg);
cout << " </TD>";

cout << " <TD>";
cout << " &nbsp;" << ReadPosition("customer_FirstName", Menu_Cfg);
cout << " </TD>";

cout << " <TD>";
cout << " &nbsp;" << ReadPosition("customer_LastName", Menu_Cfg);
cout << " </TD>";

cout << " <TD ALIGN=center>";
cout << " &nbsp;" << ReadPosition("total_items", Menu_Cfg);
cout << " </TD>";

cout << " <TD>";
cout << " &nbsp;<script>document.write(ToDigits(\"" << ReadPosition("subtotal", Menu_Cfg) << "\"));</script>";
cout << " </TD>";

cout << " <TD ALIGN=left>";
cout << " &nbsp;" << ReadPosition("status", Menu_Cfg);
cout << " </TD>";

cout << " <TD>";
cout << " &nbsp;" << ReadPosition("date", Menu_Cfg);
cout << " &nbsp;" << ReadPosition("time", Menu_Cfg);
cout << " </TD>";

Share this post


Link to post
Share on other sites
you are right, i should have used functions.
and I was thinking about functions, but just didn't know how that would be applied.

I think the best approach would be....


function new_function(string Menu_Cfg){
//bbla blla bbal
}

...
...
if( type=="ascending" ){
for( iterator.begin; iter.end; iter++ ){
Menu_Cfg="system/something";
Menu_Cfg+=iter.first();
Menu_Cfg+=".cgi";

new_function(Menu_Cfg);
}
}
else{
for( iterator.rbegin; iter.rend; iter++ ){
Menu_Cfg="system/something";
Menu_Cfg+=iter.first();
Menu_Cfg+=".cgi";

new_function(Menu_Cfg);
}
}



I'm such an idiot!
I was doubting myself before I even let my brain to think.

Share this post


Link to post
Share on other sites
Best (most elegant and short to write, but not super efficient) solution I can think of for your original problem:



bool yes;
...
//somewhere decide if yes is 1 or 0
...
for(int i=(1000*(yes^1)); (i*((yes)?1:-1))<=(1000*(yes)); i+=((yes)?1:-1)){
cout << i << "\t";
}









edit: an issue with <= just a sec... ... FIXED with (i*((yes)?1:-1))

[Edited by - M2tM on March 2, 2006 8:20:32 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!