6 Replies - 282 Views - Last Post: 12 February 2014 - 07:49 AM Rate Topic: -----

#1 Night Stalker  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 58
  • Joined: 26-December 13

Simple while loop

Posted 11 February 2014 - 10:32 PM

Hello gentlemen and ladies. The program below yields no warnings, no errors. If I un-comment while statement and the closing brace at the end of main, it still runs with no warnings or errors but does not print any information. Nor does it seem to loop. While you are skipping through stupid with my code, if you think of a better technique (pick one) and wish to share, please feel free. As always, I appreciate you all, and I thank you.


#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;

class roller {
public:

    void stat (int& a,int val) {
        a = (rand() % val) + 5;
    }
};
int highlow(int,int);

int main() {

    srand(time(NULL));
    int total,temp,Intellect,Knowledge=0;
    int Physique, Agility, Stamina, Charisma=0;

    roller roll;

    // while ( total<26 ) {

        roll.stat(Intellect,17);                       // roll stat(+5)
        cout << "Intellect " << Intellect << " ";      // print rolled stat to screen
        temp=highlow(Intellect,17);                    // return results into temp
        total = total+temp;                            // add to total value

        roll.stat(Knowledge,20);
        cout << "Knowledge " << Knowledge << " ";
        temp=highlow(Knowledge,20);
        total = total+temp;

        roll.stat(Physique,10);
        cout << "Physique  " << Physique << " ";
        temp=highlow(Physique,10);
        total = total+temp;

        roll.stat(Agility,10);
        cout << "Agility   " << Agility << " ";
        temp=highlow(Agility,10);
        total = total+temp;

        roll.stat(Stamina,21);
        cout << "Stamina   " << Stamina << " ";
        temp=highlow(Stamina,21);
        total = total+temp;

        roll.stat(Charisma,18);
        cout << "Charisma  " << Charisma << " ";
        temp=highlow(Charisma,18);
        total = total+temp;

        cout << "Total roll value " << total << endl;        // print total value


    //}                                                       // completes the while loop
    return 0;

}                                                            // end of main function





