|
-
Jan 10th, 2003, 12:03 AM
#1
Thread Starter
Junior Member
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.
-
Jan 10th, 2003, 05:27 AM
#2
Hyperactive Member
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;
}
-
Jan 10th, 2003, 05:30 AM
#3
Hyperactive Member
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;
-
Jan 10th, 2003, 06:48 AM
#4
Frenzied Member
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.
-
Jan 10th, 2003, 09:22 AM
#5
Thread Starter
Junior Member
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.
-
Jan 11th, 2003, 09:25 AM
#6
Frenzied Member
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.
-
Jan 11th, 2003, 12:51 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|