445,655 Members | 959 Online Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

# Simple Program using Arrays

 P: n/a I am a newbie to C++ and I need help with a simple program I am trying to write. My program is designed to print all of the odd integers contained in an array and output the sum of the odd integers. My code is listed below, but the problem is that my output of sum is wrong. For example, I am using 1347830 for my integers and the program outputs 373 and after adding 373 you should get 13 for the total, but that doesn't happen. Any help you can provide is appreciated! Thanks! ------------------------------------------------------------*--- #include #include void main() { char numbers; int sum = 0; cout << "Barcode: "; cin.get(numbers, 20); for(int i = 1; i < numbers[i]; i+=2) { cout << numbers[i]; sum += numbers[i]; } cout << endl; cout << sum; cout << endl; } Jul 23 '05 #1
6 Replies

 P: n/a ho************@hotmail.com wrote: I am a newbie to C++ and I need help with a simple program I am trying to write. My program is designed to print all of the odd integers contained in an array and output the sum of the odd integers. My code is listed below, but the problem is that my output of sum is wrong. For example, I am using 1347830 for my integers and the program outputs 373 and after adding 373 you should get 13 for the total, but that doesn't happen. Any help you can provide is appreciated! Thanks! ------------------------------------------------------------*--- #include #include void main() { char numbers; int sum = 0; cout << "Barcode: "; cin.get(numbers, 20); for(int i = 1; i < numbers[i]; i+=2) { cout << numbers[i]; sum += numbers[i]; } I think you should write i < 20 instead of i < numbers[i]. More precisely i < (number of elements you actually put in the array...) Bye Andrea Jul 23 '05 #2

 P: n/a ho************@hotmail.com wrote: I am a newbie to C++ and I need help with a simple program I am trying to write. My program is designed to print all of the odd integers contained in an array and output the sum of the odd integers. My code is listed below, but the problem is that my output of sum is wrong. For example, I am using 1347830 for my integers and the program outputs 373 and after adding 373 you should get 13 for the total, but that doesn't happen. Any help you can provide is appreciated! Thanks! ------------------------------------------------------------*--- #include #include void main() { char numbers; int sum = 0; cout << "Barcode: "; cin.get(numbers, 20); for(int i = 1; i < numbers[i]; i+=2) { cout << numbers[i]; sum += numbers[i]; } cout << endl; cout << sum; cout << endl; } Note that arrays in C/C++ start at index 0. That means, the first odd number would be at , the next at  and so on. Jul 23 '05 #3 