Weird Error

This is a discussion on Weird Error within the C Programming forums, part of the General Programming Boards category; Hello today i wanted to know like how many strings inside a str * but problem its 1 + orginal ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    187

    Weird Error

    Hello today i wanted to know like how many strings inside a str * but problem its 1 + orginal strings and I wanted to make this following code so the str get rand strings only after like 2nd string only but i keep getting weird results as if the thing counted the strings as 7 not 6
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    char *Names[]= {
        "first",//stops
        "second",//vowels
        "third",
        "fourth","fifth","sixth","seventh"
    };
    int main(void)
    {
        int y;
        srand(time(NULL)); 
        y=  2+ rand() % sizeof (Names) / sizeof(Names[0]);
    
        printf("%d\n",y);
        puts(Names[y]);
        getchar();
        return 0;
    }
    try print
    y= sizeof (Names) / sizeof(Names[0]);
    and it will print 7th character not 6th i know its 7 but arrays start from zero so i dunno how to fix this .....
    and i dunt wanna make it so i have some tests if to test if y==7 --y because that would be lame :P

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I have no idea what you're asking. sizeof(Names)/sizeof(Names[0]) is 7. This means that when you do %7, you will get either 0, 1, 2, 3, 4, 5, or 6. (You can't have a remainder of 7 when you divide by 7.) So you could use y as an index perfectly, except for the fact that you add two to it....

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    i want the rand to start at 2 not so thats y i added the code to it

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    i want random elements from 2 to 6

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So if you only want five possibilities, then you have to % by 5, not by 7.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    but it will start between 0 to 5 not 2 6 it will choose only from strings 0 to 5

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by elwad
    but it will start between 0 to 5 not 2 6 it will choose only from strings 0 to 5
    That is where the addition of 2 comes in. (Besides, it will be from 0 to 4, not 0 to 5).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    Quote Originally Posted by laserlight View Post
    That is where the addition of 2 comes in. (Besides, it will be from 0 to 4, not 0 to 5).
    yes but if you see in my code that it types 7 or 8 wrong and if i make it with % output will be 0 like if i made y % 7

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by elwad
    yes but if you see in my code that it types 7 or 8 wrong and if i make it with % output will be 0 like if i made y % 7
    You have not posted your updated code, so I see nothing relevant in code. If you have not updated your code, then read tabstop's post #5 and my post #7 again.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    yah i fixed it i think
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    char *Names[]= {
        "first",//stops
        "second",//vowels
        "third",
        "fourth","fifth","sixth","seventh"
    };
    int main(void)
    {
        int y;
        srand(time(NULL)); 
        y=2+rand() % (sizeof (Names) / sizeof(Names[0])-2);
        printf("%d\n",y);
        puts(Names[y]);
        getchar();
        return 0;
    }
    first we + 2 to rand() then after that we minus the number so it get only 5 possiblities is that right ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what am I missing? (Program won't compile)
    By steals10304 in forum C Programming
    Replies: 3
    Last Post: 08-25-2009, 03:01 PM
  2. Linking to shlwapi.lib in C, MSVC CMD.
    By Joerge in forum Windows Programming
    Replies: 4
    Last Post: 08-07-2009, 05:18 PM
  3. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 03:35 PM
  4. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  5. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM

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