Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » for (int f = 0; szInput[f] !='@'; f++)
Closed Thread
Old 07-14-2006, 08:27 PM   #1 (permalink)
 
True Techie

Join Date: May 2006

Posts: 101

jonmon6691

Default for (int f = 0; szInput[f] !='@'; f++)

My compiler gives this error at compile time

name lookup of `f' changed for new ANSI `for' scoping

and points to this line

for (int f = 0; szInput[f] !='@'; f++)

i need to know what i need to changge to make it work and if you need more info i can send you the rest of the source code
__________________
<small>
.:HP Pavillion dv6000:.
1.8 ghtz Intel Core 2 Duo
2GB RAM
120 GB HDD
nVidia Go graphics
Apache 2.2 (looking for a domain name)
</small>
jonmon6691 is offline  
Old 07-14-2006, 10:13 PM   #2 (permalink)
Spartan's Avatar
 
Ultra Techie

Join Date: Jul 2005

Posts: 814

Spartan

Default

I don't need the whole code, probably, but can you put it up or PM me with it anyway??
__________________
Spartan is offline  
Old 07-14-2006, 10:36 PM   #3 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default

Post your code. The line a compiler flags isn't necessarily the line that contains the error. From the error message alone, I would assume you're trying to use 'f' outside of its scope. It's impossible to tell for sure without seeing the rest of your code.
jaeusm is offline  
Old 07-14-2006, 11:09 PM   #4 (permalink)
 
True Techie

Join Date: May 2006

Posts: 101

jonmon6691

Default

All right, full source code coming soon
__________________
<small>
.:HP Pavillion dv6000:.
1.8 ghtz Intel Core 2 Duo
2GB RAM
120 GB HDD
nVidia Go graphics
Apache 2.2 (looking for a domain name)
</small>
jonmon6691 is offline  
Old 07-17-2006, 03:48 PM   #5 (permalink)
 
True Techie

Join Date: May 2006

Posts: 101

jonmon6691

Default

Code:
/*
THis will take an input string and output the number index of the 
letters respectivly. i.e. a=1 b=2 c=3...
*/


#include <iostream.h>
#include <stdlib.h>

int main()
{
      //Main variables intalized here
      int disp;
      int f = 1;
      char szInput[128];
      char cAlpha[53] = {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
      int  nIndex[52] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26};
      int  ninputindex[128];
      //Beyond here is the program
      cout << ">:";
      cin >> szInput;
      cout << "ALLGOOD";
      for (; szInput[f] != '@'; f++)
      {
          for (int ii;ii<52;ii++)
          {
              if (szInput[f] == cAlpha[ii])
              {
                 ninputindex[f]=nIndex[ii];
              }
          }
      }
      int j = f ;
      for (;j > 0;j--)
      {
          disp++;
          cout << ninputindex[disp] << " ";
      }

      system("PAUSE");
      return 0;
}

__________________
<small>
.:HP Pavillion dv6000:.
1.8 ghtz Intel Core 2 Duo
2GB RAM
120 GB HDD
nVidia Go graphics
Apache 2.2 (looking for a domain name)
</small>
jonmon6691 is offline  
Old 07-17-2006, 04:34 PM   #6 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default

There are a ton of syntax errors in that code. First of all, you should be including <iostream> without the '.h' extension, and you should include <cstdlib> instead of 'stdlib.h'. Second, you did not specify a namespace for 'cout' and 'cin'. You need to use the 'using' directive or type 'std::cout' and 'std::cin' instead. Third, use loops to fill your arrays instead of doing so manually. Fourth, you did not assign an initial value to the variable 'ii' before you used it. Fifth, in terms of style and code readability, you should initialize your loop control variables in the 'for' declaration. However, declaring a variable outside the loop is fine. Sixth, your char array should have length 54, not 53 (remember the null terminator). Also, if you're going to manually assign values to an array as you have done, you don't need to specify the length of the array. Just leave the square brackets empty.

