Ok i'm new to the board so don't shoot me if i'm doing something wrong here....hehe...plz...I just have some problems with my code here. It's only 1 page or so. The only reason i'm posting all of it is so that you can see what i'm doing..kinda new to C++. The program is supposed to take any number of integers from the user and average them out with variable argument list usage in a function.
So here it is:
Code:1. #include <iostream> #include <stdlib.h> #include <stdio.h> #include <stdarg.h> 5. using namespace std; int average2(int num2, ...); int average2(int num2, ...) { int sum = 0, count = 0, currentNum = num2; va_list currentList; 10.va_start(currentList, num2); while (currentNum != 0) { sum += currentNum; count++; currentNum = va_arg(currentList, int); 15. } va_end(currentList); int final = sum / count; return final; } 20. int main() { cout << "How many entries do you want to average? \n> "; int i; cin >> i; int array[i + 1]; 25. cout << "Enter in the values that you want to average. \n> "; int k = 0; char t; for (k == 0; k < i; k++) { 30. cin >> array[k]; cout << "\nPress y to continue. \n"; cin >> t; if (t == 'y') { t = 'n'; 35. break; } else goto end; } 40.array[i+1] = 0; char *original = array[]; int len = strlen(original), doublelen = len * 2; char string[doublelen]; 45. for (int p = 0; p < doublelen; p++) { for (int e = -1; e < doublelen; e++) { string[e + 1] = original[e]; } for (int x = 0; x < doublelen; x++) { string[x + 1] = ","; 50. } } cout << average2(int(string[])); end: system("pause"); 54. }
Forgive me for my lack of commenting but I had to save space here..
Not only am I not sure if this will actually fully compile, because i'm converting an int array to char array...and converting a char array to a string..and trying to put a string into an int based function..but i'm not sure what is wrong on line 41...
I'm just making this program to get a more firm grasp on C++, it isn't supposed to fully compile, more along the lines of..bits and pieces are supposed to function correctly. Eventually i'll resolve this little string to int problem I have..
So any idea's?
And feel free to state something you don't like about my coding style or..how I code things or whatnot..what you see wrong with my looping etc. anything that you could reccomend that would make this better?
-Thank you.



LinkBack URL
About LinkBacks




