Thread: Trouble with if statement

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    4

    Trouble with if statement

    Hi ive got an assignment that asks the user to input sets of data (x,y) and then stores them in an array and does various things with them, the program asks for the number of data sets to be entered (so that i know how big the array will be), the number of data sets must be a positive integer , so i wanted to put and 'if' statement that printed an error message if the user inputted zero or a negative number, it works fine for zero, -1 , -2 and -3 , but anything below -3 and the program shuts down, cant work out why its doing that, would appreciate any tips in figuring it out

    Heres my code:
    Code:
    #include<Stdio.h>
    
    int x,y,N;
    int main()
    
    {
    
    
        printf("How many pairs of sets of data are there?(note:must be a positive integer): ");
        scanf("&#37;d",&N);
        if (N<=0) printf("Error Restart program");
    
    
    
        float array [N][2];
    
    
        for (x=0;x<N;x++)
    
            {
                printf("Enter the two values of the data set (leave a space inbetween)  :  "); /* does work if you put minus numbers in*/
                scanf("%f%f",&array[x][0],&array[x][1]);
                printf("\n");
    
                /*printf("(%f,%f)\n",array[x][0],array[x][1]);  omit this line in final program*/
            }
    
            printf("   x       y");
            printf("\n\n");
            for (x=0;x<N;x++)
    {
        printf("%6.2f %6.2f\n",array [x][y],array [x][y+1]);
        printf("\n");
    
    }
        printf("You entered the above data sets");
        printf("\n");
    Thank you

    Tegan

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Use proper indenting, please.
    First problem:
    Code:
    if (N<=0) printf("Error Restart program");
    Checks if N is equal to or less then 0, prints and error and continues the program. You should probably wrap it in brackets and do an exit there.

    Second error:
    Code:
    float array [N][2];
    Invalid - arrays must be a constant size. You can't do a dynamic array on the stack. You'll have to use malloc and do it on the heap instead.

    Code:
    scanf("%f%f",&array[x][0],&array[x][1]);
    I'm not sure that works.
    Perhaps it's better to use two scanf and ask for two numbers instead of asking the user to enter two numbers at the same time.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't see any if-statement in your code.

    In standard C, you can't have run-time sized arrays, so this construct will not work in all compilers:
    Code:
        float array [N][2];
    You can either dynamically allocate memory, or just replace N with a "large enough to work for all likely cases" constant. [say 10000 or 100000].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Matsp,
    Code:
    if (N<=0) printf("Error Restart program");
    This one perhaps?

    OP: You're also using the SAME counting variable in TWO loops, which won't work as you intend it. You'll probably end up with a borked program.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    4
    Thank you for your help Ive got a lot to work on there, if i allocate lots of memory to the array and then run a loop asking whether or not the user wants to input a set of data would that be better than asked for the set amount of data values? Just thought that might make the code less clumsy by asking for a value and running the loop that number of times

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can do either way, of course. A real program would dynamically resize the array when there isn't enough room available.
    Another option is to use C++ and std::vector.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sure, asking for "how many" is fine. You could then either ask again if you get a "silly" answer (0 or negative), or just exit.

    Then the choice is how you deal with this size. You can allocate memory, but it's a bit messy, or you can "guess" the maximum size anyone would ever want. [and then in the range of "silly" answers also have a upper bound, saying "Too many, can't handle it"]

    By the way, to be stricly C compliant, you should also move the declaration of your array up to before the first "code" statement [in this case your first printf()].

    And you probably don't need N, x and y to be global variables, so you can move them into main.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    4
    Elysia i am actually studying a C module - isnt C++ a different type of programming? We are working with codeblocks, can you switch between the code for C and C++ without causing problems?

    Thanks for all the tips matsp - can see ive got a long way to go yet, really want to get it right, im determined to be good at this

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Tegan View Post
    Elysia i am actually studying a C module - isnt C++ a different type of programming? We are working with codeblocks, can you switch between the code for C and C++ without causing problems?
    C++ is C and ++. Basically C with extensions. Yeah, you can write C in C++ too, but C++ is generally much better at things such as memory handling, arrays, etc. Making it much easier to code such a program as yours with minimal worries.
    It's up to you to actually try to use C++, but I recommend everyone that uses C to use C++. I find it depressing that so many are actually using plain old C >_<

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you are studying C, then you shouldn't mess with C++. C++ has a base in C, but there's a whole heap of differences, including what and how you do things.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    4
    Got ya - think ill stick to c just to keep my lecturers happy, might learn C++ as an aside anyway as im pretty sure that we will learn about that aswell later on

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Really? C and C++ are generally considered different languages, so if you learn C, then you learn C and not C++.
    But back on topic, I think we've pointed out most of the problems. I don't know if you want to try or should try to use a dynamic array to hold the data since it can be a little advanced.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sure, but remember that you are learning two things right now:
    1. How to program
    2. The language C.

    The former is like learning to drive, and the second is like learning how to drive, say, a Ford Escort [loosely quoting Salem - or was it laserlight?]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Switch statement
    By beene in forum C++ Programming
    Replies: 21
    Last Post: 07-01-2007, 08:13 AM
  3. If statement being ignored?
    By FincH in forum C Programming
    Replies: 3
    Last Post: 04-18-2007, 01:51 PM
  4. having trouble understanding this statement
    By kes103 in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2003, 09:00 AM
  5. string & if statement
    By Curacao in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2003, 09:56 PM