// HelloWorldDemo.c : Defines the entry point for the console application. // // This is just a simple demo of C program structure. // This shows the C input and output statements, with the C++ versions // shown as comments above each C statement. // Quickly cobbled together by Patrick Kelley 2/7/2014 // #include "stdafx.h" // INCLUDE THIS ONLY IF YOUR IDE NEEDS IT // #include // using namespace std; #include "stdio.h" // define global variables here int my_array[5]; // pre-declare functions int myfunc(); int sum_it(int*, int); // actual code begins here /* int myfunc() This function prompts the user for up to 5 numbers to go into the global array my_array. It then calls sum_it to get the sum of the numbers in the array and outputs the result. No strict input checking is performed in this simple example. */ int myfunc() { int return_value = 0; // holds the good return value int input_count = 0; // keeps track of how many inputs we have int in_value = 0; // used for getting values into the array char yes_no; // used for yes/no input // lets make a nice greeting // cout << "Hello there, friend. Let's add some numbers. Please give me"; // cout << " up to 5 numbers to add." << endl; printf("Hello there, friend. Let's add some numbers. Please give me"); printf(" up to 5 numbers to add.\n"); while (input_count < 5) { // ask for a number and repeat it // cout << "Give me a number: "; printf("Give me a number: "); // cin >> in_value; scanf("%d", &in_value); // cout << endl << "Ok, you gave me " << in_value << "." << endl; printf("Ok, you gave me %d.\n", in_value); // cout << "Just let me write that down." << endl; printf("Just let me write that down.\n"); // store the number in the array and increase the count my_array[input_count] = in_value; input_count++; // same as: input_count = input_count + 1 // is the array full? if (input_count == 5) { // cout << endl << "That's enough numbers, let's add them up."; printf("\nThat's enough numbers, let's add them up."); break; } // now ask if we're done while (true) { // cout << "Is that all the numbers? (y/n) "; printf("Is that all the numbers? (y/n) "); // cin >> yes_no; scanf("%c", &yes_no); // see if the response was valid char test = toupper(yes_no); if ((toupper(yes_no) == (char)'N') || (toupper(yes_no) == (char)'Y')) break; else { // cout << endl << "Aw, come on! Y for yes or N for no!" << endl; printf("\nAw, come on! Y for yes or N for no!\n"); } } // see what the answer was and respond accordingly if (toupper(yes_no) == (int)'Y') break; else //cout << endl << "Ok, I'm ready! "; printf("\nOk, I'm ready! "); } // add the numbers and output the result // cout << endl << "Let me think, let me think ..." << endl; printf("\nLet me think, let me think ...\n"); // cout << "The answer is " << sum_it(my_array, input_count) << "!" << endl; printf("The answer is %d!\n", sum_it(my_array, input_count)); // cout << "That was hard! I'm going to take a nap now. Oyasumi nasai!"; printf("That was hard! I'm going to take a nap now. Oyasumi nasai!"); return 0; // assume nothing bad happened } /* int sum_it(int* array, int count) sum_it is a function that takes as it's first parameter a pointer to an array of integers and a second parameter that is a count of the numbers to be added from the array. It does no checking to see if the count is correct; it is up to the caller to make sure the array and count are right. Internally, it loops the right number times to add the numbers together, and stores the result for returning once the loop is finished. */ int sum_it(int* array, int count) { int return_value = 0; // create and initialize storage for the sum // loop and add the numbers for (int i = 0; i < count; i++) { return_value += array[i]; // add the next number to the result } // return the result return return_value; } // This function is the main entry point to the program // Note that this version is slightly different from the C++ version int main(int argc, char* argv[]) { myfunc(); // The following loops infinitely to 'stop' the program while still // keeping the environment running. // A better way would be: // char dummy_var; // cout << "Press Enter to continue"; // cin << dummy_var; while (true) { } return 0; }