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, 03:19 AM
Bogi88 Bogi88 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Location: nor cali
Posts: 1 Bogi88 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 33 m 41 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, 05:59 AM
MaHuJa MaHuJa is offline
Contributing User
Click here for more information.
 
Join Date: Dec 2007
Posts: 880 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: 6 Days 19 h 48 m 25 sec
Reputation Power: 2
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.

Reply With Quote
Reply

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


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




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

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




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek