Results 1 to 7 of 7

Thread: Whats wrong with this code?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2002
    Location
    Kentucky
    Posts
    29

    Whats wrong with this code?

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    ////////////////////////////////
    int StrStat(int Str1, int Str2, int Str3)
    {
    	Str1 = (rand() % 6) + 1;
    	Str2 = (rand() % 6) + 1;
    	Str3 = (rand() % 6) + 1;
    	int StrTotal;
    	StrTotal = 0;
    	StrTotal = Str1 + Str2 + Str3;
    	while(StrTotal<8)
    	{
    		StrTotal = Str1 + Str2 + Str3;
    	}
    	return StrTotal;
    }
    /////////////////////////////////
    int main()
    {
    	cout<<StrStat(Str1, Str2, Str3);
    	return 0;
    }
    StrStat is a function calling for the Str1, Str2, and Str3 parameters, all of which are defined in the function. Then, the total of those are returned in the variable StrTotal, which is also the return value of the function. Well, the problem occurs when I call the function in the program. cout<<StrStat(Str1, Str2, Str); gives me undeclared identifiers. My question is why does it do that, when they are obviously declared in the function? I'm kinda new to functions you could say, because the only functions I did were add and subtract, heh, and I apparently cant even remember how to do those. Please help, thank you.

  2. #2
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    ////////////////////////////////
    int StrStat()//no need for arguement since you are not passing any to this function
    {
      int Str1 = (rand() % 6) + 1;
      int Str2 = (rand() % 6) + 1;
      int Str3 = (rand() % 6) + 1;
      int StrTotal;
      StrTotal = 0;
      StrTotal = Str1 + Str2 + Str3;
      while(StrTotal<8)
      {
        StrTotal = Str1 + Str2 + Str3;
      }
      return StrTotal;
    }
    /////////////////////////////////
    int main()
    {
      cout<<StrStat();
      return 0;
    }

  3. #3
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    What are you trying to do here? It has no effect at all
    Code:
    StrTotal = Str1 + Str2 + Str3;
      while(StrTotal<8)
      {
        StrTotal = Str1 + Str2 + Str3;
      }
    Why don't you just do this.

    Code:
    StrTotal = Str1 + Str2 + Str3;

  4. #4
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Also, just to make sure you know, you can initialize variables while you declare them:

    Code:
    int StrTotal;
      StrTotal = 0;
    
    // Can also be written as:
    
    int iTotal = 0;
    Please note that you're using a prefix for the variable data type, but you're using Str which means string. so if anyone was using your code and wouldn't pay attention to the parameters it would suspect it was a string. It's all up to you, but to me it seems better if you'd used the correct prefix (i for int, or just int, I don't care, as long as someone understands what kinda data type the variable is)
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2002
    Location
    Kentucky
    Posts
    29

    Thanks guys

    I got the actual program working, so thank you.

    Sorry about using StrStat, but I dont normally use prefixes for my variables. I work alone (lol) so it is really only me and you guys who see it, and since my programs are all relatively small it shouldnt be hard for you to figure out. Sorry, again. And thanks.

  6. #6
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Please, no sorries It doesn't matter as long as you know what it does, but it does matter when you're working for a team or applying for a job, it'd be better if you'd used the right prefixes
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Or when you suddenly want to change or extend the program a few months later.

    Code:
    StrTotal = Str1 + Str2 + Str3;
    while(StrTotal<8)
    {
    	StrTotal = Str1 + Str2 + Str3;
    }
    Since the body of the while doesn't change the value of StrTotal (because Str1, 2 and 3 don't change) this loop will loop forever once it is entered.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width