Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Help with Static Variables

Help with Static Variables

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    I'm trying to pass my local variables of one function to another fuction, all within the main. I get errors saying that the variables score1-score5 are undeclared. Here is what I'm trying to do so far: #include using namespace std; void getvalues(); void findlowest(); void calcaverage(); int main() { getvalues(); return 0; } void getvalues() { static int score1, score2, score3, score4, score5=0; cout << "What is score 1? "; cin >> score1; cout << "What is score 2? "; cin >> score2; cout << "What is score 3? "; cin >> score3; cout << "What is score 4? "; cin >> score4; cout << "What is score 5? "; cin >> score5; findlowest(); } void findlowest() { if (score1 < score2 && score1 < score3 && score1 < score4 && score1 < score5) { return score1; } if (score2 < score1 && score2 < score3 && score2 < score4 && score2 < score5) { return score2; } if (score3 < score1 && score3 < score2 && score3 < score4 && score3 < score5) { return score3; } if (score4 < score1 && score4 < score2 && score4 < score3 && score4 < score5) { return score4; } if (score5 < score1 && score5 < score2 && score5 < score3 && score5 < score4) { return score5; } }

    W T R 3 Replies Last reply
    0
    • L Lost User

      I'm trying to pass my local variables of one function to another fuction, all within the main. I get errors saying that the variables score1-score5 are undeclared. Here is what I'm trying to do so far: #include using namespace std; void getvalues(); void findlowest(); void calcaverage(); int main() { getvalues(); return 0; } void getvalues() { static int score1, score2, score3, score4, score5=0; cout << "What is score 1? "; cin >> score1; cout << "What is score 2? "; cin >> score2; cout << "What is score 3? "; cin >> score3; cout << "What is score 4? "; cin >> score4; cout << "What is score 5? "; cin >> score5; findlowest(); } void findlowest() { if (score1 < score2 && score1 < score3 && score1 < score4 && score1 < score5) { return score1; } if (score2 < score1 && score2 < score3 && score2 < score4 && score2 < score5) { return score2; } if (score3 < score1 && score3 < score2 && score3 < score4 && score3 < score5) { return score3; } if (score4 < score1 && score4 < score2 && score4 < score3 && score4 < score5) { return score4; } if (score5 < score1 && score5 < score2 && score5 < score3 && score5 < score4) { return score5; } }

      W Offline
      W Offline
      wb
      wrote on last edited by
      #2

      Hi! first, you dont pass the local variables declared in main() to the findlowest() function. you have to declare it like this

      int findlowest(int score1,int score2 ... int score5);

      and I think, the minimum search can be done better:

      if(score2 < score1)
      score1=score2;
      if(score3 < score1)
      score1=score3;
      if(score4 < score1)
      score1=score4;
      if(score5 < score1)
      score1=score5;
      return score1;

      1 Reply Last reply
      0
      • L Lost User

        I'm trying to pass my local variables of one function to another fuction, all within the main. I get errors saying that the variables score1-score5 are undeclared. Here is what I'm trying to do so far: #include using namespace std; void getvalues(); void findlowest(); void calcaverage(); int main() { getvalues(); return 0; } void getvalues() { static int score1, score2, score3, score4, score5=0; cout << "What is score 1? "; cin >> score1; cout << "What is score 2? "; cin >> score2; cout << "What is score 3? "; cin >> score3; cout << "What is score 4? "; cin >> score4; cout << "What is score 5? "; cin >> score5; findlowest(); } void findlowest() { if (score1 < score2 && score1 < score3 && score1 < score4 && score1 < score5) { return score1; } if (score2 < score1 && score2 < score3 && score2 < score4 && score2 < score5) { return score2; } if (score3 < score1 && score3 < score2 && score3 < score4 && score3 < score5) { return score3; } if (score4 < score1 && score4 < score2 && score4 < score3 && score4 < score5) { return score4; } if (score5 < score1 && score5 < score2 && score5 < score3 && score5 < score4) { return score5; } }

        T Offline
        T Offline
        twing
        wrote on last edited by
        #3

        Please check your grammar book. Give your fun some parameters to need! Hello World!

        1 Reply Last reply
        0
        • L Lost User

          I'm trying to pass my local variables of one function to another fuction, all within the main. I get errors saying that the variables score1-score5 are undeclared. Here is what I'm trying to do so far: #include using namespace std; void getvalues(); void findlowest(); void calcaverage(); int main() { getvalues(); return 0; } void getvalues() { static int score1, score2, score3, score4, score5=0; cout << "What is score 1? "; cin >> score1; cout << "What is score 2? "; cin >> score2; cout << "What is score 3? "; cin >> score3; cout << "What is score 4? "; cin >> score4; cout << "What is score 5? "; cin >> score5; findlowest(); } void findlowest() { if (score1 < score2 && score1 < score3 && score1 < score4 && score1 < score5) { return score1; } if (score2 < score1 && score2 < score3 && score2 < score4 && score2 < score5) { return score2; } if (score3 < score1 && score3 < score2 && score3 < score4 && score3 < score5) { return score3; } if (score4 < score1 && score4 < score2 && score4 < score3 && score4 < score5) { return score4; } if (score5 < score1 && score5 < score2 && score5 < score3 && score5 < score4) { return score5; } }

          R Offline
          R Offline
          rrrado
          wrote on last edited by
          #4

          Isn't this some joke ? Ok, first of all you shold study something about arrays and loops. Like

          int score[5];
          ...
          min = GetMin(score,sizeof(score)/sizeof(score[0]));
          ....
          int GetMin(int *score,int count)
          {
          int i,min = score[0];
          for (i=1; i<count; i++)
          if (min>score[i])
          min = score[i];
          return min;
          }


          rrrado

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups