C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old November 7th, 2009, 02:19 AM
Bogi88 Bogi88 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Location: nor cali
Posts: 2 Bogi88 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 6 sec
Reputation Power: 0
Send a message via Yahoo to Bogi88
Unhappy File IO - In program, getting error "declaration of 'int variable ' shadows a parameter"

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

Reply With Quote
  #2  
Old November 8th, 2009, 04:59 AM
MaHuJa's Avatar
MaHuJa MaHuJa is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Dec 2007
Posts: 1,177 MaHuJa User rank is Private First Class (20 - 50 Reputation Level)MaHuJa User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 1 Day 21 h 27 m 36 sec
Reputation Power: 8
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
Quote:
Originally Posted by Bogi88
"declaration of 'int probsPerSet ' shadows a parameter"

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;
}
With the corresponding change on the caller side, saying probsPerSet = getProbsPerSet();

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:
Programming by Coincidence
Fred types in some more code, tries it, and it still seems to work. [Then] the program suddenly stops working. [...] Fred doesnít know why the code is failing because he didnít know why it worked in the first place.
Undefined behavior results in: (worst to best)
-Erases your harddisk. Really.
-Appears to work - for now
-Delayed errors/crashes
-Crashes
-Compiler warning

Reply With Quote
  #3  
Old November 21st, 2009, 10:06 PM
Bogi88 Bogi88 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Location: nor cali
Posts: 2 Bogi88 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 6 sec
Reputation Power: 0
Send a message via Yahoo to Bogi88
Thank you for the help MaHuJa. i think now my only problem is that for each set of problems i need to calculate the number of correct problems. the thing is i cant send no more than 3 arguments to doOneSet:

int main()
{
srand(time(0));
getProbsPerSet(<arguments>);
doOneSet(<no-more-than-3-arguments>);
doOneSet(<no-more-than-3-arguments>);
doOneSet(<no-more-than-3-arguments>);
printReport(<arguments>);
}
the hint my teacher gave us was:

"Although main will need three separate variables to keep track of the number of problems answered correctly in each set, your doOneSet function will have only one parameter which keeps track of the number of problems answered correctly in the current set. It will be up to main to get that information into the correct variable. "

but I don't really know how that would work. any ideas? This is my code so far:


#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
void getProbsPerSet(int &probsPerSet);

void doOneSet(char problemType, int probsPerSet, int& numCorrect1);

void printReport(int probsPerSet, int numCorrect1, int numCorrect2, int numCorrect3);
int sum(int,int);
int sub(int,int);
int mul(int,int);

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;
}

void getProbsPerSet(int& probsPerSet)
{
cout<<"Enter problems per set: "<<endl;
cin>>probsPerSet;
}

void doOneSet(char problemType, int probsPerSet, int& numCorrect1)
{
int num1,num2,max,result,correct_output, count;
numCorrect1=0;
cout<<"What is the maximum number for this set? "<<endl;
cin>>max;
for(count=0;count<probsPerSet;count++)
{
num1=rand() % (max+1);
num2=rand() % (max+1);


switch (problemType)
{



case '+':cout<<num1<<" + "<<num2<<" = ";
cin>>result;
correct_output=sum(num1,num2);
if(result==correct_output){
numCorrect1++;
}
break;

case '-':cout<<num1<<" - "<<num2<<" = ";
cin>>result;
correct_output=sub(num1,num2);
if(result==correct_output){
numCorrect2++;
}
break;

case '*':cout<<num1<<" * "<<num2<<" = ";
cin>>result;
correct_output=mul(num1,num2);
if(result==correct_output){
numCorrect3++;
}
break;
}



}

}


int sum(int value1,int value2)
{
return value1+value2;
}
int sub(int value1,int value2)
{
return value1-value2;
}
int mul(int value1,int value2)
{
return value1*value2;
}

void 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;
}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > File IO - In program, getting error "declaration of 'int variable ' shadows a parameter"


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap