Wilan Wong
01-01-2002, 01:10 AM
I need help debugging a small and simple C program.. I dont' know why it doesn't work.. but hopefully you guys can figure it out.. download the source files wif these links.. thnx!
http://wilan.ausgamers.com/cpp/prog1.zip
Oh yeh.. I'm only having one error.. which is I'm having trouble sending an array to another function..
Can you do this following line of code??
result = add(addf[20]);
i get an error..
D:\programming\sample\sample.cpp(55) : error C2664: 'add' : cannot convert parameter 1 from 'int' to 'int []'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.
I have one source file and 1 source header.. (wanted to try something new).. The add() function is in another file.. called SAMPLE.H and the line I copied is from SAMPLE.CPP.
Any help would be great! :)
CODE
//////////////////////////////////////
// SAMPLE.CPP - Main Source File //
//////////////////////////////////////
#include <stdio.h>
#include "sample.h"
// Global Variables
int addf[20];
int result=0;
int loop_input=0;
int loop_count=0;
int loop_amount=0;
main()
{
// Clears/Declares values for array
int count=0;
for (; count>=20; count++)
{
addf[count]=0;
}
// Private Variable Declarations
int incre_array=0;
// UI (User Interface)
printf("Welcome to the multiple addition program.");
printf("\nThis program supports up to 20 inputs,");
// Prompt User to Input
printf("\nHow many additions do you want to do?");
printf("\nEx. 2+3+6 is 3 additions");
printf("\nCurrent Amount of Additions: ");
// Receive User Input
scanf("%d",&loop_amount);
// Prompt User to Enter the values
loop_input = loop_amount;
for (; !loop_input==0; loop_input--)
{
printf("\n\nInput a value: ");
printf("\nYour current value is: ");
scanf("%d",&addf[incre_array]);
}
// Call the add() function
result = add(addf[20]);
printf("\n\n");
printf("The final answer is: %d",&result);
return 0;
}
//////////////////////////////////////////////////
// SAMPLE.H - Header File //
// Contains multiple Mathematical Functions //
//////////////////////////////////////////////////
// The add() function adds multiple operations together
// Supports up to 20 additions
int add(int array[20]);
int add2(int x,int y);
int add(int array[20])
{
// Private Variable Declarations
int temp_result=0;
int temp_result2=0;
int incre_array2=0;
int incre_array3=1;
int loop_amount=20;
for(; !loop_amount==0; loop_amount -= 2)
{
temp_result = add2(array[incre_array2], array[incre_array3]);
incre_array2 += 2;
incre_array3 += 2;
temp_result2 += temp_result;
}
return temp_result2;
}
// The add2() function adds 2 inputs together and returns it
// to the calling function, which is add()
int add2(int x, int y)
{
return x+y;
}
http://wilan.ausgamers.com/cpp/prog1.zip
Oh yeh.. I'm only having one error.. which is I'm having trouble sending an array to another function..
Can you do this following line of code??
result = add(addf[20]);
i get an error..
D:\programming\sample\sample.cpp(55) : error C2664: 'add' : cannot convert parameter 1 from 'int' to 'int []'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.
I have one source file and 1 source header.. (wanted to try something new).. The add() function is in another file.. called SAMPLE.H and the line I copied is from SAMPLE.CPP.
Any help would be great! :)
CODE
//////////////////////////////////////
// SAMPLE.CPP - Main Source File //
//////////////////////////////////////
#include <stdio.h>
#include "sample.h"
// Global Variables
int addf[20];
int result=0;
int loop_input=0;
int loop_count=0;
int loop_amount=0;
main()
{
// Clears/Declares values for array
int count=0;
for (; count>=20; count++)
{
addf[count]=0;
}
// Private Variable Declarations
int incre_array=0;
// UI (User Interface)
printf("Welcome to the multiple addition program.");
printf("\nThis program supports up to 20 inputs,");
// Prompt User to Input
printf("\nHow many additions do you want to do?");
printf("\nEx. 2+3+6 is 3 additions");
printf("\nCurrent Amount of Additions: ");
// Receive User Input
scanf("%d",&loop_amount);
// Prompt User to Enter the values
loop_input = loop_amount;
for (; !loop_input==0; loop_input--)
{
printf("\n\nInput a value: ");
printf("\nYour current value is: ");
scanf("%d",&addf[incre_array]);
}
// Call the add() function
result = add(addf[20]);
printf("\n\n");
printf("The final answer is: %d",&result);
return 0;
}
//////////////////////////////////////////////////
// SAMPLE.H - Header File //
// Contains multiple Mathematical Functions //
//////////////////////////////////////////////////
// The add() function adds multiple operations together
// Supports up to 20 additions
int add(int array[20]);
int add2(int x,int y);
int add(int array[20])
{
// Private Variable Declarations
int temp_result=0;
int temp_result2=0;
int incre_array2=0;
int incre_array3=1;
int loop_amount=20;
for(; !loop_amount==0; loop_amount -= 2)
{
temp_result = add2(array[incre_array2], array[incre_array3]);
incre_array2 += 2;
incre_array3 += 2;
temp_result2 += temp_result;
}
return temp_result2;
}
// The add2() function adds 2 inputs together and returns it
// to the calling function, which is add()
int add2(int x, int y)
{
return x+y;
}