Do some more work and post back again with updates.
jaeusm is offline  
Old 07-17-2006, 04:40 PM   #7 (permalink)
 
True Techie

Join Date: May 2006

Posts: 101

jonmon6691

Default

Quote:
Originally posted by jaeusm
There are a ton of syntax errors in that code. First of all, you should be including <iostream> without the '.h' extension, and you should include <cstdlib> instead of 'stdlib.h'. Second, you did not specify a namespace for 'cout' and 'cin'. You need to use the 'using' directive or type 'std::cout' and 'std::cin' instead. Third, use loops to fill your arrays instead of doing so manually. Fourth, you did not assign an initial value to the variable 'ii' before you used it. Fifth, in terms of style and code readability, you should initialize your loop control variables in the 'for' declaration. However, declaring a variable outside the loop is fine. Sixth, your char array should have length 54, not 53 (remember the null terminator). Also, if you're going to manually assign values to an array as you have done, you don't need to specify the length of the array. Just leave the square brackets empty.

Do some more work and post back again with updates.
I dont need to specify a namespace or the using directive , the compiler compiles that whole thing fine (DEV-C++) but i will take ur other suggestions to clean it up

and what do you mean:
"Third, use loops to fill your arrays instead of doing so manually."
__________________
<small>
.:HP Pavillion dv6000:.
1.8 ghtz Intel Core 2 Duo
2GB RAM
120 GB HDD
nVidia Go graphics
Apache 2.2 (looking for a domain name)
</small>
jonmon6691 is offline  
Old 07-17-2006, 04:50 PM   #8 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default

It is always a good idea to explicitly put the namespace in the source code. Otherwise, someone else will not be able to compile your code using their compiler -- especially someone who has little experience. In this specific case, it's not a big deal. However, it's better to kill bad practices early before they become habits.
jaeusm is offline  
Old 07-17-2006, 05:08 PM   #9 (permalink)
 
True Techie

Join Date: May 2006

Posts: 101

jonmon6691

Default

well, i made some changes and now, if u compile it it will run but the results are unexpected, only the first number works;
Code:
>:hello
8 2089952524 1703961 2089952188 1441812
ya so if you can see the problem...

Code:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
      //Main variables intalized here
      int disp=0;
      int f = 0;
      int ii = 0;
      char szInput[128];
      char cAlpha[] = {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
      int  nIndex[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26};
      int  ninputindex[128];
      //Beyond here is the program
      cout << ">:";
      cin >> szInput;

      for (; szInput[f] != '\0'; f++)
      {
          //cout << "\nloop    1: " << f;
          for (;ii<53;ii++)
          {
              //cout << "\nloop  2: " << ii;
              if (szInput[f] == cAlpha[ii])
              {
                 ninputindex[f]=nIndex[ii];
              }
          }
      }
      int j = f ;
      for (;j > 0;j--)
      {

          cout << ninputindex[disp] << " ";
          disp++;
      }

      system("PAUSE");
      return 0;
}

__________________
<small>
.:HP Pavillion dv6000:.
1.8 ghtz Intel Core 2 Duo
2GB RAM
120 GB HDD
nVidia Go graphics
Apache 2.2 (looking for a domain name)
</small>
jonmon6691 is offline  
Old 07-17-2006, 10:37 PM   #10 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default

You're getting large, unexpected numbers for a good reason. After you go through your inner loop (with the variable 'ii') once, you don't reinitialize it back to zero. So the second time you go through that loop, ii = 54, then 55, then 56...you get the idea. You're referencing memory past the end of your array, so who knows what is stored out there. By the way, that is how buffer overflow attacks work. Programmers using C and C++ get lazy and don't enforce the array bounds in programs they write (which can be altogether eliminated by using vectors instead of arrays). This allows attackers to reference memory they have no business touching. Always be sure to check your array index variables before using them. Or, once you've learned enough about C++, use vectors instead.
jaeusm is offline  
 
Closed Thread

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On