why does my program print off the wrong answers?

This is a discussion on why does my program print off the wrong answers? within the C Programming forums, part of the General Programming Boards category; ok my program is relitivly simple at the moment but i cant get it to print out what class and ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    21

    why does my program print off the wrong answers?

    ok my program is relitivly simple at the moment but i cant get it to print out what class and id that you select will some one show me what i did wrong??
    Code:
    #include <stdio.h>
     main ()
    {
    char *class_name[03]={"hunter","ranger","force"};
    char *statnum   [06]={"SYNC","IQ  ","DEF ","POW ","DEX ","MIND"};
    char *section_id[10]={"viridia","greenil","skyly","bluefull"  ,"purplenum",
                      "pinkal" ,"redria" ,"oran" ,"yellowboze","whitill"};
    int *ID;
    
    int *class;
    int blah;
    int qwe;
    int comm[4][9];
    	int stat[7][2];			 /* initialize STATS */ 
        for (qwe=0;qwe<7;qwe++)
        {
           for (blah=0;blah<2;blah++)
           {
        	stat [qwe][blah] =0;
          }
        }	
        stat [0][0] =40;			 /* initialize SYNC */
        stat [2][0] =5;			/* initialize DEF */
        printf ("Enter section ID \n");
        for (qwe=0;qwe<10;qwe++)
         {
         printf ("%i : %s\n",qwe,section_id[qwe]);
         }
        scanf  ("%i",&ID);
    blah=ID;
    
       
        printf ("Enter character class \n");
        for (qwe=1;qwe<4;qwe++)
         {
         printf ("%i :  %s\n",qwe,class_name[qwe-1]);
         }
        scanf  ("%i",&class);
         
      printf ("your section id is\n %s \n",section_id[*ID]);
      printf ("your class is \n%s \n",class_name[*class-1]);
      
      
    for (qwe=0;qwe<6;qwe++) printf ("%s is %i . %i \n",statnum[qwe],stat[qwe][0],stat[qwe][1]);
    
    }
    thanx

    ~kl3pt0~

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    First thing I noticed:
    char *class_name[03]={"hunter","ranger","force"};
    char *statnum [06]={"SYNC","IQ ","DEF ","POW ","DEX ","MIND"};
    Get rid of the preceeding 0. The preceding 0 changes it from a base 10 number to a base 8 (octo) number. While it doesn't have much affect as of yet it could potentionally become a problem later if you don't learn it now

    Also:
    Code:
    blah=ID;
    blah is an int and ID is a pointer to an int.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Change ID and class to be ints, instead of pointers to ints.

    Remove this:
    >blah = ID;

    At least you should be on your way...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    21
    ok i changed what you guys said to but it still gives me part of one of the strigs, (null) or just blank heres the revised code tell me is theres any part i missed
    Code:
    #include <stdio.h>
     main ()
    {
    char *class_name [3]={"hunter","ranger","force"};
    char *statnum    [6]={"SYNC","IQ  ","DEF ","POW ","DEX ","MIND"};
    char *section_id[10]={"viridia","greenil","skyly","bluefull"  ,"purplenum",
                      "pinkal" ,"redria" ,"oran" ,"yellowboze","whitill"};
    int ID;
    int class;
    int blah;
    int qwe;
    int comm[4][9];
    	int stat[7][2];			 /* initialize STATS */ 
        for (qwe=0;qwe<7;qwe++)
        {
           for (blah=0;blah<2;blah++)
           {
        	stat [qwe][blah] =0;
          }
        }	
        stat [0][0] =40;			 /* initialize SYNC */
        stat [2][0] =5;			/* initialize DEF */
        printf ("Enter section ID \n");
        for (qwe=0;qwe<10;qwe++)
         {
         printf ("%i : %s\n",qwe,section_id[qwe]);
         }
        scanf  ("%i",ID);
    
        printf ("Enter character class \n");
        for (qwe=1;qwe<4;qwe++)
         {
         printf ("%i :  %s\n",qwe,class_name[qwe-1]);
         }
        scanf  ("%i",class);
         
      printf ("your section id is\n %s \n",*section_id[ID]);
      printf ("your class is \n%s \n",*class_name[class-1]);
      
      
    for (qwe=0;qwe<6;qwe++) printf ("%s is %i . %i \n",statnum[qwe],stat[qwe][0],stat[qwe][1]);
    
    }
    thanx

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    21
    ok i changed
    Code:
      printf ("your section id is\n %s \n",*section_id[ID]);
      printf ("your class is \n%s \n",*class_name[class-1]);
    to

    Code:
      printf ("your section id is\n %i \n",ID);
      printf ("your class is \n%i \n",class-1);
    and when you in put you get

    your section id is (random number less or = to 0)
    your class is -29

    so now yeah..........im lost

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Sorry, I should have said before:
    >scanf ("%i",&ID);
    >scanf ("%i",&class);
    Note: You need the & when you're using a real int variable.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    21
    ok sweet that worked now i have one more question
    how can i check to make shure that what you input has no decimals would it be a stament like
    Code:
    if (ID!=round(ID)) return (0);
    and if thats what i need do i need any new include staments?

  8. #8
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    round is in math.h and you would need to include that. and if in gcc you would need to compile with
    Code:
    gcc -o test test.c -lm
    [edit] Now looking at your code you cannot have ID as a decimal, because your number is an int.[/edit]
    [edit]and I would change scanf("%i... to scanf("%d... because %i can intake other number bases say I enter 023 that is octal. I don't think you would want that.[/edit]
    [edit](Wow I need to stop) if you wanted to use the round function you would have to coerce or typecast ID to a double or use roundf then you would have to force it a float. You also don't use the variable comm and you shoud have int main(void) not main(){ then you need to return 0[/edit]
    Last edited by linuxdude; 06-09-2004 at 07:58 PM.

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    21
    Quote Originally Posted by linuxdude
    round is in math.h and you would need to include that. and if in gcc you would need to compile with
    Code:
    gcc -o test test.c -lm
    [edit] Now looking at your code you cannot have ID as a decimal, because your number is an int.[/edit]
    [edit]and I would change scanf("%i... to scanf("%d... because %i can intake other number bases say I enter 023 that is octal. I don't think you would want that.[/edit]
    [edit](Wow I need to stop) if you wanted to use the round function you would have to coerce or typecast ID to a double or use roundf then you would have to force it a float. You also don't use the variable comm and you shoud have int main(void) not main(){ then you need to return 0[/edit]
    wow look like you had fun editing
    and the reason for there being pointless ints and no return at the end is because this is just the 1st part of my program

    i found another question is there a command to clear you screen insted of it just scroling like it does?
    Last edited by kl3pt0; 06-09-2004 at 08:09 PM.

  10. #10
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    they are all OS dependent if you are in linux it is
    Code:
    system("clear");
    in DOS
    Code:
    system("cls");(I think)
    or if you have conio.h
    Code:
    #include <conio.h>
    ...
    clrscr();

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could have just told them to read the FAQ link on it, or search the forum, since this question gets posted about fifty times a week.

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    I felt ambitious

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>how can i check to make shure that what you input has no decimals
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    Registered User
    Join Date
    Jun 2004
    Posts
    21
    yeah the reason i asked about how to clear screen is because ever time i try
    Code:
    system("cls");
    it goes to the start of main()

    but now i have a final question for this thread i need to make a 3D int thats size is 8X11X6 the data is inbetween -99 and 99 heres like what i would need

    the matix [1][][] is

    3,3,5,40,5,0,
    3,3,10,45,5,0,
    4,4,15,50,10,0,
    3,3,5,0,5,40,
    3,3,10,0,5,45,
    4,4,15,0,10,50,
    3,3,5,10,40,0,
    3,3,5,0,44,10,
    4,1,15,30,15,25,
    4,1,15,25,15,30,
    6,5,25,25,25,25

    but im not shure on how to put in in to the program

    thanx

    ~kl3pt0~

  15. #15
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    3d array sample:
    Code:
     #include <stdio.h>
     #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
     
     int main(void)
     {
       int myarray[2][3][4];
       int i, j, k;
       
       
       for (i = 0; i < ARRAY_SIZE(myarray); i++)
       {
         for (j = 0; j < ARRAY_SIZE(myarray[0]); j++)
         {
           for (k = 0; k < ARRAY_SIZE(myarray[0][0]); k++)
           {
             myarray[i][j][k] = (i*100) + (j*10) + (k);
           }
         }
       }
       
       
       for (i = 0; i < ARRAY_SIZE(myarray); i++)
       {
         for (j = 0; j < ARRAY_SIZE(myarray[0]); j++)
         {
           for (k = 0; k < ARRAY_SIZE(myarray[0][0]); k++)
           {
             printf ("[%d][%d][%d] = %03d\n", i, j, k, myarray[i][j][k]);
           }
         }
       }
       
       return(0);
     }
     
     /* 
     Output
     
     [0][0][0] = 000
     [0][0][1] = 001
     [0][0][2] = 002
     [0][0][3] = 003
     [0][1][0] = 010
     [0][1][1] = 011
     [0][1][2] = 012
     [0][1][3] = 013
     [0][2][0] = 020
     [0][2][1] = 021
     [0][2][2] = 022
     [0][2][3] = 023
     [1][0][0] = 100
     [1][0][1] = 101
     [1][0][2] = 102
     [1][0][3] = 103
     [1][1][0] = 110
     [1][1][1] = 111
     [1][1][2] = 112
     [1][1][3] = 113
     [1][2][0] = 120
     [1][2][1] = 121
     [1][2][2] = 122
     [1][2][3] = 123
     
     */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 02-17-2005, 08:27 PM
  2. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  3. Replies: 1
    Last Post: 03-12-2002, 05:31 AM
  4. C++ program to print itself
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 11-27-2001, 01:25 PM
  5. Pretty Print Program
    By 2TheGrindStone in forum C++ Programming
    Replies: 5
    Last Post: 11-16-2001, 11:16 AM

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