beginner

This is a discussion on beginner within the C Programming forums, part of the General Programming Boards category; i am brand new to coding, but i can nver get brackets right {} Will some one please explaing this ...

  1. #1
    Unregistered
    Guest

    beginner

    i am brand new to coding, but i can nver get brackets right {}

    Will some one please explaing this to me?

    /************************************************** ************************************************** *
    Filename: pgen.c
    Author: Mike Hartwig
    Purpouse: Generate given amount of prime numbers.
    Input: Keyboard
    Output: Screen
    ************************************************** ************************************************** */
    #include <stdio.h>


    int main() {


    int prime_amount; //the amount of primes to generate.
    int prime_start = 2; //the number to start generating primes at.
    int prime_count; // the count of numbers we already have.
    int prime_answer; // the answer.

    printf("Welcome to the Prime Number Generator.\n");
    printf("How many prime numbers would you like to generate: ");
    scanf("%d", &prime_amount);
    printf("Generating %d prime numbers...\n", prime_amount);




    do (prime_count < 2) {


    prime_answer = prime_start + prime_count * 2



    } while


    printf("%d, \n", &prime_answer)



    return (1);


    }

    return (0);

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    4

    Lightbulb Re: Beginner

    Ok, just a small mix up in your do while loop.

    It should look like this:

    #include <stdio.h>


    int main() {


    int prime_amount; //the amount of primes to generate.
    int prime_start = 2; //the number to start generating primes at.
    int prime_count; // the count of numbers we already have.
    int prime_answer; // the answer.

    printf("Welcome to the Prime Number Generator.\n");
    printf("How many prime numbers would you like to generate: ");
    scanf("%d", &prime_amount);
    printf("Generating %d prime numbers...\n", prime_amount);




    do {


    prime_answer = prime_start + prime_count * 2



    }
    while(prime_count < 2);


    printf("%d, \n", &prime_answer);



    return (1);


    }

    return (0);

    PS, I'm a bit rusty so if anyone sees this code and notices anything wrong, correct me, but it looks like it should be right.

    -AJ

  3. #3
    Unregistered
    Guest
    i get this. There is something wrong with my squiggles {}. If somebody could explain to me exactly where and when to put them. It would be great

    C:\Program Files\Microsoft Visual Studio\MyProjects\pgen\pgen.c(34) : error C2143: syntax error : missing ';' before '}'
    C:\Program Files\Microsoft Visual Studio\MyProjects\pgen\pgen.c(37) : error C2061: syntax error : identifier 'printf'
    C:\Program Files\Microsoft Visual Studio\MyProjects\pgen\pgen.c(46) : error C2059: syntax error : 'return'

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    4

    Re: Beginner

    Ok, try this:

    #include <stdio.h>
    #include <stdlib.h>

    int main() {


    int prime_amount; //the amount of primes to generate.
    int prime_start = 2; //the number to start generating primes at.
    int prime_count; // the count of numbers we already have.
    int prime_answer; // the answer.

    printf("Welcome to the Prime Number Generator.\n");
    printf("How many prime numbers would you like to generate: ");
    scanf("%d", &prime_amount);
    printf("Generating %d prime numbers...\n", prime_amount);




    do {


    prime_answer = prime_start + prime_count * 2



    }
    while(prime_count < 2);


    printf("%d, \n", &prime_answer);



    return (1);


    }

    return (0);

    }

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    What I'm usually doing with { and } is placing them on the same colum. So

    Code:
    while (condition)
    {
        /* some code */
    }
    In this way, it is much easier to find {}-pairs. A lot of programmers write:

    Code:
    while (condition)
       /* a statement */
    When more code is needed, you can easily forget the brackets. So it is a good style to do this:

    Code:
    while (condition)
    {
        /* a statement */
    }
    Further some comments on your code:

    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    
    int main () 
    { 
        int prime_amount; //the amount of primes to generate. 
        int prime_start = 2; //the number to start generating primes  at. 
        int prime_count; // the count of numbers we already have. 
        int prime_answer; // the answer. 
    
        printf("Welcome to the Prime Number Generator.\n"); 
        printf("How many prime numbers would you like to generate: "); 
        scanf("%d", &prime_amount); 
        printf("Generating %d prime numbers...\n", prime_amount); 
    
        /* Shiro: you need to initialise prime_count before using it */
        prime_count = INITIAL_VALUE;
    
        do 
        { 
            /* Shiro: you forgot a ; at the end */
            prime_answer = prime_start + prime_count * 2;
        } 
        while (prime_count < 2); 
    
        printf("%d, \n", &prime_answer); 
    
        /* Shiro: usually 0 is used as "everything went OK" value */
        return 0; 
    }

  6. #6
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187
    Problem :

    Code:
        do 
        { 
            /* Shiro: you forgot a ; at the end */
            prime_answer = prime_start + prime_count * 2;
        } 
        while (prime_count < 2);

    Looks like an infinite loop since your testing
    prime_count without ever changing it within
    the do while loop
    -------------------------------------------------------

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    8
    You should check your last printf statement
    printf("%d,\n", &prime_answer);
    You should omit the & otherwise you might get some funny answer.

  8. #8
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Thanks ginoitalo. There can be an infinite loop. The variable prime_count doesn't change so prime_count < 2 will always be TRUE or FALSE, depending on the INITIAL_VALUE.

    Since prime_count is the actual number of primes and prime_amount the requested number of primes it should be:

    Code:
    do 
    { 
        /* Shiro: you forgot a ; at the end */
        prime_answer = prime_start + prime_count * 2;
    } 
    while (prime_count++ < prime_amount);
    Therefor I would initialise prime_count to 0.

    By the way, this program doesn't generate primes. If that was the purpose of the program.

  9. #9
    Registered User
    Join Date
    Jan 2002
    Posts
    12
    The braces {} are used to establish a block of code and they establish the scope of the variables and objects defined inside them.

  10. #10
    Unregistered
    Guest
    hey if i omit the and in the printf statement i get a warning about print_answer not being initailized.

  11. #11
    Unregistered
    Guest
    i got this now.

    /************************************************** ************************************************** *
    Filename: pgen.c
    Author: Mike Hartwig
    Purpouse: Generate given amount of prime numbers.
    Input: Keyboard
    Output: Screen
    ************************************************** ************************************************** */
    #include <stdio.h>
    #include <stdlib.h>

    int main ()
    {
    int prime_amount; //the amount of primes to generate.
    int prime_count = 1; // the count of numbers we already have.
    int prime_answer; // the answer.
    // int prime_start = 2;

    printf("Welcome to the Prime Number Generator.\n");
    printf("How many prime numbers would you like to generate: ");
    scanf("%d", &prime_amount);
    printf("Generating %d prime numbers...\n", prime_amount);

    do
    {
    printf("%d, ", prime_answer);

    }
    while (prime_count++ < prime_amount);

    prime_answer = prime_count++;




    return (0);
    }

  12. #12
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Still one thing. I would initialise prime_count to 0, because of (prime_count++ < prime_amount).

    Assume prime_amount is N. Then you run from 0 to N-1. Which is in fact N prime numbers. You are initialising prime_count as 1, so you are running from 1 to N-1, which results in N-1 primenumbers, but you want N prime numbers.

    Or do (prime_count++ <= prime_amount). This would lead to running from 1 to N.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help Me with this beginner C++ Program!
    By ClearSights in forum C++ Programming
    Replies: 7
    Last Post: 09-24-2008, 10:22 AM
  2. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  3. What are some good beginner programs I shouold make?
    By oobootsy1 in forum C# Programming
    Replies: 6
    Last Post: 08-09-2005, 02:02 PM
  4. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 10:21 AM

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