Thread: Coding help.... have a few questions

  1. #16
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    ok tried using chIDE and I got it to work except it acts funny with doubles....I need it to do the same thing as it does for int to do with the inputs for doubles
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    
        int intarray[10];
        double doublearray[10];
        char string[20];
        int uppercount = 0, lowercount = 0;
        int i;
        int numints, numdoubles;
        int negcount, poscount, zerocount;
    
        printf("Enter how many numbers you want to find values for: ");
        scanf("%d", &numints);
        for (i=0; i< numints; i++)
        {
            printf("Enter an integer value: ");
            scanf("%d", &intarray[i]);
        }
        /* Then ask for number of double values and read them in using %lf */
        printf("How many decimals do you want to find values for: ");
        scanf("%lf", &numdoubles);
        for (i=0; i< numdoubles; i++)
    
        {
            printf("Enter an demcimal value: ");
            scanf("%lf", &doublearray[i]);
        }
    
        /* #3 */
        printf("Enter a short character string: ");
        scanf("%s", string);
        negcount=0;/* initialize the counters*/
        poscount=0;
        zerocount=0;
        for (i = 0; i < numints; i++)
        {
            if (intarray[i] < 0)
                negcount++;
            if (intarray[i] == 0)
                zerocount++;
            if (intarray[i] > 0)
                poscount++;
        }
       
        printf("you entered: \n %d negative values \n %d positive values \n %d zero values \n", negcount, poscount, zerocount);
        
    
    
        /* Do counts for double array */
        negcount=0;/* initialize the counters*/
        poscount=0;
        zerocount=0; 
        for (i = 0; i < numints; i++)
        {
            if (doublearray[i] < 0)
                negcount++;
            if (doublearray[i] == 0)
                zerocount++;
            if (doublearray[i] > 0)
                poscount++;
        }
        printf("you entered: \n %d negative values \n %d positive values \n %d zero values \n", negcount, poscount, zerocount);
      
        
        
        
        for (i = 0; string[i]; i++)
        {
            if ((string[i] >= 'A') && (string[i] <= 'Z')) {
                uppercount++;}
    
    
            if ((string[i] >= 'a') && (string[i] <= 'z')) {
                lowercount++;}
    
        }
        printf("You have %d upper case characters\n",uppercount);
        printf("You have %d lower case characters\n",lowercount);
       /* print out counter values etc.....*/
    
        system("Pause");
        return 0;
    }
    debugging screen in ch
    Code:
    >ch -u "hw3 tryed to fix errors in ch"    
    Enter how many numbers you want to find values for: 2
    Enter an integer value: 2
    Enter an integer value: -2
    How many decimals do you want to find values for: 2
    Enter a short character string:
    it does ask for the decimal values .... it just skips to string grrrr
    Last edited by Cess; 10-03-2011 at 08:17 PM.
    ~Cess~
    AKA : total newbie
    ....and totally frustrated
    thanks for any help given.....
    I feel like I"m going to fail this class....blah!

  2. #17
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You are trying to dump a double value into an int at line 24...

    Code:
    printf("How many decimals do you want to find values for: ");     
    scanf("%d", &numdoubles);

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I do not know if fixing this will fix your problem, but this looks wrong since numdoubles is an int:
    Code:
    scanf("%lf", &numdoubles);
    You should still be using %d as the format specifier.
    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

  4. #19
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    Quote Originally Posted by CommonTater View Post
    You are trying to dump a double value into an int at line 24...

    Code:
    printf("How many decimals do you want to find values for: ");     
    scanf("%d", &numdoubles);
    wow that was silly of me... I was soo worried about using %lf like the professor asked... I didn't even use it correctly thanks
    ~Cess~
    AKA : total newbie
    ....and totally frustrated
    thanks for any help given.....
    I feel like I"m going to fail this class....blah!

  5. #20
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    Ok my only question is now how come the code below works in ch but not in code blocks?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    
        int intarray[10];
        double doublearray[10];
        char string[20];
        int uppercount = 0, lowercount = 0;
        int i;
        int numints, numdoubles;
        int negcount, poscount, zerocount;
    
        printf("Enter how many numbers you want to find values for: ");
        scanf("%d", &numints);
        for (i=0; i< numints; i++)
        {
            printf("Enter an integer value: ");
            scanf("%d", &intarray[i]);
        }
        /* Then ask for number of double values and read them in using %lf */
        printf("How many decimals do you want to find values for: ");
        scanf("%d", &numdoubles);
        for (i=0; i< numdoubles; i++)
    
        {
            printf("Enter an demcimal value: ");
            scanf("%lf", &doublearray[i]);
        }
    
        /* #3 */
        printf("Enter a short character string: ");
        scanf("%s", string);
        negcount=0;/* initialize the counters*/
        poscount=0;
        zerocount=0;
        for (i = 0; i < numints; i++)
        {
            if (intarray[i] < 0)
                negcount++;
            if (intarray[i] == 0)
                zerocount++;
            if (intarray[i] > 0)
                poscount++;
        }
       
        printf("you entered: \n %d negative values \n %d positive values \n %d zero values \n", negcount, poscount, zerocount);
        
    
    
        /* Do counts for double array */
        negcount=0;/* initialize the counters*/
        poscount=0;
        zerocount=0; 
        for (i = 0; i < numints; i++)
        {
            if (doublearray[i] < 0)
                negcount++;
            if (doublearray[i] == 0)
                zerocount++;
            if (doublearray[i] > 0)
                poscount++;
        }
        printf("you entered: \n %d negative values \n %d positive values \n %d zero values \n", negcount, poscount, zerocount);
      
        
        
        
        for (i = 0; string[i]; i++)
        {
            if ((string[i] >= 'A') && (string[i] <= 'Z')) {
                uppercount++;}
    
    
            if ((string[i] >= 'a') && (string[i] <= 'z')) {
                lowercount++;}
    
        }
        printf("You have %d upper case characters\n",uppercount);
        printf("You have %d lower case characters\n",lowercount);
       /* print out counter values etc.....*/
    
        system("Pause");
        return 0;
    }
    program in ch
    Code:
     
    >ch -u "hw3 tryed to fix errors in ch"    
    Enter how many numbers you want to find values for: 1
    Enter an integer value: 1
    How many decimals do you want to find values for: 1
    Enter an demcimal value: 1.1
    Enter a short character string: HItoU
    you entered: 
     0 negative values 
     1 positive values 
     0 zero values 
    you entered: 
     0 negative values 
     1 positive values 
     0 zero values 
    You have 3 upper case characters
    You have 2 lower case characters
    Press any key to continue . . .

    code in codeblocks
    Code:
    >   
    Enter how many numbers you want to find values for: 1
    Enter an integer value: 1
    Enter an demcimal value: 1.1
    Enter a short character string: HItoU
    ~Cess~
    AKA : total newbie
    ....and totally frustrated
    thanks for any help given.....
    I feel like I"m going to fail this class....blah!

  6. #21
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ch and Code::Blocks are IDEs (Integrated Developer's Environments) not compilers... What compilers are they using... and even if the same compiler, what settings in that compiler?

    (FWIW... it works as you'd expect in Pelles C...)

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Cess
    my only question is now how come the code below works in ch but not in code blocks?
    You might want to answer CommonTater's question anyway, but... if an argument is not of the correct type for the corresponding format specifier, the behaviour is undefined.
    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

  8. #23
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    You might want to answer CommonTater's question anyway, but... if an argument is not of the correct type for the corresponding format specifier, the behaviour is undefined.
    ... and the compiler should have warned about it...

  9. #24
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Quote Originally Posted by Cess View Post
    wow that was silly of me... I was soo worried about using %lf like the professor asked... I didn't even use it correctly thanks
    And if you had done like I suggested at post #7, you wouldn't be in this mess now.

    It really is astonishingly simple in code blocks.
    Coding help.... have a few questions-screenshot-png

    While you're at it, ticking the next two -W options would be good as well.
    And the -ansi option above it for extra bonus points.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IDE for vlc coding
    By binnyshah in forum Windows Programming
    Replies: 2
    Last Post: 07-06-2010, 02:56 AM
  2. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  3. Coding a log in.....
    By Darkozuma in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2008, 07:55 PM
  4. C coding questions
    By vopo in forum C Programming
    Replies: 3
    Last Post: 08-29-2007, 02:37 AM
  5. coding.
    By programer9000 in forum C++ Programming
    Replies: 2
    Last Post: 01-30-2003, 08:59 PM