Coding help.... have a few questions

This is a discussion on Coding help.... have a few questions within the C Programming forums, part of the General Programming Boards category; I am having a lot of trouble with this program, I've tried adding and changing things but I think I'm ...

  1. #1
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55

    Coding help.... have a few questions

    I am having a lot of trouble with this program, I've tried adding and changing things but I think I'm making it worst.
    My 1st problem is the program seems to end after imputing the 1st values.... It didn't do that before... I need it to take in values and out put which values are negative, odd, and zero. However I can't do that if the program ends after the users 1st input... I'm guessing its an ; or bracket problem that I couldn't find

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* Homework Create a program that accepts user input and store a set of integers in one for loop,
     a set of double values in a second for loop and a set of characters in a character string.
     Then the program will count the number of positive, zero and negative values for the integer 
    and double arrays and the number of upper and lower case variables in the character string. 
    
    In other words it needs to:
    The program is just to count the number of positive, negative and zero
    values that a user enters. In the first case, the user enters integers
    and then real values are entered. A character string is then entered
    and the program counts upper and lower case characters.*/
    
    #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("Input number of integer values: ");
        scanf("%d", &numints);
        for (i=0; i< numints; i++)
        {
            printf("Enter an integer value: ");
            scanf("%d", &intarry[i]);
        }
        /* Then ask for number of double values and read them in using %lf */
        printf("Input a another value:  ");
        scanf("%lf", &numdoubles);
        for (i=0; i< numdoubles; i++)
    
        {
            printf("Enter an integer 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++;
            printf("You entered %d negtive values ",negcount);
    
            if (intarray[i] == 0)
                zerocount++;
            printf("You entered %d zero values ",zerocount);
    
            if (intarray[i] > 0)
                poscount++;
            printf("You entered %d postive values",poscount);
        }
    
       /* print out counter values */
       printf("Enter a short character string: ");
        scanf("%s", string);
        negcount=0;/* initialize the counters*/
        poscount=0;
        zerocount=0;
        /* Do counts for double array */
        for (i = 0; string[i]; i++)
        {
            if ((string[i] >= 'A') && (string[i] <= 'Z'))
                uppercount++;
                printf("You have %d upper case characters\n",uppercount);
    
            if ((string[i] >= 'a') && (string[i] <= 'z'))
                lowercount++;
                printf("You have %d lower case characters\n",lowercount);
        }
       /* print out counter values etc.....*/
    
        system("Pause");
        return 0;
    }
    thanks for any help!!!
    Last edited by Cess; 10-03-2011 at 09:10 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    That code does not compile because of a typo on line 31. You need to post the code you are actually using that created the problem you describe.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    whoa sorry about that... thought it was, must of forgot to copy and paste the recent changes I made
    Ok the code I have entered is above...same problem as before compiles but stops after asking for integer.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    Well the first thing is get a compiler that can warn you about printf/scanf mis-use.
    Code:
    $ gcc -W -Wall -ansi -pedantic bar.c 
    bar.c: In function ‘main’:
    bar.c:38: warning: format ‘%lf’ expects type ‘double *’, but argument 2 has type ‘int *’
    Then there is this
    Code:
            if ((string[i] >= 'A') && (string[i] <= 'Z'))
                uppercount++;
                printf("You have %d upper case characters\n",uppercount);
    Despite your indentation, what you really have is this.
    Code:
            if ((string[i] >= 'A') && (string[i] <= 'Z')) {
                uppercount++;
            }
            printf("You have %d upper case characters\n",uppercount);
    This mistake can easily be avoided by always using braces, even when they seem to be optional.
    The problem being, there is no error message going from optional to compulsory, the code just doesn't work as expected.


    Perhaps you only want to print the results ONCE at the end, in which case,
    Code:
        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);
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    Ok tried to make those changes but the program is still ending after
    Code:
     Input number of integer values:
    press any key to continue....
    press any key to continue....
    press any key to continue....
    I'm using codeblocks.... its the program that was suggested by the professor....
    I'm a newbie so ya

    this is what my code looks like now
    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("Input number of integer values: ");
        scanf("%d", &numints);
        for (i=0; i< numints; i++)
        {
            printf("Enter an integer value: ");
            scanf("%d", &intarry[i]);
        }
        /* Then ask for number of double values and read them in using %lf */
        printf("Input a demcimal value:  ");
        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++;
            printf("You entered %d negtive values ",negcount);
    
            if (intarray[i] == 0)
                zerocount++;
            printf("You entered %d zero values ",zerocount);
    
            if (intarray[i] > 0)
                poscount++;
            printf("You entered %d postive values",poscount);
        }
    
       /* print out counter values */
       printf("Enter a short character string: ");
        scanf("%s", string);
        negcount=0;/* initialize the counters*/
        poscount=0;
        zerocount=0;
        /* Do counts for double array */
        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;
    }

    the class starts at 1.... maybe I'll just stay after and ask the professor since I"m so confused.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    this is what my code looks like now
    That is the third time you have made that claim and still posted code that contains the error I mentioned in post #2:

    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("Input number of integer values: ");
        scanf("%d", &numints);
        for (i=0; i< numints; i++)
        {
            printf("Enter an integer value: ");
            scanf("%d", &intarry[i]);
    No compiler in the world can possibly overlook that error (altho apparently Salem kindly did, corrected it so it would compile, and posted the subsequent errors).

    But since that code cannot be compiled, how am I to know it does not also contain other errors or differences that are not in your REAL source? And if you cannot provide source that does what you say it does, how can anyone help you with a problem in it?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > I'm using codeblocks.... its the program that was suggested by the professor....
    It's a good choice.
    Just so you know, the default compiler is GCC, so you should be able to add those -W flags to your project->settings->compiler kind of dialog.

    Do you type in anything to respond to "Input number of integer values:"
    Note, if it is expecting an int, and you type in a letter, then this is bad.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    Quote Originally Posted by Salem View Post
    >
    Just so you know, the default compiler is GCC, so you should be able to add those -W flags to your project->settings->compiler kind of dialog.
    Huh!?!?! Ok does that mean code bock has GCC as the compiler.... add what? what flags?
    Last edited by Cess; 10-03-2011 at 03:24 PM.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Cess... on windows Code::Blocks ships with the MinGW port of GCC. On Linux (etc) it usually either ships with GCC or finds the native GCC on your system.

    Look in your menus and help files... Code::Blocks is very well documented.

  10. #10
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    um ok I downloaded it for free..... I didn't have it shipped.....I am guessing that means something else, will this help me learn how to fix errors with my program...??
    ~Cess~
    AKA : total newbie
    ....and totally frustrated
    thanks for any help given.....
    I feel like I"m going to fail this class....blah!

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Did you even bother to look in your compiler settings to see if there was a place to check or enter the flags?

    NO... you responded within 3 minutes... you didn't have time to do that...

    Now... go and actually look around inside your software... use the help file... you'll be amazed at what you've been missing.


    The ide is to set the -Wall flag ... Warnings All... so that you actually have a list of things to fix each time you compile the program...

  12. #12
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    um I did... I still not seeing flags in the help menu...... I see about tips and plugins.... under plugins there is a long list... I looked under debug.. because I think that's what I want to do... and I'm still confused... so I replied....and now i've looked under half of them..... and now I get where he said the compiler is

    This plugin is an interface to various compilers:

    GNU GCC compiler
    Microsoft Visual C++ Free Toolkit 2003
    Borland C++ Compiler 5.5

    ya but I have no clue what that means... or where the flags are.... I just want to learn how to debug so I can stop bugging you people and start to solve my errors and be able to make programs... and quit being frustrated newbie.... blah I really wish this wasn't so foran to me.
    ~Cess~
    AKA : total newbie
    ....and totally frustrated
    thanks for any help given.....
    I feel like I"m going to fail this class....blah!

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok, I'm not a Code::Blocks user... but I learn my way around software by a simple method ... I click on stuff... lots and lots of stuff... Menus, taskbar buttons, etc...everything... until I know what it can and can't do for me...

    Maybe someone who is using code::blocks can give you explict instructions, but you still need to learn your own way around in there... knowledge and skill do not fall as pennies from the heavens...

  14. #14
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    What do you do, if your user put a non digit character to any scanf?
    Right!
    The next scanf will be ignored.

  15. #15
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    Ok didn't know that but it makes sense.. thanks....
    ~Cess~
    AKA : total newbie
    ....and totally frustrated
    thanks for any help given.....
    I feel like I"m going to fail this class....blah!

Page 1 of 2 12 LastLast
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, 07:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21