int highlow(int stat,int comp) {
    int num=0;
    if (stat<comp) {
        cout << "\tStat is low." << endl;
        num=0;
    } else {
        if (stat==comp) {
            cout <<"\tStat is average. "<< endl;
            num=1;
        } else {
            if (stat==comp+1) {
                cout << "\tStat is good." << endl;
                num=2;
            } else {
                if (stat==comp+2) {
                    cout << "\tStat is very good." << endl;
                    num=3;
                } else {
                    if (stat==comp+3) {
                        cout << "\tStat is outstanding." << endl;
                        num=4;
                    } else {
                        if (stat==comp+4) {
                            cout << "\tStat is awesome." << endl;
                            num=5;
                        } else {
                            cout << "\tStat is excellent." << endl;
                            num=6;
                        }
                    }
                }
            }
        }
    }
    return num;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Simple while loop

#2 Black_Epiphany  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 66
  • Joined: 02-February 14

Re: Simple while loop

Posted 12 February 2014 - 12:01 AM

Line 18 and 19 you didn't set values for the other variables.

This post has been edited by Black_Epiphany: 12 February 2014 - 12:04 AM

Was This Post Helpful? 1
  • +
  • -

#3 Night Stalker  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 58
  • Joined: 26-December 13

Re: Simple while loop

Posted 12 February 2014 - 12:29 AM

Quote

Line 18 and 19 you didn't set values for the other variables.


To what other variables are you referring? Line 18 and 19 are in the main and all the local variables used are accounted for, and assigned the value of zero.

The function highlow() has it's own variable, and are set accordingly. Please expand, because I am missing it.
Was This Post Helpful? 0
  • +
  • -

#4 tarmizi_adam2005  Icon User is online

  • جوروترا

Reputation: 245
  • View blog
  • Posts: 892
  • Joined: 18-April 09

Re: Simple while loop

Posted 12 February 2014 - 03:17 AM

Yes,

You didn't set the other variables with initial values. You only made Knowledge=0 and Charisma=0. The other variables should also be set to some value maybe to 0 like this:

int total = 0, temp =0, Intellect=0,Knowledge=0;
int Physique =0, Agility=0, Stamina=0, Charisma=0;


so the increment that you use:

total = total+temp;


will add up.

This post has been edited by tarmizi_adam2005: 12 February 2014 - 03:17 AM

Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,433
  • Joined: 25-December 09

Re: Simple while loop

Posted 12 February 2014 - 06:50 AM

Also the following snippet can be "simplified" slightly to make the logic easier to follow:
int highlow(int stat,int comp) {
    int num=0;
    if (stat<comp) {
        cout << "\tStat is low." << endl;
        num=0;
    } else {
        if (stat==comp) {
            cout <<"\tStat is average. "<< endl;
            num=1;
        } else {
            if (stat==comp+1) {
                cout << "\tStat is good." << endl;
                num=2;
            } else {
                if (stat==comp+2) {
                    cout << "\tStat is very good." << endl;
                    num=3;
                } else {
                    if (stat==comp+3) {
                        cout << "\tStat is outstanding." << endl;
                        num=4;
                    } else {
                        if (stat==comp+4) {
                            cout << "\tStat is awesome." << endl;
                            num=5;
                        } else {
                            cout << "\tStat is excellent." << endl;
                            num=6;
                        }
                    }
                }
            }
        }
    }
    return num;
}


Even though your indentation is correct by switching to if else() clauses you can eliminate some of the indentation making the code clearer IMO.

int highlow(int stat,int comp) {
   int num = 0;
   if (stat < comp) {
      cout << "\tStat is low." << endl;
      num = 0;
   } else if (stat == comp) {
      cout <<"\tStat is average. "<< endl;
      num = 1;
   } else if (stat == comp + 1) {
      cout << "\tStat is good." << endl;
      num = 2;
   } else if (stat == comp + 2) {
      cout << "\tStat is very good." << endl;
      num = 3;
   } else if (stat == comp + 3) {
      cout << "\tStat is outstanding." << endl;
      num=4;
   } else if (stat == comp + 4) {
      cout << "\tStat is awesome." << endl;
      num = 5;
   } else {
      cout << "\tStat is excellent." << endl;
      num = 6;
   }

   return num;
}



Also don't forget that vertical whitespace can be as important as horizontal whitespace to make the code easier to read, note the spaces between the calculations.

Also many people today recommend making your function prototype match your function implementation by including the names of the variables. When you use meaningful parameter names this helps document the code, especially when you start writing multiple file programs.

Quote

so the increment that you use:

total = total+temp;

Or alternatively you could use:
total += temp; 



Jim

This post has been edited by jimblumberg: 12 February 2014 - 06:52 AM

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,463
  • Joined: 05-May 12

Re: Simple while loop

Posted 12 February 2014 - 07:26 AM

And that highlow() function can be simplified even more...
int highlow(int stat,int comp) {
   static const char * remarks[] = {
           "Stat is low.",
           "Stat is average.",
           "Stat is good.",
           "Stat is very good.",
           "Stat is outstanding.",
           "Stat is awesome.",
       };

   int num = 0;
   int delta = comp - stat;

   switch(delta) {
      case 0:
      case 1:
      case 2:
      case 3:
      case 4:
          num = delta + 1;
          break;

      default:
          num = (delta < 0) ? 0 : 6;
          break;
   }

   cout << "\t" << remarks[num] << endl;
   return num;
}



Basically it takes advantage of the relationship of num to the delta between the comp and the stat. Additionally, it uses a data driven approach to looking up the appropriate remark.
Was This Post Helpful? 0
  • +
  • -

#7 Night Stalker  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 58
  • Joined: 26-December 13

Re: Simple while loop

Posted 12 February 2014 - 07:49 AM

Thank you all very much for your input. I was under the impression using multiple variables on a single line as I had it would set them all to zero. I'll reread that section as well. I appreciate your responses. Thank you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1