Thread: Coding Woes: Program on the Fritz

1. Coding Woes: Program on the Fritz

Code:
```#include <stdio.h>
#include <math.h>
void main (void)
{
int m,k,num,x[50];
for(m=1;m<=6;m++)
{

printf("Enter the number of numbers to be entered\n");

scanf("%d",&num);

printf("\nNow scan in those numbers\n");

for(k=1;k<=num;k++);
{

scanf("%d",&x[k]);

}
printf("\n");

for (k=1;k<num;k++)
{

if(x[k+1]==x[k]*x[k])goto square;
else goto cool;

}
square:printf("square\n");
cool:printf("cool\n\n");

}
}```
The object of the program is to A) scan in the total numbers you are entering. B) Enter the numbers, C) Check to see if the next number is the square of the previous.
Ex/ Enter the amount of numbers: 3

Enter the numbers: 3 9 81

If they are the sequential square of the previous, then it will print the word square, and start the program over for a new set of data.

When it is done, it will have a list of words, "Square" if the entries follow the parameters, "cool" if they do not.

The problem I am having is, when it goes to check if it follows the parameters, the code doesnt work.

OUTPUT:

Code:
``` Enter the number of numbers to be entered
3

Now scan in those numbers
3 9 81

cool

Enter the number of numbers to be entered

Now scan in those numbers

cool

Enter the number of numbers to be entered```
FOr some reason, it prints enter the numbers to be entered once, prints now scan in those numbers, cool, before printing "enter the number of numbers to be entered" and allowing me to scan in the next data set.

After tracing the program, I cant make heads or tails of where I am going wrong, and why I am getting multiple instances of those print lines

2. > void main (void)
main returns an int

> for(k=1;k<=num;k++);
Watch that ; at the end of the line, it makes this loop do nothing.
The block of code which follows it gets executed just the once, with the final value of k

> if(x[k+1]==x[k]*x[k])goto square;
There's no reason for gotos in such a short and simple program.

3. We have always been taught, when writing programs, to use:

Code:
```#include <stdio.h>
void main (void)
{

}```

How else would you write in the main function/

4. Code:
```#include <stdio.h>
#include <math.h>

int main()
{
int m,k,num,x[50];

for(m=1;m<=6;m++)
{
printf("Enter the number of numbers to be entered\n");
scanf("&#37;d",&num);

printf("\nNow scan in those numbers\n");

for(k=1;k<=num;k++)
scanf("%d",&x[k]);
printf("\n");

for(k=1;k<num;k++)
{
if(x[k+1]== (x[k]*x[k]))
printf("square\n");
else
printf("cool\n\n");
}
}
getchar();
return 0;
}```
ssharish2005

5. Originally Posted by astrodude15
We have always been taught, when writing programs, to use:

Code:
```#include <stdio.h>
void main (void)
{

}```

How else would you write in the main function/
main returns an int, that's why you have to write:
Code:
```#include <stdio.h>
int main ()
{

}```
and if you search this forum, you'll find a thread somewhere that says why it's better not to specify void inside the parameter list.

6. Originally Posted by astrodude15
We have always been taught, when writing programs, to use:

Code:
```#include <stdio.h>
void main (void)
{

}```

How else would you write in the main function/
Understood that you have been taught by that way, but u will have get used to including int. Some teacher still follow that way which they shouldn't. Read FAQ on why the main should return an int. For brief, without return anythng from main the OS will never know wheather the program terminted successfull or failuer. by returing a value from main we specify the exit status of the program where return 0 specifies that main exited successfully return 1 specifies faulier.

ssharish2005

7. and if you search this forum, you'll find a thread somewhere that says why it's better not to specify void inside the parameter list.
This is C, so I believe that is fine.

8. > and if you search this forum, you'll find a thread somewhere that says why it's better not to specify void inside the parameter list.