# Trouble with if statement

• 11-30-2007
Tegan
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
• 11-30-2007
Elysia
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.
• 11-30-2007
matsp
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
• 11-30-2007
Elysia
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.
• 11-30-2007
Tegan
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
• 11-30-2007
Elysia
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.
• 11-30-2007
matsp
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
• 11-30-2007
Tegan
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 :)
• 11-30-2007
Elysia
Quote:

Originally Posted by Tegan
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 >_<
• 11-30-2007
matsp
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
• 11-30-2007
Tegan
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
• 11-30-2007
Elysia
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.
• 11-30-2007
matsp
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