English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

// This program by Dillon Nicholson gets numbers by the user
// then counts the number of digits in the number either being both
// positive or negitive whole numbers

#include
#include
using namespace std;

void countingFunction();

int main()
{
ofstream out;
out.open("output.txt");

int num;

out<<"This is the program of : ";
out<<"Dillon Nicholson"<<"\n";
out<<"CMPS 1043-02"<<"\n";
out<<"Due date:";
out<<" Nov 15 2007"<<"\n"<<"\n";

countingFunction();

out.close();
return 0;
}

void countingFunction()

{

while (num != 0)
if (num < 0) num*=-1;
count = 1;
else if (num >= 10)
{count++;
num= num / 10;
out>> num;
}





tell me whats wrong i cant tell but it wont build right says 'num' : unreferenced local variable on line 16 but theres nothing wrong that i see

2007-11-14 18:27:15 · 5 answers · asked by Dillon N 1 in Computers & Internet Programming & Design

so can any one tell me what to add to fix it

2007-11-14 18:37:53 · update #1

5 answers

you declare your "num" variable outside main()
like:

int num = 0;

int main()
{
ofstream out;
out.open("output.txt");


hope this helps

2007-11-14 18:48:53 · answer #1 · answered by --jrom-- 3 · 2 0

I'm pretty sure you're seeing two different errors in your code. One will say "Unreferenced local variable" and be pointing to your "int num;" line. The other will say something to the effect that 'num' is not a valid type or is unknown, and it'll be pointing to your "while(num!=0)" line.

The problem here is because of 'scope'. Scope is basically what a particular part of the code has access to, as far as variables are concerned. What you've done is you've declared your variable 'num' in main, and then you're attempting to use that variable from your countingFunction(). countingFunction() doesnt share scope with main, so it cant see that variable.

You need to move 'num' down to be in countingFunction().

You'll also need to somehow get the "ofstream out" variable to be available in countingFunction(). You can do this in two ways... One is to pass that variable as a parameter to the function, and the other is to make that variable global. To make it global, just move it out of and before the main() function. I'd recommend using a parameter, and passing it by reference, if you know how to do that. :)

Good luck!

2007-11-16 05:55:28 · answer #2 · answered by Anonymous · 0 0

Your function main is returning a value but it isn't defined as returning a value.

No idea about the undefined variable.

2007-11-14 18:36:46 · answer #3 · answered by Lorenzo H 3 · 0 1

It doesn't look like you are passing the users input to the variables in Main Of course I could be wrong but this is how I would do it. int Main () { int width, length, area; //call first function getLength() // your function here simply needs to pass the users input to length in main getWidth() // same thing as getLength getArea(length, width) // this function you need to pass length and width to and pass Area to the area in main displayData(length, width, area)// I don't see this function but all you need to do it output the variables passed to it return 0; } int getLength() { cout << "Please Enter the length" <> length; return length; } int getArea(int l1 , int w1 ) { int area; area = l1 * w1; cout << "The Area is " << area <

2016-05-23 05:58:39 · answer #4 · answered by ? 3 · 0 0

first declare the variables then use it

2007-11-14 18:30:59 · answer #5 · answered by Anonymous · 0 1

fedest.com, questions and answers