need help

my professor sucks i need help with some kind of text thing

write a function to read #s from a file and calculate their sum, output to screen

input file name is arbitrary

someone please help!!!
what do you have so far ?
thats the thing he did not show us anything and we dont have a book on this shit so i dont know where to start shadow fiend
even hello world ?? by the way you can look at the documentation on this site, it's quite hard at first but you'll get it, trust me :///

http://www.cplusplus.com/doc/tutorial/

write a function to read #s from a file and calculate their sum, output to screen


i will provide the function snippet, what you gonna do basically is just #include necessary headers, create a main function, then call this function :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
 * the syntax for a function is :
 * <return_type> <function_name> ( <parameters> )
 */
void some_funtion( void )
{
    // ifstream class is used to "read" from a file
    // ofstream on the other hand is used to "write" to a file
    ifstream inputFile;
    
    // open the file :
    // replace <name_of_file> with the actual name of the file
    // you want to read from
    inputFile.open( "<name_of_file>.txt" );
    
    // create a variable to hold the "total" sum :
    int sum = 0;
    // create another variable to hold the temporary value read
    int temp;
    
    // read, add it to sum, read again, add to sum, and so on
    // until end of file is found
    while( inputFile >> temp )
        sum += temp; // this is the same as temp = temp + sum
        
    
    /* Now, a simple test :
     * all you need to do is print the value of sum variable,
     * i assume you know cout ?
     */
    // ...
    
}
Topic archived. No new replies allowed.