newbie in need of help

This is a discussion on newbie in need of help within the C++ Programming forums, part of the General Programming Boards category; ok so who can tell me what is wrong with these 4 sections of code? i have no idea. these ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    12

    newbie in need of help

    ok so who can tell me what is wrong with these 4 sections of code? i have no idea. these are parts of code from seperate programs.

    thanx


    Code:
    int sum (int p, int q)
       {
       int result;
       result=p+q;
       }
    Code:
    int c[10]
    int i;
    for(i=o; i<=10; i++)
    c[i]=0
    Code:
    int *ptrl;
    int number=5;
    ptrl=number;
    printf("the value pointed by ptrl is %d\n", *ptrl);
    Code:
    switch (code)
       {
       case 1:
          printf("the number is 4\n");
       case 2:
          printf("the number is 8\n");
          break;
       default:
          printf("the number is not for or 8\n");
          break:
       }
    Last edited by idiotprogrammer; 06-13-2003 at 08:49 AM.

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    12
    another question i was asked to convert some small parts of progs from while loops to for and was wondering if these are right.

    WHILE LOOP

    Code:
    int count 1;
    
    while (count<=10)
    {
    printf("%d",count);
    count++;
    }
    CONVERTED TO ------ FOR LOOP
    Code:
    {
    for (count=1; count<=10; count++);
    printf("%d",count);
    }
    Last edited by idiotprogrammer; 06-13-2003 at 08:51 AM.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    1) No 'return result'.

    2) Missing semicolons, 'i=0', 'i<10'.

    3) '*pctrl = number;' - don't set the address, set the value.

    4) You have 'case 2:' twice, and no break after the first one.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    5) Huh? That syntax isn't even close. 'for(count = 1; count <= 10; count += 2)'
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    12
    check them again. had a few stupid typos

    *quote*
    Huh? That syntax isn't even close. 'for(count = 1; count <= 10; count += 2)'

    why is it count +=2)

    that would mean that its adding 2 each loop ?

    and sorry as my post says im a new instead of telling me whats wrong can someone spend an extra sec and type in the correct code as this woudl be more clear to me than an explination

    cheers
    Last edited by idiotprogrammer; 06-13-2003 at 08:56 AM.

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    42
    Why don't you just paste the whole code so we can understand what the hell you're doing
    Last edited by Nectron; 06-13-2003 at 11:23 AM.
    I have a code which stops life!

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by idiotprogrammer

    why is it count +=2)
    Sorry, I could've sworn I saw two 'count++'s in there.

    The first one says it returns an int, but never actually does (done by calling 'return some_int;' in this case).

    Still typos in the second one ('0' and not 'o'), semicolons.

    Third one you need to dereference the pointer before assigning a value to it (otherwise you are assigning it a new address with little chance to have any meaning). pctrl also needs to be allocated.

    Probably need a break in 'case 1'.

    Hope this helps a bit... If not, you might want to post a bit more, and possibly what you are getting/what you expect.
    Last edited by Zach L.; 06-13-2003 at 11:53 AM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571

    Re: newbie in need of help

    Code:
    int sum (int p, int q)
       {
       int result;
       result=p+q;
       return result;
       }
    You need to actually give the result back.

    Code:
    int c[10];
    int i;
    for(i=0; i<10; i++)
    c[i]=0;
    Missing semicolons, you need the number 0 not the letter o, and there are 10 ints in the array, 0 through 9, so 10 is out of bounds.

    Code:
    int *ptrl;
    int number=5;
    ptrl=&number;
    printf("the value pointed by ptrl is %d\n", *ptrl);
    You want to assign the ADDRESS of number to the pointer (ptrl is a pointer, so it stores an address.) You CAN'T do *ptrl = number; this copies number into the place pointed at by ptrl -- but ptrl is an uninitialized pointer, dereferencing it yields undefined behavior. The line ptrl=&number; initializes the pointer (by making it point to the memory location of "number". After that, you can dereference it successfully in the next statement.

    Code:
    switch (code)
       {
       case 1:
          printf("the number is 4\n");
          break;
       case 2:
          printf("the number is 8\n");
          break;
       default:
          printf("the number is not for or 8\n");
          break:
       }
    Last edited by Cat; 06-13-2003 at 11:53 AM.

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Or...

    Code:
    int* ptrl = new int;
    int number = 5;
    *ptrl = number;
    printf("the value pointed by ptrl is %d\n", *ptrl);
    Hard to tell which you want.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    "Why don't you just paste the whole code so we can understand what the hell you're doing"

    Most likely, it looks like you're doing his homework.

  11. #11
    Registered User
    Join Date
    Mar 2003
    Posts
    12

    Thumbs down

    yes well it woudl be my homework except for one thing....i dont goto school

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 06:49 PM
  2. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 11:53 AM
  3. Newbie Programmer
    By Extropian in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2004, 01:17 PM
  4. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 07:44 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM

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