| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hello,
this is my first post on the forums. I'm doing an assignment for my first ever C++ class and am getting the error: "declaration of 'int probsPerSet ' shadows a parameter" I'm not quite sure how to fix it and was wondering if someone could help me out. I also wanted to ask if the part of my program in which the symbols change for the given "set" of problems was correct. So below is my code and what the output "should" look like. This program first asks the user how many problems they want in each set (there is a defined number of 3 sets). Then the user is asked to choose a max number to which the randomly generated numbers may go up to. I had to limit the number to 100. The program outputs problems and the user is asked to give an answer. The first set is all addition, the second is subtraction, and the third is multiplication. The program calculates the correct answer and compares it to the user's input. At the end, the program outputs the number and percentages of each: the first set, second set, third set, and overall. CODE: #include <iostream> #include <iomanip> #include <cstdlib> #include <ctime> using namespace std; int getProbsPerSet(int); int printReport(int, int, int, int); int doOneSet (int, int, int); int sum(int,int); //function prototype int main() { int probsPerSet; int numCorrect1; int numCorrect2; int numCorrect3; srand(time(0)); getProbsPerSet(probsPerSet); doOneSet('+', probsPerSet, numCorrect1); doOneSet('-', probsPerSet, numCorrect2); doOneSet('*', probsPerSet, numCorrect3); printReport(probsPerSet,numCorrect1,numCorrect2,nu mCorrect3); system("PAUSE"); return 0; } int getProbsPerSet(int probsPerSet) { cout<<"Enter problems per set: "<<endl; cin>>probsPerSet; } int doOneSet (int symbol, int probsPerSet, int numCorrect1) { int num1,num2,max,result,correct_output, numCorrect=0, probsPerSet, count; cout<<"What is the maximum number for this set? "<<endl; cin>>max; for(count=0;count<probsPerSet;count++) { // max++; num1=rand() % (max+1); num2=rand() % (max+1); cout<<num1<<symbol<<num2<<" = "; cin>>result; correct_output=sum(num1,num2);//function call if(result==correct_output) numCorrect++; } } int sum(int value1,int value2) { return value1+value2; } int printReport(int probsPerSet, int numCorrect1, int numCorrect2, int numCorrect3) { int percCorrct1, percCorrct2, percCorrct3, TotalPercCorr, TotalCorrect, TotalProbs; percCorrct1 = numCorrect1/probsPerSet; percCorrct2 = numCorrect2/probsPerSet; percCorrct3 = numCorrect3/probsPerSet; TotalCorrect = numCorrect1 + numCorrect2 + numCorrect3; TotalProbs = 3*probsPerSet; TotalPercCorr = TotalCorrect/TotalProbs; cout <<"Set#1: You got " <<numCorrect1<<" correct out of " <<probsPerSet<< "for" <<percCorrct1<<endl; cout <<"Set#2: You got " <<numCorrect2<<" correct out of " <<probsPerSet<< "for" <<percCorrct2<<endl; cout <<"Set#3: You got " <<numCorrect3<<" correct out of " <<probsPerSet<< "for" <<percCorrct3<<endl; cout <<"Overall you got " <<TotalCorrect<<" correct out of " <<TotalProbs<< "for" <<TotalPercCorr <<endl; } Desired sample OUTPUT: Enter problems per set: 3 Set #1 ---------- What is the maximum number for this set? 100 45 + 21 = 66 correct 0 + 100 = 100 correct 54 + 23 = 67 incorrect Set #2 ---------- What is the maximum number for this set? 90 59 - 19 = 40 correct 19 - 84 = -29 incorrect 0 - 65 = -65 correct Set #3 ---------- What is the maximum number for this set? 20 0 * 18 = 0 correct 15 * 4 = 398 incorrect 8 * 17 = 873 incorrect Set#1: You got 2 correct out of 3 for 67% Set#2: You got 2 correct out of 3 for 67% Set#3: You got 1 correct out of 3 for 33% Overall you got 5 correct out of 9 for 56% Thank you in advance |
|
#2
|
||||
|
||||
|
Quote:
Here's what I get (from Visual Studio 2010 Beta 2) error C2082: redefinition of formal parameter 'probsPerSet' Remove it from the list of int variables inside the function. ---- Code:
int getProbsPerSet(int probsPerSet)
{
cout<<"Enter problems per set: "<<endl;
cin>>probsPerSet;
}
error C4716: 'getProbsPerSet' : must return a value Moreover, the value read is discarded. What you want is either passing the variable by reference void getProbsPerSet(int& probsPerSet) so the function can change the original, instead of getting handed a copy. Or to get rid of the parameter entirely: Code:
int getProbsPerSet()
{
int probsPerSet;
cout<<"Enter problems per set: "<<endl;
cin>>probsPerSet;
return probsPerSet;
}
The last is the most common of the two, the former is usually only used where a) coding rules dictate that every (or at least this particular) function returns an error code rather than a value. In which case the return type is not void, of course. b) The value the function would change is big, and expensive to copy to its new destination, and one does not want to rely on the compiler doing a Return Value Optimization. This is also the case with the rest of your functions. For each of the four variables in main() I get warning C4700: uninitialized local variable 'numCorrect1' used ---- I noted that you use a "define all variables at the top of the function" style. This was required in an old version of C, but not in modern compilers where you're essentially telling the compiler not to reuse the stack memory. Code:
int main()
{
srand(static_cast<unsigned int>(time(0)));
int probsPerSet = getProbsPerSet();
int numCorrect1 = doOneSet('+', probsPerSet);
int numCorrect2 = doOneSet('-', probsPerSet);
int numCorrect3 = doOneSet('*', probsPerSet);
---- Code:
int sum(int value1,int value2)
{
return value1+value2;
}
This one deserves a section all to itself. This kind of thing is often featured on thedailywtf.com This is bad because: a) it may hamper performance b) it's not obvious on the call site that this is all it does (if it is, why not write x+y to begin with? c) it's verbose for no reason. There are reasons to use them, such as when a template function takes a function parameter. It may start having some kind of purpose if you add a third parameter for which operation (+-*/) it should do, but at that point you should rename it to reflect that. ---- doOneSet('+', //doOneSet (int, int, int); A char is an 8-bit int, but is treated specially when it comes to printing it. When you pass it to a function as an int parameter, you lose that property.
__________________
Quote:
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > File IO - In program, getting error "declaration of 'int variable ' shadows a parameter" |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|