0

What do I need to do to fix the uninitialized error on my program? The program is suppose to print the number of divisors a number has but keeps displaying an error saying the variable c is uninitialized.

/*
/ Name: Ralph Lee Stone
/ Date: 10/10/2013
/ Project: RLStone3_HW_08
/ Description: This program gets a positive value and determines the number of divisors    it has.
*/
#include <iostream>
using namespace std;

int DivisorCount(int x)
{
  int counter = 0;
  for(int c; x < c; c++)
  {
    if (x%c==0)
     counter++;
  }
  return counter;
}
int main()
{
  int x;
  cout << "Please a positive value: ";
  cin >> x;
  cout << x << "has this many divsors: " << DivisorCount(x);
}
pippin1289
  • 4,861
  • 2
  • 22
  • 37

5 Answers5

5

You need to initialize it with some value:

for(int c = 1; x < c; c++)
//        ^^^ here

also, did you mean to write c < x instead of x < c?

Daniel Frey
  • 55,810
  • 13
  • 122
  • 180
  • 2
    @Ralphs17 That looks like another problem. Could you be more specific? Post the exact error message. – Daniel Frey Oct 16 '13 at 21:54
  • @Ralphs17, Fixing a linker error by introducing a compiler error doubles your problems. – chris Oct 16 '13 at 21:56
  • 1
    @DanielFrey Since the loop calculates `x%c`, `c` should probably be initialised to 1 (or maybe 2 depending on whether 1 counts as a divisor) – simonc Oct 16 '13 at 21:57
  • The errors says "Error 1 error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup – Ralph Lee Stone Oct 16 '13 at 21:58
  • @Ralphs17 That seems to be a problem with your project settings. You need to make it a console application. Since I don't use Windows/VS, I can't help with that one. Google it, ask a new question or wait for some kind soul to leave a comment here :) – Daniel Frey Oct 16 '13 at 22:02
  • 1
    @Ralphs17 This previous thread may help your linking problem: http://stackoverflow.com/questions/6626397/error-lnk2019-unresolved-external-symbol-winmain16-referenced-in-function – Shafik Yaghmour Oct 16 '13 at 22:04
2

c has an indeterminate value in your for loop here:

for(int c; x < c; c++)
        ^

you need to initialize it to a value for example:

for(int c=2; x < c; c++)

it will need to be greater than 0(perhaps 2 since you probably don't want 1 as a divisor) since you are using it in the modulus operation here:

if (x%c==0)

and modulus by 0 is undefined behavior as per the draft C++ standard section 5.6 Multiplicative operators paragraph 4 says:

[...]If the second operand of / or % is zero the behavior is undefined.[...]

It looks like you may have flipped your ending condition and it should go from c < x instead of x < c.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • @Walter I have indeed compiled and ran it and the results are as expected, my suggestions except for starting at two are identical to Daniel's and you do not seem to have an issue with his answer so not much I can do here. If you can not be constructive I will just flag this comment thread for removal b/c it does no one any good to be obscure. – Shafik Yaghmour Oct 17 '13 at 09:40
1
for(int c; x < c; c++) // what do you think is the value of c initially?
                       // and when do you think the loop will stop?

edit I don't know what you meant with unresolved external errors. If I correct your bugs

for(int c=1; c<x; c++) 

it compiled and gave reasonable results: 5 has 1 divisor, 9 has 2, 8 has 3, and 12 has 5.

Walter
  • 44,150
  • 20
  • 113
  • 196
1

You are not giving any initial value to c

for(int c; x < c; c++)

change to

for(int c=0; x < c; c++) //assign a value
digital_revenant
  • 3,274
  • 1
  • 15
  • 24
1

c isn't initialized:

for(int c; x < c; c++)

This declares (but does not initialize) c, and then immediately accesses it to test whether x < c.

user229044
  • 232,980
  • 40
  • 330
  • 338