Thread: atoanger

  1. #1
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    atoanger

    can someone tell me what is wrong with this code.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    
    {
    
    char input[5]; /* uers's input */
    int r_input,count,t_num=0;  /* converted intput, counter and tri number */
    
    printf("What number would you like the triangular of? ");
    scanf("%d", &input); /* caputre data */
    r_input = atoi(input);  /* validate input */
    
    /* calculate triangular number
    * which is bascically factorial
    * ex: 7 + 6 + 5 + 4 + 3 + 2 + 1
    */
    
    for(count = 0; count <=r_input; count++)
       {
          t_num = t_num + count;
          printf("Count = %d \t T_num = %d\n", count, t_num);
    
        }
    
    printf("Finished!\n");
    system("pause");
    return 0;
    
    }
    when i run it, it seems the loop stays at 0? what am i doing wrong?

    I removed the atoi function and it worked fine
    Code:
    #include <stdio.h>
    /* #include <stdlib.h>  */
    
    int main()
    
    {
    
    /* char input[10]; uers's input */
    
    int r_input,count,t_num=0, input;  /* converted intput, counter and tri number */
    
    printf("What number would you like the triangular of?\n");
    scanf("%d", &input); /* caputre data */
    
    /* r_input = atoi(input); validate input */
    
    /* calculate triangular number
    * which is bascically factorial
    * ex: 7 + 6 + 5 + 4 + 3 + 2 + 1
    */
    
    for(count = 0; count <=input; count++)
       {
          t_num = t_num + count;
          printf("Count = %d \t T_num = %d\n", count, t_num);
    
        }
    
    printf("Finished!\n\a");
    system("pause");
    return 0;
    
    }
    What am i doing wrong?
    while i'm at. Is the triangular number not the same thing as a factorial.
    Thank you.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    atoi( ) expects a string, and you're passing it an integer.

    Also, you need to check scanf's return code, to make sure that the user does input an integer.

    >>while i'm at. Is the triangular number not the same thing as a factorial.
    Factorials are n(n-1)(n-2)...2(1). Triangular numbers are n + (n-1) + (n-2)+...+2+1.
    Last edited by XSquared; 12-13-2003 at 06:25 PM.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >char input[5]; /* uers's input */
    >int r_input,count,t_num=0; /* converted intput, counter and tri number */

    >printf("What number would you like the triangular of? ");
    >scanf("%d", &input); /* caputre data */

    Use %s for strings, and lose the &:
    scanf("%s", input); /* capture data */

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Whoops. Missed the declaration of input as a char[].
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Use %s for strings:
    >scanf("%s", input); /* capture data */

    Bad advice when there are better ways to get strings or integers.
    • scanf's %s format has the same problem that gets has: it's hard to guarantee that the receiving buffer won't overflow.
    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