Thread: Errors!

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    130

    Errors!

    Hi I have 2 questions:

    First question:

    Code:
    void cp_even()
    {	int j;
    	j=0;
    	int i,a[6],b[j],*ptra, *ptrb;
    	printf("Enter SIX values:");
    	
    	for(i=0; i<=5; i++)
    	{
    		scanf("%d",&a[i]);
    		if(a[i]%2==0)
    		{
    			b[j]=a[i];
    			j++;
    		}
    	}
    	
    	ptra=&a[0];
    	ptrb=&b[0];
    	
    	............................... end of the code
    }
    My problem with the bold font in the code, I recieve this missage:
    : error C2057: expected constant expression
    I don't know why it says that; j=0 is constant! (this works in java)

    Second question:

    Code:
    void upperCase(char *ptr)
    {
    	//ptr=&string[0];
    	while(*ptr!='\0')
    	{
    		if(islower(*ptr)) 
    			*ptr=toupper(*ptr);
    		ptr++;
    	}
    }
    void p8()
    {
    	char string[3];
    	printf("Tybe a string in lower case");
    	gets(string);
    
    	upperCase(string);//change to *ptr
    	printf("The string in uppercase: %s",string);
    }
    The code works fine, BUT after displaying the out put on the screen I recieve this message:

    Debug error!
    ......... some paths
    Run-Time check Failure #2 - stack around the variable 'string' was corrupted.....


    Thankx

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    1. > It works in Java.
    lol.
    Okay well, to repeat the compiler, j is not constant at all and even if you used the const keyword, you can only use a variable to initialize arrays in C99. It's also impossible to allocate a zero-length array.


    2. I blame gets for this. Use fgets.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    Quote Originally Posted by citizen
    2. I blame gets for this. Use fgets.
    fgets takes more than one arguments and I don't want that

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Yes you do. gets is evil.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    Quote Originally Posted by citizen
    Yes you do. gets is evil.
    !!!!

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    Quote Originally Posted by laserlight
    So where is the problem!

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, use:
    Code:
    fgets(buf, 3, stdin);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    fgets takes more than one arguments and I don't want that
    That's the lamest excuse not to follow good programming practice I've heard in a while!
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  10. #10
    ---
    Join Date
    May 2004
    Posts
    1,379
    Quote Originally Posted by citizen
    1. > It works in Java.
    lol.
    Okay well, to repeat the compiler, j is not constant at all and even if you used the const keyword, you can only use a variable to initialize arrays in C99.
    I thought you could use
    Code:
    const int i = 10;
    int j[i];
    Actually, I'm certain you can

  11. #11
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    It works sand_man , but I recieve this message:
    : error C2105: '++' needs l-value

    for this code:

    Code:
    for(i=0; i<=5; i++)
    	{
    		scanf("%d",&a[i]);
    		if(a[i]%2==0)
    		{
    			b[j++]=a[i];
    			//j++;
    		}
    	}
    I don't know if I can do that, or what I did is correct, again I am new in learning C

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > Actually, I'm certain you can [declare array sizes with variables]
    If you read my post correctly, I did not dispute this. and no you can't allocate a zero length array, like he was trying to do.
    Code:
    for(i=0; i<=5; i++) {
         scanf("%d", &a[i]);
         if(a[i]%2==0) {
    	b[j+1]=a[i];
    	//j++;
        }
    }
    I had to learn this lesson to, but you cannot use stuff like j++ on the right side of an expression. Write it out.
    Last edited by whiteflags; 06-25-2006 at 10:31 AM.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I had to learn this lesson to, but you cannot use stuff like j++ on the right side of an expression. Write it out.
    hmm... wait a minute. b[j++]=a[i] looks valid to me, unless j is const.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    I had to learn this lesson to, but you cannot use stuff like j++ on the right side of an expression. Write it out.
    What do you mean by this?

    you can do
    Code:
    array[i]=array[j++]; /*array[i]=array[j]; j++;*/
    if you want this
    Code:
    array[i]=array[++j];
    If you meant left
    Code:
    array[++i]=array[j];
    Code:
    array[i++]=array[j];
    are both perfectly legal
    Last edited by linuxdude; 06-26-2006 at 04:21 PM.

  15. #15
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by linuxdude
    you can do
    Code:
    array[i]=array[i++]; /*array[i]=array[j]; j++;*/
    http://c-faq.com/expr/evalorder1.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  2. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 10:50 AM
  3. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  4. Help me with these errors... :-(
    By major_small in forum C++ Programming
    Replies: 6
    Last Post: 09-07-2003, 08:18 PM
  5. errors in class(urgent)
    By ayesha in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2001, 06:51 PM