Thread: undeclared variable problem

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    66

    undeclared variable problem

    I am having a bit of problem understanding why the compiler is shouting at me that I have an undeclared variable.

    This bit of code works:
    Code:
    void line(int n, char c)
    {
      cout << callnumber << ": " ;
    
      for (int i = 0; i < n; i++)
        {
          cout << c ;
        }
    
      cout << endl ;
    }
    But this code gives me the following errors :

    barchart.cc: In function `void hyphen(int)':
    barchart.cc:7: `i' undeclared (first use this function)
    barchart.cc:7: (Each undeclared identifier is reported only once
    barchart.cc:7: for each function it appears in.)
    Code:
    void hyphen(int a)
    {
      for (i = 0; i < a; i++)
        {
          cout << "-" ;
        }
      cout << endl ;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    for (int i = 0; i < n; i++)
    for (i = 0; i < a; i++)
    Guess what this does...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    Thank you.

    As always, one is blind as a bat to the obvious.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  2. Problem getting the input from a temp variable into the Array
    By hello_moto in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2006, 01:50 AM
  3. Problem with a char variable set as a letter
    By 7smurfs in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2004, 01:25 PM
  4. Strange variable problem
    By Mithoric in forum Windows Programming
    Replies: 9
    Last Post: 06-19-2004, 03:34 AM
  5. Peculiar Problem with char variable in C Language
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-31-2001, 04:06 PM